POJ 1426:Find The Multiple
Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %I64d & %I64u |
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
题意是要找到input数的一个倍数,该倍数其十进制只含有0和1。
刚刚看完了广度优先搜索,A了几道题之后,做这个题卡住了,原因是不知道怎么用广度搜索表示十进制只有0 1的数。结果看了discuss才知道一开始用1,之后1导出10 11,10导出100 101,11导出110 111.这么一直下去就能有所有的数了。觉得嗯嗯,不错(。。。)
之后是自己建队列,结果莫名其妙地MLE,我也真是什么错误都见识过了,不知道怎么办,索性因为输入时大于等于1小于等于200的,得到结果打表吧。
打表代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <queue> #include <cstring> #pragma warning(disable:4996) using namespace std; long long temp; queue<long long> q; int main() { freopen("i.txt","r",stdin); freopen("o.txt","w",stdout); __int64 test,temp; while(1) { cin>>test; if(test==0) break; while(q.size())q.pop(); q.push(1); while(1) { temp=q.front(); q.pop(); if(temp%test==0) { cout<<"a["<<test<<"]="<<temp<<";"<<endl; break; } else { q.push(temp*10);//亮点在于所有0 1组成的十进制的数都可以这样广度搜索出来,这种想法很亮。 q.push(temp*10+1); } } } return 0; }
最终代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <queue> #include <cstring> #pragma warning(disable:4996) using namespace std; long long a[205]; int main() { a[1]=1; a[2]=10; a[3]=111; a[4]=1100; a[5]=100; a[6]=11010; a[7]=11011; a[8]=11000; a[9]=111111111; a[10]=1000000; a[11]=1000010; a[12]=1001100; a[13]=1010100; a[14]=1011010; a[15]=1100010; a[16]=1110000; a[17]=1110100; a[18]=10111111110; a[19]=100111; a[20]=101000; a[21]=101010; a[22]=110000; a[23]=110101; a[24]=111000; a[25]=111100; a[26]=11001000010; a[27]=11010111111; a[28]=11011000000; a[29]=11011000111; a[30]=11011001010; a[31]=11011001011; a[32]=11011100000; a[33]=11011100001; a[34]=11011101110; a[35]=11011110110; a[36]=110111111100; a[37]=111000000000; a[38]=111000010010; a[39]=111001000110; a[40]=111001001000; a[41]=111001010110; a[42]=111010011000; a[43]=111101111110; a[44]=111110000100; a[45]=111110011110; a[46]=1001111110; a[47]=1010001001; a[48]=1010010000; a[49]=1010010001; a[50]=1010010100; a[51]=1010111100; a[52]=1011101000; a[53]=1100000001; a[54]=11011111110; a[55]=11100000010; a[56]=11100110000; a[57]=11110001100; a[58]=11110100100; a[59]=11111100011; a[60]=11111111100; a[61]=1000101100; a[62]=1010011000; a[63]=1111011111; a[64]=1010000000000; a[65]=1010000000100; a[66]=1010000101110; a[67]=1010001101100; a[68]=1010001110000; a[69]=1010011100001; a[70]=1010011110010; a[71]=1010110000110; a[72]=1110111111000; a[73]=1111010000110; a[74]=1111010100000; a[75]=1111010111100; a[76]=1111011111000; a[77]=1111100000010; a[78]=1111101010110; a[79]=1111101101100; a[80]=1111101110000; a[81]=1111110001101; a[82]=1111110111010; a[83]=10010111001; a[84]=10011101100; a[85]=10011110110; a[86]=10100101010; a[87]=10100111010; a[88]=10110001000; a[89]=10111111110; a[90]=11011111110; a[91]=11011111111; a[92]=11101111000; a[93]=11111010111; a[94]=1000011010; a[95]=1000101100; a[96]=1001100000; a[97]=1001101110; a[98]=1010110010; a[99]=1101111111111111111; a[100]=1000000; a[101]=1000001; a[102]=1000110; a[103]=1110000000010110110; a[104]=1110000000011110000; a[105]=1110000000011110110; a[106]=1110000001010000100; a[107]=1110000001011001101; a[108]=1110000001011101100; a[109]=1110000001110010101; a[110]=1110000001110011000; a[111]=1110000001110011100; a[112]=1110000010000000000; a[113]=1110000010001000011; a[114]=1110000010001101110; a[115]=1110000010010110110; a[116]=1110000010011101100; a[117]=1110000010011110010; a[118]=1110000010101000110; a[119]=1110000010101100111; a[120]=1110000010101111000; a[121]=1110000010110100010; a[122]=1110000010111000110; a[123]=1110000010111001100; a[124]=1110000010111111100; a[125]=1110000011000000000; a[126]=1110000011000011110; a[127]=1110000011000101110; a[128]=1110000011010000000; a[129]=1110000011101001001; a[130]=1110000011101111000; a[131]=1110000011110111100; a[132]=1110000100000001100; a[133]=1110000100000111110; a[134]=1110000100111100000; a[135]=1110000100111100010; a[136]=1110000101001100000; a[137]=1110000101100000111; a[138]=1110000101110100100; a[139]=1110000101110100101; a[140]=1110000101110110000; a[141]=1110000110001100101; a[142]=1110000111000100100; a[143]=1110000111110000110; a[144]=1110000111110010000; a[145]=1110000111111011010; a[146]=1110001000000010110; a[147]=1110001000010110110; a[148]=1110001000010111100; a[149]=1110001000011010010; a[150]=1110001000011011100; a[151]=1110001000011100010; a[152]=1110001000101101000; a[153]=1110001000101101001; a[154]=1110001001000001110; a[155]=1110001001001001100; a[156]=1110001001011110000; a[157]=1110001010010000110; a[158]=1110001010011010100; a[159]=1110001010101010001; a[160]=1110001010101100000; a[161]=1110001011010110100; a[162]=1110001011011001000; a[163]=1110001011101010101; a[164]=1110001011101110000; a[165]=1110001011110101110; a[166]=1110001100000111000; a[167]=1110001100100001001; a[168]=1110001100101110000; a[169]=1110001101001001001; a[170]=1110001101001110000; a[171]=1110001101010000110; a[172]=1110001101111101000; a[173]=1110001101111110010; a[174]=1110001110011000100; a[175]=1110001110011010000; a[176]=1110001111000100000; a[177]=1110001111010110011; a[178]=1110010001011110100; a[179]=1110010010110111011; a[180]=1110010010111000100; a[181]=1110010011001101001; a[182]=1110010011010000100; a[183]=1110010011011010000; a[184]=1110010011011110000; a[185]=1110010011100001100; a[186]=1110010011111011100; a[187]=1110010100000110001; a[188]=1110010101001111000; a[189]=1110010110000101100; a[190]=1110010110000110100; a[191]=1110010110010010001; a[192]=1110010110111000000; a[193]=1110011000000010110; a[194]=1110011000001000010; a[195]=1110011000001111000; a[196]=1110011000010010000; a[197]=1101000101; a[198]=1111111111111111110; a[199]=111000011; a[200]=1000; long long test; while(1) { cin>>test; if(test==0) break; cout<<a[test]<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。