D - Find The Multiple
阿新 • • 發佈:2020-11-03
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
Input
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input
2 6 19 0Sample Output
10 100100100100100100 111111111111111111
1.每個數的二進位制數 乘以 10,就等於他原來的十進位制樹乘以 2。
舉個例子 : 1二進位制1如果 1*10=10 那就是2的二進位制,10*10=100 那就是4的二進位制。
一個十進位制數除以2得到的數,他們在二進位制裡相差10倍。
2.同餘定理
(a+b)%n = (a%n +b%n)%n;
(a*b)%n = (a%n *b%n)%n;
列舉每一個數的二進位制數,看這個是不是n的倍數1 // 2 // Created by w on 2020/11/3. 3 // 4 5 #include <iostream> 6using namespace std; 7 int arr[1000000]; 8 int main() 9 { 10 int n; 11 while (cin>>n) 12 { 13 int ans[200]; 14 if(n==0) 15 return 0; 16 arr[1]=1; 17 int i=0; 18 for(i=2;arr[i-1]!=0;i++) 19 { 20 arr[i]=(arr[i/2]*10+i%2)%n; //一個個的列舉每個數的二進位制,是否滿足條件,感覺有點像堆排序 21 } 22 i--; 23 int k=0; //記錄下標,直到i==0 24 while (i) 25 { 26 int tmp=i%2;//縮減倍數 27 ans[k]=tmp; 28 k++; 29 i/=2; 30 } 31 for(int j=k-1;j>=0;j--) 32 cout<<ans[j];33 cout<<endl; 34 } 35 36 }