湖南大學第十四屆ACM程序設計新生杯 E.Easy Problem
E.Easy Problem
Description:
Zghh likes number, but he doesn‘t like writing problem description. So he will just give you a problem instead of telling a long story for it.
Now given a positive integer x and k digits a1,a2,...,ak, can you find a positive integer y such that y is the multiple of x and in decimal representation y contains all digits of a1,a2,...,ak.
Input:
The first line contains an integer T (1<=T<=10000) which is the number of test case.The following T lines each line is a test case, start with two integer x (1<=x<=1e8) and k (1<=k<=10), k integer a1,a2,..,ak (0<=ai<=9 for i=1..k and ai!=aj for i!=j) is following.
Output:
For each test case output your answer y. Your answer should be a positive integer without leading zero and should be no more than 1e18. Every answer that satisfy the conditions descripted above will be accepted.
Sample Input:
3
5 3 1 5 7
21 4 2 5 6 9
10 9 0 1 2 3 4 5 6 7 9
Sample Output:
175
2592576
題意:
多組數據,每組數據給出一個數x,然後k個0~9的數,現在要你求出一個數y,滿足y%x=0並且y包含這k個數。
題解:
比賽的時候想了半天都沒有想到啊...後來看別人的代碼恍然大悟。
註意這裏的數據範圍,x只有1e8,然後0~9一共10個數,所以我們可以選取一個大數比如1234567890*1e8,可以將這個作為答案進行待定。
因為要求能夠整除,所以我們用這個大數(假定為n)n%x,令r=n%x,那麽易知r是小於1e8的,我們現在用n加上x-r那麽就可以同時滿足題目中的條件了。
這裏如果用減的話可能會因為借位而對1234567890進行改變,用加就不用擔心這個問題出現了。
感覺思路特別巧妙,主要還是對數據範圍的細心觀察。
代碼如下:
#include <bits/stdc++.h> typedef long long ll; ll n = 123456789000000000; int main(){ ll T,k,t,r; ll x; scanf("%lld",&T); while(T--){ scanf("%lld %lld",&x,&k); for(int i=1;i<=k;i++){ int tmp; scanf("%d",&tmp); } r = n % x; t = x - r; printf("%lld\n",n+t); } return 0; }
湖南大學第十四屆ACM程序設計新生杯 E.Easy Problem