light oj 1078 - Integer Divisibility

本文介绍了一个关于寻找特定整除数的问题,并通过使用同余定理提供了一种有效的解决方案。问题要求找到最小数量的指定数字串,使得该数字串能被给定的整数整除。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://lightoj.com/volume_showproblem.php?problem=1078


1078 - Integer Divisibility
Time Limit: 2 second(s)Memory Limit: 32 MB

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

Output for Sample Input

3

3 1

7 3

9901 1

Case 1: 3

Case 2: 6

Case 3: 12

 


PROBLEM SETTER: JANE ALAM JAN

题目大意:给你两个整数n、s,让你求最少个s (如:ss、sss等)能够整除n


解析:刚开始用搜索,一直LTE,后来听一位大牛说用同余定理,然后试着就AC了,,,,


同余定理:  所谓的同余,顾名思义,就是许多的数被一个数d去除,有相同的余数。d数学上的称谓为模。

                      如a=6,b=1,d=5,则我们说a和b是模d同余的。因为他们都有相同的余数1。

水题,不多说了,上代码:


第一次用DFS错误的代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 10000007;

unsigned long long n, k;
int f, ans;
void dfs(unsigned long long x)
{
    if(f) return ;
    if(x % n == 0)
    {
        f = 1;
        return ;
    }
    ans++;
    dfs(x * 10 + k);
}
int main()
{
    int t, cnt = 0;
    cin >> t;
    while(t--)
    {
        unsigned long long kk;
        f = ans = 0; ans++;
        cin >> n >> k;
        kk = k;
        dfs(kk);
        printf("Case %d: ", ++cnt);
        cout << ans << endl;
    }
    return 0;
}



第二次用同余定理AC的代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 10000007;

int main()
{
    int t, cnt = 0, n, k;
    cin >> t;
    while(t--)
    {
        scanf("%d%d", &n, &k);
        int ans = 1, kk = k;
        while(kk % n != 0)
        {
            kk = (kk % n) * 10 + k;
            ans++;
        }
        printf("Case %d: %d\n", ++cnt, ans);
    }
    return 0;
}


 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值