大数取模 模板

1 scanf("%s%d",a,&b);
2 int len=strlen(a);
3 int ans=0;
4 for(int i=st;i<len;i++)
5     ans=(int)(((long long)ans*10+a[i]-‘0‘)%b);
6 printf("%d\n",ans);

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 #define maxn 10000000
 8 int t,b;
 9 char a[205];
10 int main()
11 {
12 scanf("%d",&t);
13 int cnt=0;
14 while(t--)
15 {
16     scanf("%s%d",a,&b);
17     int len=strlen(a);
18     int ans=0;
19     int st=0;
20     printf("Case %d: ",++cnt);
21     if(a[0]==‘-‘)
22         st=1;
23     for(int i=st;i<len;i++)
24     ans=(int)(((long long)ans*10+a[i]-‘0‘)%b);
25     if(ans==0)
26         printf("divisible\n");
27         else
28             printf("not divisible\n");
29
30 }
31
32  return 0;
33 }
34
35 //6
36 //
37 //101 101
38 //
39 //0 67
40 //
41 //-101 101
42 //
43 //7678123668327637674887634 101
44 //
45 //11010000000000000000 256
46 //
47 //-202202202202000202202202 -101
时间: 2024-10-16 15:43:31

大数取模 模板的相关文章

快速幂+大数取模

快速幂+大数取模 快速幂,其实就是求(a^b)% p,(其中a,b,p都比较大在int范围内)这类问题. 首先要知道取余的公式:(a*b)%p=(a%p*b%p)%p. 那么幂不就是乘机的累积吗,由此给出代码: int fast(int a,int b,int p) {   long long a1=a,t=1; while(b>0) { if(b&1)          /如果幂b是奇数多乘一次,因为后边会除2变偶数,(7/2=3) t=(t%p)*(a1%p)%p; a1=(a1%p)*

csu 1556: Jerry&#39;s trouble(大数取模)

题意:求出1^m+2^m+...n^m 思路:直接套用模板 #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<algorithm> #include<queue> #include<stack> #include<ctype.h> #define LL l

大数取模

//大数取模 #include "cmath" #include "iostream" #include "string.h" using namespace std; int mod(char str[],int num) { int number[100]; for(int i=0;i<strlen(str);i++) number[i]=str[i]-'0'; int remainder=0; for(int i=0;i<str

hdu2302(枚举,大数取模)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2303 题意:给出两个数k, l(4<= k <= 1e100, 2<=l<=1e6):其中k是两个素数的乘积,问k是否存在严格小于l的因子,若有,输出 BAD 该因子,反之输出GOOD: 思路: 先1e6内素数打表,再枚举一个因子,判断因子用大数取模: 代码: 1 #include <iostream> 2 #include <stdio.h> 3 #inclu

POJ 1426 Find The Multiple(大数取模)【DFS】||【BFS】

<题目链接> 题目大意: 给一个小于200的正整数n,问只有0和1组成的位数小于100的最小能被n整除的数是多少. 解题分析: 用DFS或者BFS沿着位数进行搜索,每一次搜索到下一位都有两种情况,0或者1,还要注意的是,大数取模的方法.但是我有一个疑问,这题位数最高为100,用dfs为什么不会超时?还是说只是此题数据太弱吗? BFS解法 #include<iostream> #include<cstdio> #include<queue> #include&

poj2635--The Embarrassed Cryptographer(数论篇1,大数取模)

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

机试真题 大数取模运算

之前总结过,大数问题,取模就是取商取余数: #include<iostream> #include<stdlib.h> #include<string> using namespace std; string devide(int& r, string s, int n) { string ss = ""; for (int i = 0; i < s.size(); i++) { int temp = r*10 + int(s[i] -

hdu 3037 Saving Beans 组合数取模模板题。。

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2707    Accepted Submission(s): 1014 Problem Description Although winter is far away, squirrels have to work day and night to save b

UVA128 - Software CRC(大数取模)

题目链接 题目大意:给你一个字符串,这个字符串代表的是256进制的一个数,要求在这个字符串后面再加上两个两个字节,使得这个新的字符串的值取模于34943等于0.最后要求你输出这两个字节(十六进制). 解题思路:256进制的数先转化成十进制的数m,然后求出还需要的数n使得(n + m)%34943 ==0 ,最后再将n转换成16进制的数输出就可以了. 代码: #include <cstdio> #include <cstring> typedef long long ll; cons