Time Limit: 1 second(s) | Memory Limit: 32 MB |
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 | Output for Sample Input |
6 101 101 0 67 -101 101 7678123668327637674887634 101 11010000000000000000 256 -202202202202000202202202 -101 | Case 1: divisible Case 2: divisible Case 3: divisible Case 4: not divisible Case 5: divisible Case 6: divisible
|
题意:让你找出一个大数能否被另一个数整除
思路:模拟除法运算过程,比如114除以3,我们先用11%3== 2,再用(2*10+4)%3==0,取余为0时,说明能够整除。还有同余定理的另一种写法,放在最后。由于第一个数范围很大,所以我们用字符串读入,第二个数的读入,我以为是用int,wrong了一次,去oj上的讨论版看数据,结果发现有一个数据不能过,就是下面给出这个,改为onglong才是正解,因为这个数据非常特殊。是2^31-1,刚好是题目边界数据,这时候我们*10就超int范围,虽然都是同余定理的应用,但是感觉这道题poj上1426好理解很多
1
10737418235 2147483647
方法1
#include<stdio.h> #define N 2100 char s1[N]; long long sum,d; int main() { int t,i,t2=0,flag; char *p; scanf("%d",&t);getchar(); while(t --) { i = flag = 0; scanf("%s %lld",s1,&d); p = s1; if(*p =='-') *p ++;//puts(p); sum = p[i]-'0'; while(p[i]!='\0') { if(sum %d == 0&&p[i+1]=='\0') { flag = 1; break; } sum = (sum%d)*10 + p[++i]-'0'; } printf("Case %d: ",++t2); if(flag) printf("divisible\n"); else printf("not divisible\n"); } return 0; }
方法2
#include<stdio.h> #include<string.h> #define N 2100 char s1[N]; long long sum,d; int main() { int t,i,t2=0; char *p; scanf("%d",&t);getchar(); while(t --) { i = 0; scanf("%s %lld",s1,&d); p = s1; if(*p =='-') *p ++; sum = 0; while(i < strlen(p)) { sum = (sum*10 + p[i++]-'0')%d; } printf("Case %d: ",++t2); if(!sum) printf("divisible\n"); else printf("not divisible\n"); } return 0; }