您的位置:首页 > 其它

lightoj 1214 - Large Division 【判大数整除 拆分字符串 同余】

2015-11-04 22:10 405 查看
1214 - Large Division



PDF (English)StatisticsForum
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

 

PROBLEM SETTER: JANE ALAM JAN

题意:给你一个a (-10^100 <= a <= 10^100),又给你一个b(int范围),问你b能否整除a。

思路:用字符串表示a,然后依次拆分每一位,根据同余性质对b取余到最后一位,判断最后结果是否为0。

AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 500000+10
#define MAXM 50000000
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
using namespace std;
int main()
{
int t, kcase = 1;
Ri(t);
W(t)
{
LL mod; char str[200];
Rs(str); Rl(mod);
int s = 0;
int len = strlen(str);
if(str[0] == '-')
s++;
LL ans = 0;
if(mod < 0)
mod = -mod;
for(int i = s; i < len; i++)
ans = (ans * 10 + str[i]-'0') % mod;
if(ans == 0)
printf("Case %d: divisible\n", kcase++);
else
printf("Case %d: not divisible\n", kcase++);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: