how many ones?
时间限制:
3000 ms | 内存限制:
65535 KB
难度:
2
-
描述
-
Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1's. How many digits are in the smallest such a multiple of n?
-
输入
- Each line contains a number n. 输出
- Output the number of digits. 样例输入
-
3 7 9901
样例输出
-
3 6 12
来源
- Waterloo local 2001.06.02 上传者
-
思路:想说题意吧 英语不好是硬伤 T.T
Given any integer 0 <= n <= 10000 not divisible by 2 or 5
给你一个不能被2或者5整出 数n,some multiple of n is a number which in decimal notation is a sequence of 1's.
这个n变化多少次可以变为由1组成的十进制数
看懂了:就问你由这个数可被由多少个1组成的数整除
同余定理:如果两个数同余,那么对这两个数作任何相同运算,结果还是同余。
拿n=3来举例
我们要找到3的倍数m,也就是说m mod 3 == 0;也就是说不一定要求出m只要找到mod与m同余的数即可。
从 1开始 1 mod 3 == 1
要把1变成11就要把1乘以10加1 11 mod 3 == 1*10+1 mod 3 == 2
考虑111的情况 111 mod 3 == 2*10+1 mod 3 == 0
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int t=0;
int k;
for(int i=0;;i++)
{
t=(t*10+1)%n;
if(!t)
{
k=i+1;
break;
}
}
printf("%d\n",k);
}
}