首页 > 代码库 > BZOJ1754: [Usaco2005 qua]Bull Math

BZOJ1754: [Usaco2005 qua]Bull Math

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 multiplyhuge integers together and get perfectly precise answers ... or sothey say. Farmer John wonders if their answers are correct. Helphim check the bulls‘ answers. Read in two positive integers (nomore than 40 digits each) and compute their product. Output it asa normal number (with no extra leading zeros).FJ asks that you do this yourself; don‘t use a special libraryfunction 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

题解:

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

代码:

 1 #include<cstdio> 2  3 #include<cstdlib> 4  5 #include<cmath> 6  7 #include<cstring> 8  9 #include<algorithm>10 11 #include<iostream>12 13 #include<vector>14 15 #include<map>16 17 #include<set>18 19 #include<queue>20 21 #include<string>22 23 #define inf 100000000024 25 #define maxn 1000026 27 #define maxm 500+10028 29 #define eps 1e-1030 31 #define ll long long32 33 #define pa pair<int,int>34 35 #define for0(i,n) for(int i=0;i<=(n);i++)36 37 #define for1(i,n) for(int i=1;i<=(n);i++)38 39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)40 41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)42 43 #define mod 100000000744 45 using namespace std;46 47 inline int read()48 49 {50 51     int x=0,f=1;char ch=getchar();52 53     while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}54 55     while(ch>=0&&ch<=9){x=10*x+ch-0;ch=getchar();}56 57     return x*f;58 59 }60 int n,m,a[maxn],b[maxn],c[maxn];61 char ch;62 63 int main()64 65 {66 67     freopen("input.txt","r",stdin);68 69     freopen("output.txt","w",stdout);70 71     ch=getchar();72     while(ch<=9&&ch>=0)a[++n]=ch-0,ch=getchar();73     for1(i,n>>1)swap(a[i],a[n+1-i]);74     ch=getchar();75     while(ch<=9&&ch>=0)b[++m]=ch-0,ch=getchar();76     for1(i,m>>1)swap(b[i],b[m+1-i]);77     for1(i,n)78      for1(j,m)79       c[i+j-1]+=a[i]*b[j];80     for1(i,n+m)c[i+1]+=c[i]/10,c[i]%=10;81     int t=n+m;82     while(!c[t])t--;83     for3(i,t,1)printf("%d",c[i]);84     printf("\n");85 86     return 0;87 88 }
View Code

 

BZOJ1754: [Usaco2005 qua]Bull Math