您的位置:首页 > 其它

BZOJ1754: [Usaco2005 qua]Bull Math

2014-10-17 18:20 323 查看

1754: [Usaco2005 qua]Bull Math

Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 374 Solved: 227
[Submit][Status]

Description

Bulls are so much better at math than the cows. They can multiply
huge integers together and get perfectly precise answers ... or so
they say. Farmer John wonders if their answers are correct. Help
him check the bulls' answers. Read in two positive integers (no
more than 40 digits each) and compute their product. Output it as
a normal number (with no extra leading zeros).

FJ asks that you do this yourself; don't use a special library
function for the multiplication.

输入两个数,输出其乘积

Input

* Lines 1..2: Each line contains a single decimal number.

Output

* Line 1: The exact product of the two input lines

Sample Input

11111111111111

1111111111

Sample Output

12345679011110987654321

HINT

Source

Gold

题解:

呵呵,金组出这种题,防爆零呢吧。。。

代码:

#include<cstdio>

#include<cstdlib>

#include<cmath>

#include<cstring>

#include<algorithm>

#include<iostream>

#include<vector>

#include<map>

#include<set>

#include<queue>

#include<string>

#define inf 1000000000

#define maxn 10000

#define maxm 500+100

#define eps 1e-10

#define ll long long

#define pa pair<int,int>

#define for0(i,n) for(int i=0;i<=(n);i++)

#define for1(i,n) for(int i=1;i<=(n);i++)

#define for2(i,x,y) for(int i=(x);i<=(y);i++)

#define for3(i,x,y) for(int i=(x);i>=(y);i--)

#define mod 1000000007

using namespace std;

inline int read()

{

int x=0,f=1;char ch=getchar();

while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}

return x*f;

}
int n,m,a[maxn],b[maxn],c[maxn];
char ch;

int main()

{

freopen("input.txt","r",stdin);

freopen("output.txt","w",stdout);

ch=getchar();
while(ch<='9'&&ch>='0')a[++n]=ch-'0',ch=getchar();
for1(i,n>>1)swap(a[i],a[n+1-i]);
ch=getchar();
while(ch<='9'&&ch>='0')b[++m]=ch-'0',ch=getchar();
for1(i,m>>1)swap(b[i],b[m+1-i]);
for1(i,n)
for1(j,m)
c[i+j-1]+=a[i]*b[j];
for1(i,n+m)c[i+1]+=c[i]/10,c[i]%=10;
int t=n+m;
while(!c[t])t--;
for3(i,t,1)printf("%d",c[i]);
printf("\n");

return 0;

}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: