NEFU 2016省赛演练一 F题 (高精度加法)

时间:2023-02-07 14:10:36

Function1

Problem:F

Time Limit:1000ms

Memory Limit:65535K

Description

You know that huicpc0838 has been reviewing his textbooks and doing related exercises for the coming PG exams these days. One day, when he was abused by the sixth chapter of the textbook  Computer Organization Principles, he came up with an idea. He wrote down something in his draft and happily went lunch (at 11:00 am).
Here is what he wrote: int function(int a,int b){
int c=(a&b),d=(a^b);
return c==0? d:function1(c<<1,d);
}
This function will terminated finally without doubt.
I will test it this code tonight after I back to dorm. Do you want to find what's in his mind? Given a,b, can you output the results?

Input

The input will end with EOF, consisting of several blocks. Every block is two lines, every line represents a non-negative integer, whose length is no more that 3000.

Output

Output function1(a,b)%2011.

Sample Input

0
1
31415926535897932384626
2718281828

Sample Output

1
507

题解1:将公式转换一下发现就是求A+B对2011取余,高精度加法模板题。
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int i,j,k=,n,m,t,x;
long long flag;
char c[],d[],ans[];
while(cin>>c>>d)
{
n=strlen(c);
m=strlen(d);
if (n>m)
{
x=n-m;
for (i=n-;i>=x;i--)
d[i]=d[i-x];
for (i=;i<x;i++)
d[i]='';
}
else
{
x=m-n;
n=m;
for (i=m-;i>=x;i--)
c[i]=c[i-x];
for (i=;i<x;i++)
c[i]='';
}
int jinwei=;
for (i=n-;i>=;i--)
{
ans[i]=((c[i]-'')+(d[i]-'')+jinwei)%+'';
if (c[i]+d[i]-''-''+jinwei>=)
jinwei=;
else
jinwei=;
}
ans[n]='\0';
if(jinwei)
flag=;
else
flag=;
for(i=;i<n;i++)
flag=(flag*+ans[i]-''+)%;
cout<<flag<<endl;
}
return ;
}

题解2:先取余再加,最优解。

#include <iostream>
#include <string.h>
using namespace std;
#define mod 2011
typedef long long ll;
int main()
{
int i;
ll a,b;
char c[],d[];
while(cin>>c>>d)
{
int len1=strlen(c);
int len2=strlen(d);
a=c[]-'';
b=d[]-'';
for(i=;i<len1;i++)
a=(a*+c[i]-'')%mod;
for(i=;i<len2;i++)
b=(b*+d[i]-'')%mod;
cout<<(a+b)%mod<<endl;
}
return ;
}