1. 程式人生 > >ACM暑期集訓 同餘定理+逆元 大數取餘

ACM暑期集訓 同餘定理+逆元 大數取餘

表面上看這道題是問a能不能整除b,實際上還是看二者取余余數是否為0,屬於大數取餘的範圍

a的範圍達到10的200次方,用 long long都已經不可以,需要用字串,而b可以用long long

題目:

F - 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

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>
typedef long long ll;
int main()
{
	int t,k=0,i;
	scanf("%d",&t);
	while(t--)
	{
		char s[1000];
		k++;
		long long b;
		scanf("%s%lld",s,&b);
		int len=strlen(s);
		ll num=0;
		for(i=0;i<len;i++)
		{
			if(s[i]=='-')
			continue;
			num=(num*10+s[i]-'0')%b;//關鍵步驟
		}
		if(num==0) 
		printf("Case %d: divisible\n",k);
		else
		printf("Case %d: not divisible\n",k);
	}
	return 0;
 } 

相關推薦

ACM暑期集訓 定理+ 大數

表面上看這道題是問a能不能整除b,實際上還是看二者取余余數是否為0,屬於大數取餘的範圍 a的範圍達到10的200次方,用 long long都已經不可以,需要用字串,而b可以用long long 題目: F - Large Division Given two i

定理+】知識點講解

【同餘的定義】: 【同餘的主要性質】: 性質證明: 【逆元】 (1)定義: 【費馬小引理求解逆元】: 程式碼實現: long long quickpow(long long a,long long b){ if(b<0) retur

同餘 前置知識 ————擴充套件歐幾里得定理 什麼是同餘 對於兩個數a,b,它們對於p取模結果相同,那麼就稱a和b在對p取模意義下同餘 公式表達a≡b(mod)p 如何求一個數的同餘 利用擴充套件歐幾里得定理 我們將該公式轉化一下 -> a%p == b%p

組合數學+lucas定理+ BZOJ2111 [ZJOI2010]Perm 排列計數

can clas str void script space rip esc magic 2111: [ZJOI2010]Perm 排列計數 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2118 Solved: 563

hdu 3037 費馬小定理+求組合數+Lucas定理

void log 打表 數學 mod turn ret iostream toc 組合數學推推推最後,推得要求C(n+m,m)%p 其中n,m小於10^9,p小於1^5 用Lucas定理求(Lucas定理求nm較大時的組合數) 因為p數據較小可以直接階乘打表求逆元

hdu3037--Saving Beans --【lucas定理++插板法】

                                              Saving Beans                         Time Limit: 6000/3000 MS (Java/Others)    Memory Limit

費馬小定理+(模板)

For each case, output the case number and the desired value. Since the result can be very large, you have to print the result modulo1000003.

白兔的式子(費馬小定理+

題目描述 已知f[1][1]=1,f[i][j]=a*f[i-1][j]+b*f[i-1][j-1] (i>=2,1<=j<=i)。 對於其他情況f[i][j]=0 有T組

NEFU1493 快速冪+除法

題目: Gugu 有兩個長度無限長的序列A,B A0=a^0/0!,A1=a^1/1!,A2=a^2/2!,A3=a^3/3!…. B0=0, B1=b^1/1!,B2=0,B3=b^3/3!,B4=0, B5=b^5/5! … Douge

HDU 4305 Lightning (生成樹的計數+矩陣樹定理+

題意:給你n個點,如果兩個點的距離小於等於r那麼就連一條邊,讓你求生成樹的個數。 題解: 對於無向圖G,它的Kirchhoff矩陣C定義為它的度數矩陣D減去它的鄰接矩陣A。顯然,這樣的定義滿足剛才描述的性質。 有了Kirchhoff矩陣這個工具,我們可以引入Matri

大數——&除法

#include<stdio.h> #include<string.h> int main() { char s[1100]; int num[1100]={};

Big Number(大數

Description As we know, Big Number is always troublesome. But it’s really important in our ACM. And today, your task is to write a

hdu 1212(大數

題目大意:輸入兩個數a, b, 要求輸出a mod b(a的長度小於1000, b <= 100000)(a 是長度, b是大小) 分析:直接程式碼,模板!模板! ac程式碼 #incl

與乘法

同餘: 定義:設m≠0,若m∣a-b,即a-b=km,則稱a與b同餘,餘數為m。 充要條件:a、b關於模m同餘的充要條件是整數a和b被同一正整數m除時,有相同的餘數。(a % m)=(b % m)意

擴展歐幾裏得算法、裴蜀定理與乘法

關於 算法 需要 bsp 同時 們的 乘法 str mod 擴展歐幾裏得算法 擴展歐幾裏得算法(擴O)能在求gcd(a,b)的同時求出丟番圖方程ax+by=gcd(a, b)的解。 然而怎麽求呢?我們觀察gcd(a, b)=gcd(b, a%b),所以設如下兩個方程: ax

清華集訓 2014--奇數國(線段樹&歐拉函數&乘法&狀態壓縮)

-m 很多 class iostream div %d 正整數 ber 計劃 昨天想了一晚。。。早上AC了。。。 這麽長時間沒打線段樹,這回居然一次過了。。。 感覺數論方面應該已經沒有太大問題了。。。 之

HDU - 1576(費馬小定理

math src typedef pow ble inpu show font type 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Othe

拓展尤拉定理以及費馬小定理的板子

//拓歐 void exgcd(long long a,long long b,long long &x,long long &y) { if(b == 0) { x = 1; d = a; y = 0; return ; } else { lon

定理(歐幾里得演算法)

如果  (a-b)%m==0  那麼 a%m==0  b%m==0 a,b關於模m同餘。   求最大公約數 #include "pch.h" #include<iostream> #include<cstdio> #include<

hdu1163 Eddy's digital Roots(合九法+定理)

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 84156&nbs