Poj2305-basic remains (binary conversion + large integer modulo)

Source: Internet
Author: User
Tags modulus

binary conversion + large integer modulus
One, test instructions:
in the B-system, ask for p%m, and then replace the B-input output.
where P is a B-binary large number of 1000 bits, M is a B-binary number within 9
Two, ideas:
1, enter p,m in the form of a string;
2, Conversion: string-integer decimal->b binary;
3, the decimal calculates and transforms the shaping result into the string form, and stores in reverse;
4, output.
Three, step:
1, input p[],m[];
2, string---shaping + binary->b binary:
I, the binary conversion statement: m2 = m2*b + m[j]-' 0 ';
II, large integer modulus, large integers can be written in this form:
12345 = (((((1 * 10+2) * 10+3) * 10+4) * 10+5
such as binary number (1100) = (((1 * 2+1) * 2+0) * 2+0) (2 indicates number of binary)
in order to avoid the large number of calculations, the conversion of the way to the mold
P2 = (p2*b + p[i]-' 0 ')% m2;
3, conversion and reverse storage: ans[k++] = p2%b + ' 0 '; P2/= b;
4, Reverse output

1#include <iostream>2#include <string.h>3 using namespacestd;4 Const intn=1010;//large number of digits5 6 intMain () {7     intb//binary number8      while(cin>>b&&b) {9         CharP[n], m[n], ans[n];Ten         intP2 =0, M2 =0;//defines the dividend, divisor, used to store decimal OneCin>>p>>m; A          for(intj=0; J<strlen (m); J + +) {//convert n binary string divisor to decimal number divisor -M2 = m2*b + m[j]-'0'; -         } the          for(intI=0; I<strlen (P); i++) {//converts the n-ary string dividend to a decimal number dividend -P2 = (p2*b + p[i]-'0')% m2;//in order to avoid the large number of calculations, the conversion of the way to the mold -         } -         if(!p2) +cout<<0<<Endl; -         Else{ +             intK =0; A              while(p2) { atans[k++] = p2%b +'0';//convert 10 decimal to n binary string reverse order and deposit in p[] -P2/=b; -             } -              for(inti = k1; I >=0; i--)  -cout<<Ans[i]; -cout<<Endl; in         } -     } to     return 0; +}
View Code

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Poj2305-basic remains (binary conversion + large integer modulo)

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.