Find The Multiple(poj 1426)
阿新 • • 發佈:2019-01-30
Find The Multiple
Time Limit: 1000MS | Memory Limit: 10000K | ||
Total Submissions: 33035 | Accepted: 13823 | Special Judge |
Description
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 0
Sample Output
10 100100100100100100 111111111111111111
Source
這道題,好多人都是拿long long 就給過了,但是如果資料再大一點,用 long long 就過不了了。
首先我們得明白 每個數的二進位制數 乘以 10;就等於他原來的十進位制樹乘以 2.
舉個例子 : 1 二進位制 1 如果 1 乘以 10等於10 那就是2的二進位制,10*10=100 那就是4的二進位制。那麼一個十進位制數除以2得到的數,他們在二進位制裡相差10倍。
還得明白 同餘模定理
(a*b)%n = (a%n *b%n)%n;
(a+b)%n = (a%n +b%n)%n;
這些都懂了之後,這道題,就比較好理解了,我們主要就是列舉每一個數的二進位制數,看看如果這是個十進位制的,能不能是n的倍數。如果單純的去求這樣的二進位制肯定會超出int 的範圍。那麼就需要同餘模定理了。
#include<stdio.h>
int mod_er[1000000]; //這裡必須開得足夠大,才能滿足能夠列舉到這樣的sh
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int ans[200];
if(n==0)
return 0;
mod_er[1]=1;
int i;
for( i=2;mod_er[i-1]!=0;i++)
{
mod_er[i]=(mod_er[i/2]*10+i%2)%n; //一個個的列舉每個數的二進位制,是否滿足條件。
}
i--;
int k=0;
while(i)
{
int temp=i%2;
ans[k]=temp;
k++;
i=i/2;
}
for(int j=k-1;j>=0;j--)
{
printf("%d",ans[j]);
}
printf("\n");
}
}