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
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使用字符数组保存;
一:先介绍下同余定理:
给定一个正整数m,如果两个整数a和b满足(a-b)能够
被m整除,即(a-b)/m得到一个整数,那么就称整
数a与b对模m同余,记作a≡b(mod m)。
举个例子:如果两个数a和b之差能被m整除,那么我们就
说a和b对模数m同余(关于m同余)。比如,100-60除以8正
好除尽,我们就说100和60对于模数8同余。它的另一层含义
就是说,100和60除以8的余数相同。a和b对m同余,我们记
作a≡b(mod m)。
二 :主要性质:
数学表述:
(a+b)%c=(a%c+b%c)%c
(a*b)%c=(a%c*b%c)%c
性质证明(加法)
a = k1*m+r1
b = k2*m+r2
(a+b)%m=(( k1*m+r1 )+( k2*m+r2 ))%m
= (( k1+k2 )*m+( r1+r2 ))% m
= (r1+r2 )%m
= (a%m+b%m)% m
(a+b)%m = (a%m+b%m)%m
三:大数取模
一个大数对一个数取余,可以把大数看成各位数的权值与个
位数乘积的和。
比如1234 = ((1 * 10 + 2) * 10 + 3) * 10 + 4,对这个数进
行取余运算就是上面基本加和乘的应用。
代码实现:
int len = a. length ();
int ans = 0;
for (int i = 0; i < len; i ++){
ans = (ans * 10 + a[i] - '0') mod b;
}
李
AC代码:
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
int t,h=0;
cin>>t;
while(t--)
{
h++;//记录是第几组样例
int k=0,b;
long long sum=0;//sum要开long long
char a[300];
cin>>a>>b;
b=abs(b);
int I=strlen(a);
for(int i=0;i<I;i++)
{
if(a[i]=='-')
continue;
sum=(sum*10+a[i]-'0')%b;
}
cout<<"Case "<<h<<": ";
if(sum==0)
cout<<"divisible"<<endl;
else
cout<<"not divisible"<<endl;
}
return 0;
}