Large Division LightOJ - 1214

Given two integers, a and b, you should check whether a is divisible by b or not. We know that an integer a is divisible by an integer b if and only if there exists an integer c such that a = b * c.

Input

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

Each case starts with a line containing two integers a (-10200 ≤ a ≤ 10200) and b (|b| > 0, b fits into a 32 bit signed integer). Numbers will not contain leading zeroes.

Output

For each case, print the case number first. Then print 'divisible' if a is divisible by b. Otherwise print 'not divisible'.

Sample Input

6

101 101

0 67

-101 101

扫描二维码关注公众号,回复: 5395622 查看本文章

7678123668327637674887634 101

11010000000000000000 256

-202202202202000202202202 -101

Sample Output

Case 1: divisible

Case 2: divisible

Case 3: divisible

Case 4: not divisible

Case 5: divisible

Case 6: divisible

题意:

给你俩个数 a , b .如果 b 可以被 a 整除就输出 divisible , 否则输出 not divisible

这里用到大数取余 和 同于定理的应用

一开始有在纠结 , 为什么要sum = sum * 10 + s[i] 然后 sum = sum % b。 这样不会改变答案吗

但是例如:

157 % 7 = ((1 * 10 + 5 )*10 + 7 )% 7 ,这样就可以看出来同于定理的应用啦~

下面代码:

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

int main()
{
	int t;
	ll b , sum;
	char s[200];
	cin >> t;
	for(int i = 1 ; i <= t ; i++)
	{
		sum = 0;
		scanf("%s %lld" , &s , &b);
		int len = strlen(s);
		if(b < 0) b = -b;
		printf("Case %d: " , i);
		if(len == 1 && s[0] == '0')
		{
			printf("divisible\n");
			continue;
		}
		for(int j = 0 ; j < len ; j++)
		{
			if(s[j] == '-')
			continue;
			sum = sum * 10 + (s[j]-'0');
			sum = sum % b;
		}
		if(sum == 0)
		{
			printf("divisible\n");
		}
		else
		{
			printf("not divisible\n");
		}
	 } 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41593380/article/details/84541845
今日推荐