E - Integer Divisibility

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.

Input
Input starts with an integer T (≤ 300), denoting the number of test cases.

Each case will contain two integers n (0 < n ≤ 106 and n will not be divisible by 2 or 5) and the allowable digit (1 ≤ digit ≤ 9).

Output
For each case, print the case number and the number of digits of such multiple. If several solutions are there; report the minimum one.

Sample Input
3
3 1
7 3
9901 1

Sample Output
Case 1: 3
Case 2: 6
Case 3: 12

翻译

如果整数不能被2或5整除,则十进制表示法中该数字的某个倍数只是一个数字的序列。现在您将获得数字和唯一允许的数字,您应该报告此数字的位数。
例如,您必须找到仅包含1的3的倍数。然后结果是3,因为111(3位)可被3整除。同样,如果你发现7的多数只包含3的那么,结果是6,因为333333可以被7整除。


输入:

输入以整数开始T(≤300),表示测试用例的数量。

每种情况将包含两个整数n(0 <n≤106且n不能被2或5整除)和允许的数字(1≤dig≤9)。

输出:

对于每种情况,打印案例编号和此类倍数的位数。如果有几种解决方案; 报告最小的一个。

样例输入:

3
3 1
7 3
9901 1

样例输出:

案例1:3
案例2:6
案例3:12

解决本题的方法:大数取模

一个大数对一个数取余,可以把大数看成各位数的权值与个位数乘积的和。

比如1234 = ((1 * 10 + 2) * 10 + 3) * 10 + 4,对这个数进行取余运算就是上面基本加和乘的应用。

代码实现:

char s[MAX];
int len = strlen(s);
int ans = 0;
for (int i = 0; i < len; i ++)
{
    ans = (ans * 10 + a[i] - '0') mod b;
}

 

这里只需要将循环条件、 a[i] - '0'  改一下(因为循环次数不清楚)

最初想法很简单循环算( n * 10 + n ),然后对m取余等于零停止,输出循环次数即可,但是Time Limit,后来想用快速幂依次算出n^1 n^2 n^3 ... 然后各项对m取余再累加求和的,但是Runtime Error,最后想起来大数求模,最终AC。。。。。

Time Limit代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main()
{
    ll i,t,m,n,flag,ans1,ans;
    scanf("%lld", &t);
    for(i=0; i < t; i++)
    {
        scanf("%lld%lld", &n, &m);
        flag = m;
        ans=0;
        ans1 = 0;
        while(m %= n)
        {
            ans1 = (ans1 * 10 + flag);
            m=ans1;
            ans++;
        }
        printf("Case %lld: %lld\n",i+1,ans);
    }
    return 0;
}

 Runtime Error代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int m,n;

ll quick_pow(ll a,ll b)
{
    ll ret = 1;
    while(b)
    {
        if(b & 1) ret = ret *a % m;
        a = (a * a) % m;
        b >>= 1;
    }
    return ret;

}

int main()
{
    ll i,t,flag,b,ans1,ans;
    scanf("%lld", &t);
    for(i=0; i < t; i++)
    {
        scanf("%lld%lld", &m, &n);
        ans = n;    b=1;
        while(ans)
        {
            ans = ((ans % m) + n * quick_pow(10,b)) % m;
            b++;
        }
        printf("Case %lld: %lld\n",i+1,b);
    }
    return 0;
}

最后附上AC代码。。。。。

AC代码:

自我感觉代码过于复杂,但思路还是挺明确的。。。。。。。好吧,可能是我自我感觉@(&^...^&)@!!!!!!

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/********本来是想用快速幂依次算出n^1 n^2 n^3 ... 然后各项对m取余再累加求和的,
                但是最后想起来大数求模******/
/*
ll quick_pow(ll a,ll b)
{
    ll ret = 1;
    while(b)
    {
        if(b & 1) ret = ret *a;
        a = (a * a);
        b >>= 1;
    }
    return ret;

}*/

int main()
{
    ll i,t,m,n,flag,b,ans1,ans;
    scanf("%lld", &t);
    for(i=0; i < t; i++)
    {
        scanf("%lld%lld", &m, &n);
        flag = n;
        ans = 0;
//注意ans1是从0开始的,因为如果ans1从1开始取的话ans1 != (ans1 * 10 +flag) % m;
//自己举个例子就明白了
        ans1 = 0;  b = 1;
//循环求最小的满足(n % m == 0)的ans
        while(b)
        {
            ans1 = (ans1 * 10 + flag) % m;
            b = ans1;
            ans++;

        }
        printf("Case %lld: %lld\n",i+1,ans);
    }
    return 0;
}

最后的最后来一碗鸡汤:

        我想说的是一定不要放弃,尽管你可能一题都没做出来,别人已经全AC了,但那又怎样,一定要自己先思考尝试解决,实在解决不了,搜搜别人博客,先看看别人的思路,自己再从别人的思路去考虑,尝试解题,还做不出来,再去借鉴别人的代码,注意:一定要弄懂别人的代码,下次出类似的题要能自己会做,自己会做才是关键!!!!!!

猜你喜欢

转载自blog.csdn.net/A_B_C_D_E______/article/details/81205687