lightoj 1214 - Large Division 【判大数整除 拆分字符串 同余】
2015-11-04 22:10
405 查看
1214 - Large Division
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.
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.
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;
}
PDF (English) | Statistics | Forum |
Time Limit: 1 second(s) | Memory Limit: 32 MB |
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;
}
相关文章推荐
- iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)
- 字符串之数字子串求和
- 数据库查询的基础技巧
- Linux命令——unalias
- 强联通分量-Gabow
- ASP.NET WebAPI 09 Controller的激活
- Activity优化几个结束的方法
- having——至少被订购过两回的订单
- READING NOTE: Learning to Segment Moving Objects in Videos
- set、env、export――Linux中的环境变量命令
- 转载
- 【五校联考】集体照
- 在伪分布式环境下通过宿主机上的eclipse运行WordCount小程序出现的问题小结
- hdoj 1087Super Jumping! Jumping! Jumping!【dp】
- Linux下/proc目录简介
- java基础之冒泡排序
- hdoj--1824--Let's go home(2-sat)
- java中常用的字符串的截取方法
- String str=newString("abc")创建了多少个对象
- poj2418