POJ2551 Ones

简单暴力会超时,稍微动脑筋优化一下即可

其实根据(a+b)%d=(a%d+b%d)%d就可以做出来了。

以7为例

1%7=1              1%7=1

11%7=4          (10%7+1)%7=4

111%7=6        (4*10%7+1)%7=6

1111%7=5      (6*10%7+1)%7=5

11111%7=2     (5*10%7+1)%7=2

111111%7=0   (2*10%7+1)%7=0

输出6.

 
Ones
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10892   Accepted: 6193

Description

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?

Input

Each line contains a number n.

Output

Output the number of digits.

Sample Input

3 
7 
9901

Sample Output

3
6
12

Source

 
 1 //oimonster
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<iostream>
 5 using namespace std;
 6 int main(){
 7     int i,j,n;
 8     bool f;
 9     int s,t=0;
10     while(scanf("%d",&n)!=EOF){
11         f=false;
12         t=s=1;
13         t=t%n;
14         while(t!=0){
15             t=(t*10%n+1)%n;
16             s++;
17         }
18         printf("%d
",s);
19     }
20     return 0;
21 }
View Code
原文地址:https://www.cnblogs.com/oimonster/p/4356109.html