LightOJ1214 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.

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

题意:给出两个数a, b,问能否被b整除。

题解:基础数论。简单的同余定理应用,将a作为字串储存,相当于每x位(和b同位)模b一次,得到余数时相当于将这个区间改写成这个余数,移动区间继续运算。最终余数为零时代表可以被整除,非零则否。

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <math.h>
 6 #define ll long long
 7 using namespace  std;
 8
 9 char a[300];
10 int main()
11 {
12     int T, x, s;
13     ll t, b;
14     scanf("%d", &T);
15         for(int i=1; i<=T; i++)
16         {
17             scanf("%s %lld", a, &b);
18             x=strlen(a);
19             if(a[0]==‘-‘)//注意负数变正
20             {
21                 s=2;
22                 t=a[1]-‘0‘;
23             }
24             else
25             {
26                 s=1;
27                 t=a[0]-‘0‘;
28             }
29             t=t%abs(b);
30             for(int j=s; j<x; j++)
31             {
32                 t=(t*10+a[j]-‘0‘)%abs(b); //同余定理的应用
33             }
34
35
36             if(t==0)
37             {
38                 printf("Case %d: divisible\n", i);
39             }
40             else
41                 printf("Case %d: not divisible\n", i);
42
43         }
44
45 }
时间: 2024-11-05 14:00:17

LightOJ1214 Large Division 基础数论+同余定理的相关文章

数论 --- 同余定理

声明:以下文章是借鉴了别人的再加上自己补充后的,转载请注明! 一.同余 对于整数除以某个正整数的问题,如果只关心余数的情况,就产生同余的概念. 定义1 用给定的正整数m分别除整数a.b,如果所得的余数相等,则称a.b对模m同余,记作a≡b(mod m),如 56≡0 (mod 8). 举个例子: 3%2=1 5%2=1 则有: (5-3)%=0 [同余性质] 1) 自反性 2) 传递性 3) 对称性 4) 若 a ≡ b (mod m), c ≡ d (mod m) 则 a+b ≡ b+ d(m

LightOJ 1214 - Large Division (大数取余)

1214 - Large Division PDF (English) Statistics Forum Time Limit: 1 second(s) Memory Limit: 32 MB Given two integers, a and b, you should checkwhether a is divisible by b or not. We know that an integer ais divisible by an integer b if and only if the

Light oj 1214-Large Division (同余定理)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我就把A的每一位拆开,比如A是2341,那么2341=2000+300+40+1,然后你懂的... 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5

LightOJ - 1214 Large Division(同余)

Large Division Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status 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

1214 - Large Division -- LightOj(大数取余)

http://lightoj.com/volume_showproblem.php?problem=1214 这就是一道简单的大数取余. 还想还用到了同余定理: 所谓的同余,顾名思义,就是许多的数被一个数d去除,有相同的余数.d数学上的称谓为模.如a=6,b=1,d=5,则我们说a和b是模d同余的.因为他们都有相同的余数1. //// 数学上的记法为: a≡ b(mod d) 可以看出当n<d的时候,所有的n都对d同商,比如时钟上的小时数,都小于12,所以小时数都是模12的同商. 对于同余有三种

light oj 1214 - Large Division

1214 - Large Division   PDF (English) Statistics Forum 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

POJ 2769 Reduced ID Numbers 同余定理

Reduced ID Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8989 Accepted: 3610 Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an

ACM数论中相关定理(不断更新)

费马小定理是数论中的一个重要定理,其内容为: 假如p是质数,且(a,p)=1,那么 a^(p-1) ≡1(mod p).即:假如a是整数,p是质数,且a,p互质,那么a的(p-1)次方除以p的余数恒等于1. 费马大定理,又被称为“费马最后的定理”,由法国数学家费马提出.它断言当整数n >2时,关于x, y, z的方程 x^n + y^n = z^n 没有正整数解.被提出后,经历多人猜想辩证,历经三百多年的历史,最终在1995年被英国数学家安德鲁·怀尔斯证明. 中国剩余定理的结论: 令任意固定整数

[ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   Accepted: 3194 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of