A number puzzle
Time Limit: 3000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/others) total submission (s): 938 accepted submission (s): 276
Problem descriptionlele was boring during his recent classes, so he invented a digital game to pass the time.
This game is like this. First, he took out a few pieces of paper and wrote any number between 0 and 9 (a number can be re-rewritten). Then, he asked his classmates to write two numbers, X and K. What Lele wants to do is to fight these cards again to form a digital T, and t + X is a positive integer multiple of K.
Sometimes, when there are a lot of pieces of paper, Lele often cannot be spelled out in a lesson, but he wants to know the answer, so he wants to ask you to write a program to calculate the answer. The input question contains multiple groups of test data. Please process the data until the end of the file. The first row of each data group contains two integers, N and M (0 <n <9, 0 <m <2000), representing the number of pieces of paper and the number of queries respectively. The second row contains N integers representing the numbers written on the paper. Each number may be 0 ~ 9. Next, let's look at m rows. Each query has two integers x and K (0 <= x <10 ^ 9,0 <k <100 ).
Note: When you splice a piece of paper, each piece of paper must be used, and T must not start with 0 output. If you can use these pieces of paper to spell out the correct t, output result t. If multiple results exist, the minimum t meeting the requirements is output. If it cannot be spelled out, "NONE" is output ". Sample input4 31 2 3 45 733 612 8 sample output1234none1324 question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1755 solution: I knew there was a ready-made function, so I didn't have to spend most of my time. Next_permutation (), the function for full arrangement, header file <algorithm>. With the function, everything is easy to say!
1 #include<stdio.h> 2 #include<math.h> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 int arr[40320*9]; 7 int main() 8 { 9 int n,m,x,k,i,j,beg,flag,number;10 int num[10];11 while(scanf("%d%d",&n,&m)!=EOF)12 {13 memset(arr,0,sizeof(arr));14 for(i=0;i<n;i++)15 scanf("%d",&num[i]);16 sort(num,num+n);17 i=0;18 do19 {20 if(num[0])21 {22 number=0;23 int t=n;24 for(j=0;j<n;j++)25 number+=num[j]*pow(10,--t);26 arr[i++]=number;27 beg=i;28 } 29 }while(next_permutation(num,num+n));30 while(m--)31 {32 scanf("%d%d",&x,&k);33 flag=1;34 for(i=0;i<beg;i++)35 if( (arr[i] + x)%k == 0 )36 {37 printf("%d\n",arr[i]);38 flag=0;39 break;40 } 41 if(flag)42 printf("None\n");43 }44 } 45 return 0;46 }