杭电ACM 三 大数取模

题目及代码

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
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:mod.cpp
*作    者:冷基栋
*完成日期:2015年2月20日
*版 本 号:v1.0
*/
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char str[100000];
    int s,l,b,i;
    while(cin>>str>>s)
    {
        l=strlen(str);
        b=0;
        for(i=0;i<l;i++)
        b=(b*10+(str[i]-'0')%s)%s;
        cout<<b<<endl;
    }
    return 0;
}

运行结果:

知识点总结:

在做题之前,先了解这样一些结论:

A*B % C = (A%C * B%C)%C

(A+B)%C = (A%C + B%C)%C

如 532 mod 7 =(500%7+30%7+2%7)%7;

当然还有a*b mod c=(a mod c+b mod c)mod c;

如35 mod 3=((5%3)*(7%3))%3

有了这一些结论,题目就好做了!

学习心得:

好好学习 天天向上



时间: 2024-10-12 03:50:56

杭电ACM 三 大数取模的相关文章

杭电acm 1002 大数模板(一)

从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的,但是经过自己的使用与调试也明白了其中的内涵. 首先定义大数的结构体: struct BigNum{ static const int BASE=100000000; static const int WIDTH=8; vector<int> s; BigNum(long long num=0){

杭电ACM 二 数学求模

Font Size: ← → Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Ea

杭电ACM 三 圆桌会议倒序换座

Problem Description HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家的讨论后一般没有解决不了的问题,这也只有HDU ACM集训队特有的圆桌会议,有一天你也可以进来体会一下哦:),在一天在讨论的时候,Eddy想出了一个极为古怪的想法,如果他们在每一分钟内,一对相邻的两个ACM队员交换一下位子,那么要多少时间才能得到与原始状态相反的座位顺序呢?(即对于每个队员,原先在他左面的队员

杭电ACM 三 选地盖房防水淹(Math.ceil)

问题及代码: Problem Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned that the state of Louisiana is actually shrinking by 50 square miles each year, due to

杭电ACM 三 重力搭牌

问题及代码: Problem Description How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length. (We're assuming that the cards must be perpendicular to the table.) With two cards you c

杭电acm:大数阶乘(附源码)

Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file. Output For each N, output N! in one line. Sample Input 1 2 3 Sample Output 1 2 6 #include <stdio.h> int main() {

杭电ACM 三 动能定理 物理

问题及代码 Problem Description Once again, James Bond is fleeing from some evil people who want to see him dead. Fortunately, he has left a bungee rope on a nearby highway bridge which he can use to escape from his enemies. His plan is to attach one end o

杭电ACM 三 在坐标系中的微积分

问题及代码 Problem Description Ignatius bought a land last week, but he didn't know the area of the land because the land is enclosed by a parabola and a straight line. The picture below shows the area. Now given all the intersectant points shows in the p

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY