Large Division 同餘定理

題意:一個非常大的數a是否能整除b

別人都說是同餘,我上網看定理沒研究出來啥,可能是推出來的結論,記住就好

ans=(ans*10+a[i]-'0')%b,i從0-n-1,最後結果爲0就是能整除

注意:b用long long 不然會炸,不知道爲什麼,希望大佬看的時候評論告訴我;

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;

int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
        char a[305];
        ll b,ans=0;
        scanf("%s%lld",a,&b);
        int n=strlen(a);
        b=abs(b);
        for(int i=0;i<n;i++)
        {
            if(a[i]=='-') continue;
            ans=(ans*10+a[i]-'0')%b;
        }
        printf("Case %d: ",cas++);
        if(ans) printf("not divisible\n");
        else printf("divisible\n");
    }
}

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
發佈了45 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章