How to use congruence theorem to find remainder "Hdoj 1212 big number" for remainder ""

Source: Internet
Author: User

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

The problem easier, I promise that B'll be smaller than 100000.

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

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

Outputfor Each test case, you have to ouput the result of A mod B.

Sample Input2 312 7 152455856554521 3250

Sample Output2 5 1521 This topic needs to use the same remainder theorem: The common congruence theorem has the following three kinds: (1) ((a mod m) * (B mod m)) mod m = = (a*b) mod m (2) ((a mod m) + (B mod m)) m   od m = = (a+b) mod m (3) ((A mod m)-(B mod m) + m) mod m = = (a) mod m in subtraction, since A mod n may be less than B mod n, you need to add N to the result. Let us first understand how to use the congruence theorem to find the remainder:
/* For example 10000 to M redundancy: * 10000%m* = = (10%m*1000%m)%m* = = (10%m* (10%m*100%m)%m)%m  * = = (10%m* (10%m* (10%m*10%m)%m)%m)%m* The code is: * Assuming that 10000 is the length of the string is Len *//** such as 123 to M for redundancy * 123%m * = ((12%m*10%m)%m+3%m)%m * = = (((10%m+2%m)%m*10%m)%m+3%m)%m * = (((1% m*10%m)%m+2%m)%m*10%m)%m+3%m)%m */gets (str); int ans=0;for (i=0;i<len;i++) {ans=ans*10+str[i];ans=ans%m;}

For detailed modulo operations please refer to: http://blog.csdn.net/chocolate_22/article/details/6458029

This problem has been used in (1) (2) Two:
#include <stdio.h> #include <string.h> #define MAX 1100int Main () {int N,m,j,i,s,t;char p[max];while (scanf (" %s ", p)!=eof) {scanf ("%d ", &n), int l=strlen (p); s=0;for (i=0;i<l;i++) {s=s*10+p[i]-' 0 '; s=s%n;} printf ("%d\n", s);} return 0;}

How to use congruence theorem to find remainder "Hdoj 1212 big number" for remainder ""

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.