HDU1212 Big Number 【同余定理】

Big Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4594    Accepted Submission(s): 3175

Problem 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 program to calculate A mod B.

To make the problem easier, I promise that B will be smaller than 100000.

Is it too hard? No, I work it out in 10 minutes, and my program contains less than 25 lines.

Input

The input contains several test cases. Each test case consists of two positive integers A and B. The length of A will not exceed 1000, and B will be smaller than 100000. Process to the end of file.

Output

For each test case, you have to ouput the result of A mod B.

Sample Input

2 3
12 7
152455856554521 3250

Sample Output

2
5
1521

用到同余定理:(a+b)%c=(a%c+b%c)%c=(a+b%c)%c;    附:(a*b)%c=(a%c*b%c)%c;

#include <stdio.h>
#define maxn 1002

char str[maxn];

int main()
{
    int m, ans, i;
    while(scanf("%s%d", str, &m) != EOF){
        ans = 0;
        for(i = 0; str[i]; ++i){
            ans = (ans * 10 + (str[i] - '0') % m) %m;
        }
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-07-28 16:46:41

HDU1212 Big Number 【同余定理】的相关文章

Codeforces 464C Substitutes in Number 同余定理+模拟

题目链接:点击打开链接 题意: 给定一串数字 下面有n个操作 每行格式形如 d->t d为一位数字,t为任意长度的数字. t的长度和不超过100000 问:最后的结果%1e9+7 思路: 首先我们可以得到一个结论: 同余定理使用后不能再修改数字. 那么为了让同余定理能够使用,我们倒序处理每个数字,这样就能保证能够使用同余定理. 记录每个数字实际代表的数字和实际对应的位数. 然后倒序处理上来即可. #include <stdio.h> #include <string.h> #

HDU 1212 Big Number(同余定理)

Big Number Problem 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 program to calculate A mod B. To make the problem easier, I promise that B will be smaller than 1000

hdu1212 Big Number &amp;第六届山东省赛Single Round Math (同余定理,大数取模)

题目链接:Big Number 题目大意:每次输入两个数,第一个是高精度,第二个数小于100000:求 a mod b 根据同余定理: (a+b)% c = (a%c+ b%c)%c (a*b)%c =  ( a%c* b%c)%c 所以 对于大数,例如 :123 可以这样分解 123 =  (1*10+2)*10 + 3: 123 % c =   (  (  (  1%c *  10%c)%c + 2%c) %c  * 10%c) + 3 %c  ) %c; 因此,我们用字符串处理这个数,通过

如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5930    Accepted Submission(s): 4146 Problem Description As we know, Big Number is always troublesome. But it's really important in our

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

POJ Multiple (BFS,同余定理)

http://poj.org/problem?id=1465 Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6164   Accepted: 1339 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM

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), denot

POJ--1465--Multiple【BFS+同余定理】

链接:http://poj.org/problem?id=1465 题意:给一个数字n,和m个数字,找一个由这些数字组成的最小的n的倍数,如果不存在输出0. 思路:这题怎么想都想不到bfs上去,看了别人的解题报告,其实是用bfs来枚举,但是加了一个牛逼的剪枝:同余.即如果A%X==B%X,则(A*10+K)%X==(B*10+K)%X. 我们枚举m中每一个数字做这个K,实际上是枚举了一个数,B*10是之前枚举的数字,如果这个数%X得到的值之前已经得到过,则没必要再往下计算,因为根据同余定理剩下的