数论(大数取模) - Large Division LightOJ - 1214

数论(大数取模) - Large Division LightOJ - 1214

题意:

a b a b 给定两个整数a和b,你应该检查a是否可以被b整除。

c 使 a = b × c a b 我们知道,当且仅当存在整数c使得a = b ×c时,整数a才能被整数b整除。

Input

开始会输入一个数字 T (≤ 525), 代表了样例数.

每个样例会给两个整数a (-10200 ≤ a ≤ 10200) and b (|b| > 0, b fits into a 32 bit signed integer). 数字不会包含前导零.

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

T i m e   l i m i t 1000 m s M e m o r y   l i m i t 32768 k B Time\ limit:1000 ms,Memory \ limit:32768 kB


分析:

a ( 200 ) b i n t a b 由题意,a是一个大整数(200位以内),b是一个int范围内的整数,要判断a是否能够被b整除。

a b < = > a % b = 0 a能被b整除\qquad<=>\qquad a\%b=0。

a 根据模运算的性质,我们可以边计算a,边取模。

注意:

a 当a为负数时,过滤掉负号。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>

#define ll long long

using namespace std;

const int N=210;

char a[N];
int b,len;

bool check()
{
    ll tmp=0;
    for(int i=0,j;i<len;i++)
    {
        if(a[i]=='-') continue;
        tmp=tmp*10+a[i]-'0';
        if(tmp>=b) tmp%=b;
    }
    return tmp==0;
}

int main()
{
    int T; cin>>T;
    for(int t=1;t<=T;t++)
    {
        scanf("%s",a);
        scanf("%d",&b);
        len=strlen(a);

        printf("Case %d: ",t);
        if(check()) puts("divisible");
        else puts("not divisible");
    }
}

猜你喜欢

转载自blog.csdn.net/njuptACMcxk/article/details/107771526
今日推荐