Find The Multiple
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 18386 | Accepted: 7442 | 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
给这道题增加一些基础知识,有关数论的一些定理和推论,这篇博客很不错,推荐大家去看一下。
http://www.cnblogs.com/zhixingqiezhixing/archive/2012/04/03/2430676.html
另外小You的博客讲得特别细,不过我还是没看懂,倒是看了下面那张图之后清醒了许多。附上小You的博客链接,大家可以去膜拜一下,
http://blog.csdn.net/lyy289065406/article/details/6647917
poj1426
题意:
给出一个整数n,(1 <= n <= 200)。求出任意一个它的倍数m,要求m必须只由十进制的'0'或'1'组成。
思路:
就是用数组保存二叉树,根据同余模定理来解决数据太大的问题
首先我们利用 同余模定理 对得到余数的方式进行一个优化
(a*b)%n = (a%n *b%n)%n
(a+b)%n = (a%n +b%n)%n
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; int mod[600000]; int ans[600000]; int main() { int n; while(~scanf("%d",&n) && n) { memset(mod,0,sizeof(mod)); mod[1]=1%n; int i=2; while(mod[i-1]) { mod[i]=(mod[i/2]*10+i%2)%n; i++; } int p=0; i--; while(i) { ans[p++]=i%2; i/=2; } for(i=p-1;i>=0;i--)cout<<ans[i]; cout<<endl; } }
另外这道题也可以脑残的不顾一切的bfs 用long long 就可以,不过提交要用G++,C++的话WA或者RE。
#include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; queue<long long > q; int n; void bfs() { while(!q.empty())q.pop(); q.push(1); //最高位为1 long long t; while(!q.empty()) { t=q.front(); q.pop(); if(t%n==0) { cout<<t<<endl; return ; } q.push(t*10); q.push(t*10+1); } } int main() { int i,j; while(~scanf("%d",&n) && n) { bfs(); } }