1. 程式人生 > >下一排列問題(全排列暴力)

下一排列問題(全排列暴力)

A Number Puzzle

Time Limit: 1000 ms /Memory Limit: 32768 kb

Description

Lele 最近上課的時候都很無聊,所以他發明了一個數字遊戲來打發時間。

這個遊戲是這樣的,首先,他拿出幾張紙片,分別寫上0到9之間的任意數字(可重複寫某個數字),然後,他叫同學隨便寫兩個數字X和K。Lele要做的事情就是重新拼這些紙牌,組成數字 T ,並且 T + X 是 K 的正整數倍。

有時候,當紙片很多的時候,Lele經常不能在一節課之內拼出來,但是他又想知道答案,所以,他想請你幫忙寫一個程式來計算答案。

Input

本題目包含多組測試資料,請處理到檔案結束。
每組資料第一行包含兩個整數 N和M(0<N<9,0<M<2000),分別代表紙片的數目和詢問的數目。
第二行包含N個整數分別代表紙片上寫的數字,每個數字可能取0~9。
接下來有M行詢問,每個詢問給出兩個整數X和K(0<=x<10^9,0<K<100)。

注意:在拼紙片的時候,每張紙片都必須用上,且T首位不能為0

Output

對於每次詢問,如果能夠用這些紙片拼出符合答案的T,就輸出結果T。如果有多個結果,就輸出符合要求的最小的T。
如果不能拼出,就輸出"None"。

Sample Input

4 31 2 3 45 733 612 8

Sample Output

1234None1324

Source

2007省賽集訓隊練習賽(6)_linle專場

主要就是使用c++iostream裡帶的next_permutation,雖然大體意思一樣,但是時間不同,主要是可以打表實現查詢不用再重新拍一次

ac程式碼

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;

int n,m,x,k;
int a[10];
int main()
{
    int i,j;
    ll temp;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0; i<n; ++i)scanf("%d",&a[i]);

        for(i=0; i<m; ++i)
        {
            scanf("%d%d",&x,&k);
            sort(a,a+n);
            bool flag=false;

            do
            {  temp=0;
                ll power=pow(10,n-1);
            if(a[0]){
                    for(j=0; j<n; ++j)
                    {
                        temp+=a[j]*power;
                        power/=10;
                    }
                }
                else continue;
                if((temp+x)%k==0)
                {   //cout<<temp<<"SSS"<<endl;
                    flag=true;
                    break;
                }

                //for(j=0;j<n;++j)cout<<a[j]<<" ";
            }
            while(next_permutation(a,a+n));
            if(flag)
            {
                for(j=0;j<n;++j)
                   printf("%d",a[j]);
                printf("\n");
            }
            else printf("None\n");
        }
    }
    return 0;
}

tle程式碼

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;

int n,m,x,k;
int a[10];
int main()
{
    int i,j;
    ll temp;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=0; i<n; ++i)scanf("%d",&a[i]);

        for(i=0; i<m; ++i)
        {
            scanf("%d%d",&x,&k);
            sort(a,a+n);
            bool flag=false;

            do
            {  temp=0;
                ll power=pow(10,n-1);
            if(a[0]){
                    for(j=0; j<n; ++j)
                    {
                        temp+=a[j]*power;
                        power/=10;
                    }
                }
                else continue;
                if((temp+x)%k==0)
                {   //cout<<temp<<"SSS"<<endl;
                    flag=true;
                    break;
                }

                //for(j=0;j<n;++j)cout<<a[j]<<" ";
            }
            while(next_permutation(a,a+n));
            if(flag)
            {
                for(j=0;j<n;++j)
                   printf("%d",a[j]);
                printf("\n");
            }
            else printf("None\n");
        }
    }
    return 0;
}