poj 2551 Ones(同余)

来源:互联网 发布:郑州冰川网络 编辑:程序博客网 时间:2024/06/02 21:35

Ones
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11459 Accepted: 6487

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

3612
题意:求n的一个倍数都由1组成,并且输出最小的这个倍数的位数

思路:利用同余定理,边加1边取模即可,第一个模为0即是答案

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <queue>using namespace std;int main(){    int n;    while(~scanf("%d",&n))    {        int sum=0;        for(int i=1;i<=n;i++)        {            sum=(sum*10+1)%n;            if(!sum)            {                printf("%d\n",i);                break;            }        }    }    return 0;}




0 0
原创粉丝点击