【Lightoj】1214 - 能否整除(同余定理)
2016-01-13 13:38
330 查看
Time Limit:1000MS
Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
LightOJ 1214
Description
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
简单同余定理。
代码如下:
Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit
Status
Practice
LightOJ 1214
Description
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
简单同余定理。
代码如下:
#include <stdio.h> #include <string.h> int main() { int u; char t[222]; int a[222]; long int b; int l; //数字总位数 long int y; //余数 long int ty; //临时存放余数 int i,j; scanf ("%d",&u); getchar(); for (int p=1;p<=u;p++) { memset (a,0,sizeof (a)); memset (t,'0',sizeof (t)); scanf ("%s",t); scanf ("%ld",&b); printf ("Case %d: ",p); if (b<0) b=-b; l=strlen(t); if (t[0]=='-') { l--; for (i=1,j=l;i<=l;i++,j--) { a[i]=t[j]-48; } } else { for (i=1,j=l-1;i<=l;i++,j--) { a[i]=t[j]-48; } } y=a[1]%b; for (i=2;i<=l;i++) { if (a[i]==0) continue; ty=a[i]%b; for (j=2;j<=i;j++) { ty=(ty*10)%b; } y=(y+ty)%b; } if (y==0) { printf ("divisible\n"); } else { printf ("not divisible\n"); } } return 0; }
相关文章推荐
- Git Stash用法
- 并查集(union-find)学习报告
- 前端工程化-我们需要做什么
- ( Android ) eclipse查看真机数据库
- cocos2d-x中使用JNI的调用JAVA方法
- shell学习笔记
- The transaction log for database 'tempdb' is full due to 'ACTIVE_TRANSACTION'
- Java:字符乱码问题
- 多态的深层理解
- 从僵尸网络追踪到入侵检测 第9章 Nepenthesp 配置二
- Pow(x, n) leetcode
- php中获取各种路径
- Nginx+FastCGI运行原理
- JEasyframe使用教程3 页面相关
- 极验验证码流程
- 一些有意思的博客链接,不定时更新
- 如何查看openfire的错误日志
- ES6介绍
- structs 2 2013 S2-019漏洞修复方法
- 快速掌握Lua 5.3 —— 各种运算符以及各种声明