LightOJ - 1078-Integer Divisibility(同余)

link:

https://vjudge.net/problem/LightOJ-1078

Meaning of the questions:

If an integer is not divisible by 2 or 5, some multiple of that number in decimal notation is a sequence of only a digit. Now you are given the number and the only allowable digit, you should report the number of digits of such multiple.

For example you have to find a multiple of 3 which contains only 1's. Then the result is 3 because is 111 (3-digit) divisible by 3. Similarly if you are finding some multiple of 7 which contains only 3's then, the result is 6, because 333333 is divisible by 7.

Ideas:

\((a*x+y)%b = (a%b*x%b+y%b)%b\)

Code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<math.h>
#include<vector>

using namespace std;
typedef long long LL;
const int INF = 1e9;

const int MAXN = 1e6+10;
const int MOD = 1e9+7;

int main()
{
    int t, cnt = 0;
    LL n, x;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%lld%lld", &n, &x);
        printf("Case %d: ", ++cnt);
        if (x%n == 0)
            puts("1");
        else
        {
            int cnt = 1;
            int tmp = x;
            while(x!=0)
            {
                x = (x*10+tmp)%n;
                cnt++;
            }
            printf("%d\n", cnt);
        }
    }

    return 0;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11817273.html