2006 Zhejiang University Mars a+b

Source: Internet
Author: User

Title Description:
read two non-25-bit Mars positive integers a and B, calculate a+b. It is important to note that on Mars, integers are not single-binary, and nth-bit is the nth prime. For example: 10 binary number 2 on Earth, on Mars as "1,0", because the number of Mars single-digit is 2 binary, the Earth's 10 number of 38, on Mars as "1,1,1,0", because the Mars single-digit is 2 binary, 10-digit is 3, the Hundred is 5 binary, thousand is 7 binary ...
Input:
The
test input contains several test cases, one row for each test case, two Martian positive integers a and B, a comma-separated number of adjacent two-digit numbers for the Mars Integer, and a space interval between a and B. When a or B is 0 o'clock input end, the corresponding result does not output.
Output:
output 1 lines for each test case, that is, the a+b value of the Mars notation.
Sample input:
1,0 2,14,2,0 1,2,01 10,6,4,2,10 0
Sample output:
1,0,11,1,1,01,0,0,0,0,0

#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intmaxn=1005;BOOLISPRIME[MAXN];intprime[maxn],cnt;voidsieve () {memset (IsPrime,true,sizeof(IsPrime));  for(intI=2; i<= +; i++)    {        if(Isprime[i]) {prime[cnt++]=i;  for(intj=i+i;j<= +; j+=i) isprime[j]=false; }    }}CharSA[MAXN];CharSB[MAXN];structbigint{intE[MAXN]; intLen; BigInt () {memset (E,0,sizeof(e)); Len=0; } BigInt (Const Chars[]) {memset (E,0,sizeof(e)); Len=0; intL=strlen (s); intz=0;  for(intI=0; i<l;i++)        {            if(s[i]==',') {E[len++]=Z; Z=0; }            Else{z*=Ten; Z=z+ (s[i]-'0'); }} E[len++]=Z;  for(intI=0; i<len/2; i++)        {            intt=E[i]; E[i]=e[len-i-1]; E[len-i-1]=T; }} BigIntoperator+(ConstBigInt &t)Const{BigInt res; Res.len=Max (Len,t.len); intup=0; inti;  for(i=0; i<res.len;i++)        {            intz=e[i]+t.e[i]+Up ; Res.e[i]=z%Prime[i]; up=z/Prime[i]; }         while(up!=0) {Res.e[res.len++]=up%Prime[i]; up/=Prime[i]; I++; }        returnRes; }    voidprint () { for(inti=len-1; i>=1; i--) {printf ("%d,", E[i]); } printf ("%d\n", e[0]); }};intMain () {sieve ();  while(SCANF ("%s%s", SA,SB)! =EOF)        {BigInt a (SA);        BigInt b (SB); if((a.len==1&&a.e[0]==0)|| (b.len==1&&b.e[0]==0))             Break;        BigInt Res; Res=a+b;    Res.print (); }    return 0;}/************************************************************** problem:1016 User:banehunter language:c++ result:accepted time:10 Ms memory:1028 kb****************************************************************/

2006 Zhejiang University mars a+b

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.