LightOJ 1078 Integer Divisibility (同余定理)

针对一个给定且不能被2或5整除的数字n,寻找由另一个指定数字构成、能被n整除的最短数字序列。通过迭代计算找到满足条件的最小长度。

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

Integer Divisibility(答题)

Time Limit: 2000MS

Memory Limit: 32768KB

64bit IO Format: %lld & %llu

Submit Status uDebug

Description

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

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

Input

Input starts with aninteger T (≤ 300), denotingthe number of test cases.

Each case will containtwo 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, printthe case number and the number of digits of such multiple. If several solutionsare there; report the minimum one.

Sample Input

3

31

73

99011

Sample Output

Case1: 3

Case2: 6

Case3: 12

Source

ProblemSetter: Jane Alam Jan

 



题意:

给定一个不能够被 2 和 5 整除的数字 n ,再给定一个数字 dight 问至少要有几个后者组成的数字能够被前者整除。

例如,测试数据:

3 1>>3;

111%3=0;


思路:

之前在宇神博客看过这个题目,只是扫一眼而已。

没想到同余定理,也是够了。做题少,总结不够。


代码:

#include<stdio.h>

int main() {
	int t;
	scanf("%d",&t);
	int v=1; 
	while(t--) {
		int n,dight;
		scanf("%d%d",&n,&dight);
		int pro=dight%n;//记录累乘之后的余数
		int ans=1;//记录答案
		while(pro) {
			pro=dight+pro*10;//这个公式竟然一次没写出来
			pro%=n;
			ans++;
		}
		printf("Case %d: %d\n",v++,ans);
	}
	return 0;
}


#include<stdio.h>

 *******************************************************************************************************************************************************************



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值