336 条题解
-
0fan672867217 LV 6 @ 2018-03-18 10:01:31
这题用Java无敌
import java.math.BigInteger;
import java.util.Scanner;public class Main
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
BigInteger a=scan.nextBigInteger();
BigInteger b=scan.nextBigInteger();
BigInteger c=a.multiply(b);
System.out.println(c);
}}
-
02018-02-01 21:58:44@
//没有压位。。。蜜汁过了lol #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <string> using namespace std; const int MAXN = 10005; string s1, s2; struct BigInt{ int num[MAXN * 4], size; BigInt(string str){ size = (int)str.size(); for (int i = 0; i < str.size(); i++){num[i] = str[size - i - 1] - '0';} } BigInt operator*(BigInt b2){ const int *num2 = b2.num; BigInt neu(""); neu.size = b2.size + size; for (int i = 0; i < this -> size; i++){ for (int j = 0; j < b2.size; j++){ neu.num[i + j] += num2[j] * num[i]; } } for (int i = 1; i < neu.size + 10; i++){ neu.num[i] += neu.num[i - 1] / 10; neu.num[i - 1] %= 10; } for (int i = neu.size + 10; i >= 0; i--){ if (neu.num[i] != 0) {neu.size = i + 1; break;} } return neu; } void print(){ for (int i = this -> size - 1; i >= 0; i--){ printf("%d", this -> num[i]); } printf("\n"); } }; int main(){ cin >> s1 >> s2; BigInt b1(s1), b2(s2); BigInt neu = b1 * b2; neu.print(); return 0; }
-
02018-01-30 15:47:56@
print(int(input()) * int(input()))
人生苦短(
-
02017-12-11 22:02:11@
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
string tp;struct u{
int A[10000],len;
//构造函数 指的是 一开始变量生成的时候做的初始化操作
u(){
memset(A,0,sizeof(A));
len=1;
}u (string a){
memset(A,0,sizeof(A));
this->len=0;
for(int i=a.length()-1;i>=0;i--){
this->A[this->len]=a[i]-'0';
this->len++;
}
}
void pt(){
for(int i=this->len-1;i>=0;i--){
printf("%d",A[i]);
}
//printf("\n%d",len);
}u operator * (u s)//重载运算符 *
{
u ans;
int maxlen=this->len+s.len+1;
for(int i=0;i<s.len;i++){
for(int j=0;j<this->len;j++){
ans.A[i+j]+=this->A[j]*s.A[i];
ans.A[i+j+1]+=ans.A[i+j]/10;
ans.A[i+j]%=10;
}
}
while(ans.A[maxlen]==0 && maxlen>0) maxlen--;
ans.len=maxlen+1;
return ans;
}};
int main(){
cin>>tp;
u a(tp);
cin>>tp;
u b(tp);
u c=a*b;
c.pt();
return 0;
} -
02017-10-31 13:15:01@
FFT
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cstring>
using namespace std;
const long double pi=acos(-1);
struct cp{long double x,y;};
cp operator +(cp a,cp b){return (cp){a.x+b.x,a.y+b.y};}
cp operator -(cp a,cp b){return (cp){a.x-b.x,a.y-b.y};}
cp operator *(cp a,cp b){return (cp){a.x*b.x-a.y*b.y,a.x*b.y+a.y*b.x};}
cp cur[1000005],a[1000005],b[1000005];
char ch[1000005];
int len1,len2,len,ans[1000005];
void fft(cp *a,int n,int fl){
for(int i=(n>>1),j=1;j<n;++j){
if(i<j)swap(a[i],a[j]);
int k;
for(k=(n>>1);k&i;i^=k,k>>=1);i^=k;
}
for(int m=2;m<=n;m<<=1){
cp w=(cp){cos(2*pi*fl/m),sin(2*pi*fl/m)};
cur[0]=(cp){1,0};
for(int j=1;j<(m>>1);j++)cur[j]=cur[j-1]*w;
for(int i=0;i<n;i+=m)
for(int j=i;j<i+(m>>1);++j){
cp u=a[j],v=a[j+(m>>1)]*cur[j-i];
a[j]=u+v;
a[j+(m>>1)]=u-v;
}
}
if(fl==-1)
for(int i=0;i<n;i++)
a[i]=(cp){a[i].x/n,a[i].y/n};
}
int main(){
scanf("%s",ch);
len1=strlen(ch);
for(int i=len1-1;i>=0;i--)a[len1-i-1]=(cp){ch[i]-'0',0};
scanf("%s",ch);
len2=strlen(ch);
for(int i=len2-1;i>=0;i--)b[len2-i-1]=(cp){ch[i]-'0',0};
len=1;
while(len<(len1+len1)||len<(len2+len2))len<<=1;
for(int i=len1;i<len;i++)a[i]=(cp){0,0};
for(int i=len2;i<len;i++)b[i]=(cp){0,0};
fft(a,len,1);fft(b,len,1);
for(int i=0;i<len;i++)a[i]=a[i]*b[i];
fft(a,len,-1);
for(int i=0;i<len;i++)ans[i]=a[i].x+0.5;
for(int i=0;i<len;i++)ans[i+1]+=ans[i]/10,ans[i]%=10;
while(!ans[len-1]&&len)len--;
for(int i=len-1;i>=0;i--)printf("%d",ans[i]);
printf("\n");
return 0;
} -
02017-10-18 17:40:38@
QAQ
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<cstdlib> #include<cmath> #define maxn 100005 using namespace std; char s1[maxn],s2[maxn]; int c[maxn],a1[maxn],a2[maxn]; void mul(int a[],int b[]){ for(int i=1;i<=a[0];i++){ for(int j=i;j<=b[0]+i-1;j++){ c[j]+=a[i]*b[j-i+1]; if(c[j]>=10){ c[j+1]+=c[j]/10; c[j]=c[j]%10; } } }c[0]=a[0]+b[0]; while(c[c[0]]==0&&c[0]>0){c[0]--; } while(c[c[0]+1]){ c[0]++; c[c[0]+1]+=c[c[0]]/10; c[c[0]]%=10; } } int main(){ scanf("%s%s",s1+1,s2+1); int len1=strlen(s1+1),len2=strlen(s2+1); for(int i=1;i<=len1;i++){ a1[i]=s1[len1-i+1]-'0'; } for(int i=1;i<=len2;i++){ a2[i]=s2[len2-i+1]-'0'; }a1[0]=len1,a2[0]=len2; mul(a1,a2); for(int i=c[0];i>=1;i--){ printf("%d",c[i]); } }
-
02017-08-18 16:18:30@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;int main()
{
char a[10000] , b[10000];
int n[10000]={} , m[10000]={} , result[10000] = {};
int la , lb , i , j , w = 0;
int k = w;
cin >> a >> b;
la = strlen(a);
lb = strlen(b);
for(i = 0;i < la;i++) n[i] = a[la-i-1] - '0';
for(i = 0;i < lb;i++) m[i] = b[lb-i-1] - '0';
for(i = 0;i < la;i++)
for(j = 0;j < lb;j++)
{
result[i + j] += n[i] * m[j];
if(result[i + j] != 0) w = i + j;
}
for(i = 0;i <= w + 5;i++)
{
result[i + 1] += result[i] / 10;
result[i] %= 10;
if(result[i] != 0) k = i;
}
for(i = k;i >= 0;i--) cout << result[i];
return 0;
} -
02017-08-18 16:18:17@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;int main()
{
char a[10000] , b[10000];
int n[10000]={} , m[10000]={} , result[10000] = {};
int la , lb , i , j , w = 0;
int k = w;
cin >> a >> b;
la = strlen(a);
lb = strlen(b);
for(i = 0;i < la;i++) n[i] = a[la-i-1] - '0';
for(i = 0;i < lb;i++) m[i] = b[lb-i-1] - '0';
for(i = 0;i < la;i++)
for(j = 0;j < lb;j++)
{
result[i + j] += n[i] * m[j];
if(result[i + j] != 0) w = i + j;
}
for(i = 0;i <= w + 5;i++)
{
result[i + 1] += result[i] / 10;
result[i] %= 10;
if(result[i] != 0) k = i;
}
for(i = k;i >= 0;i--) cout << result[i];
return 0;
} -
02017-08-16 18:39:53@
好短的主函数
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char cd[100000];
void maint(char ak[10001],char bk[10001])
{
int a[100000],b[100000],c[100001],d,e,f,z=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
d=strlen(ak);
e=strlen(bk);
for(int i=0;i<=d-1;++i)
{
a[d-i]=ak[i]-48;
}
for(int j=0;j<=e-1;++j)
{
b[e-j]=bk[j]-48;
}
for(int q=1;q<=d;++q)
{
z=0;
for(int v=1;v<=e;++v)
{
c[q+v-1]=a[q]*b[v]+z+c[q+v-1];
z=c[q+v-1]/10;
c[q+v-1]=c[q+v-1]%10;
}
c[q+e]=z;
}
f=d+e;
while((c[f]==0)&&(f>1))
{
f--;
}
for(int u=1;u<=f;++u)
{
cd[u]=0;
}
for(int u=f;u>=1;u--)
{
cd[f-u]=c[u]+48;
}
cout<<cd;
}
int main()
{
char a[10001],b[10001];
cin>>a>>b;
maint(a,b);
} -
02017-08-08 21:49:42@
#include "stdio.h" #include "string.h" struct BigInt{ int len; int nums[100000]; }; int main(){ struct BigInt a, b, c; memset(&a, 0, sizeof(a)); memset(&b, 0, sizeof(b)); memset(&c, 0, sizeof(c)); char input[100000]; scanf("%s", input); for(int i = strlen(input) - 1; i >= 0 ; i--){ a.nums[a.len++] = input[i] - 48; } scanf("%s", input); for(int i = strlen(input) - 1; i >= 0 ; i--){ b.nums[b.len++] = input[i] - 48; } c.len = a.len + b.len; for(int i = 0; i < a.len; i++){ for(int j = 0; j < b.len; j++){ c.nums[i+j] = c.nums[i+j] + a.nums[i] * b.nums[j]; } } for(int i = 0; i < c.len; i++){ c.nums[i+1] = c.nums[i] / 10 + c.nums[i+1]; c.nums[i] = c.nums[i] % 10; } while (c.nums[c.len] == 0 && c.len > 0){ c.len--; } for(int i = c.len; i >= 0; i--){ printf("%d", c.nums[i]); } printf("\n"); return 0; }
-
02017-08-02 17:57:11@
--Haskell一行 main = print . product . map read . words =<< getContents
-
02017-07-29 22:13:54@
#include <cstdio> #include <string> #include <algorithm> #include <iostream> using namespace std; #define MAX 1 // D[i] : 0....99999 int pow_int(int a,int n) { int count=1,x=a; while(n>0) { if(n&1) count*=x; x=x*x; n=n>>1; } return count; } struct BigInt { int D[100000]; int max; int n; BigInt(int x) { fill(D,D+100,0); int i; max=pow_int(10,MAX); for(i=0;x>0;i++) { D[i]=x%max; x=x/max; } n=i; } BigInt(void) { fill(D,D+100,0); n=0; max=pow_int(10,MAX); } void print(void) { cout<<D[n-1]; for(int i=n-2;i>=0;i--) { cout<<D[i]; } cout<<endl; } }; BigInt &mul(BigInt &a,BigInt &b); int main() { string s1,s2; cin>>s1>>s2; BigInt a,b; for(int i=s1.size()-1,j=0;i>=0;i--,j++) a.D[j]=s1[i]-'0'; a.n=s1.size(); for(int i=s2.size()-1,j=0;i>=0;i--,j++) b.D[j]=s2[i]-'0'; b.n=s2.size(); BigInt result=mul(a,b); result.print(); return 0; } BigInt &mul(BigInt &a,BigInt &b) { BigInt *result=new BigInt(0); for(int i=0;i<a.n;i++) { for(int j=0;j<b.n;j++) { result->D[i+j]+=a.D[i]*b.D[j]; if(result->D[i+j]>=result->max) result->D[i+j+1]+=result->D[i+j]/result->max; result->D[i+j]=result->D[i+j]%result->max; } } result->n=a.n+b.n-1; while(result->D[result->n]==0&&result->n>1) { result->n--; } if(result->D[result->n]) result->n++; return *result; }
-
02017-07-22 11:30:45@
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <bitset>
#include <cassert>
#include <map>
#include <string>
#include <sstream>
#include <ctime>
using namespace std;
int a[20010],b[20010],c[20010];
char s1[20010],s2[20010],s3[20010];
int main()
{
int la,lb,l;
scanf("%s%s",s1,s2);
la=strlen(s1);
lb=strlen(s2);
for(int i=1;i<=la;i++)
{
a[i]=s1[la-i]-'0';
}
for(int i=1;i<=lb;i++)
{
b[i]=s2[lb-i]-'0';
}
for(int i=1;i<=la;i++)
{
for(int j=1;j<=lb;j++)
{
c[i+j-1]=c[i+j-1]+a[i]*b[j];
c[i+j]=c[i+j]+c[i+j-1]/10;
c[i+j-1]=c[i+j-1]%10;
}
}
l=la+lb;
while(c[l]==0&&l>1)
{
l--;
}
for(int i=l;i>=1;i--)
{
printf("%d",c[i]);
}
return 0;
} -
02017-05-07 22:16:47@
裸题~
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <iomanip> #include <cstdlib> using namespace std; char str1[10002],str2[10002]; int a[10002],b[10002],c[20010]; int c1,c2; void init() { for(int i=0;i<c1;i++) a[c1-i-1]=str1[i]-'0'; for(int i=0;i<c2;i++) b[c2-i-1]=str2[i]-'0'; } int main() { scanf("%s %s",str1,str2); c1=strlen(str1); c2=strlen(str2); init(); for(int i=0;i<c1;i++) for(int j=0;j<c2;j++) { c[i+j]+=a[i]*b[j]; if(c[i+j]>=10) c[i+j+1]+=c[i+j]/10,c[i+j]%=10; } int k; for(k=c1+c2;k>=0;k--) if(c[k]!=0) break; for(int i=k;i>=0;i--) cout<<c[i]; return 0; }
-
02017-03-19 21:54:29@
注意前面的0
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>using namespace std;
int main()
{
string s1,s2;
cin>>s1>>s2;
int a[10001];
int b[10001];
int ans[100005];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(ans,0,sizeof(ans));
a[0]=s1.length();
b[0]=s2.length();
for(int i=1;i<=a[0];i++)
{
a[i]=s1[a[0]-i]-'0';
}
for(int i=1;i<=b[0];i++)
{
b[i]=s2[b[0]-i]-'0';
}
int w,x=0;
w=a[0]+b[0];
for(int i=1;i<=a[0];i++)
{
x=0;
for(int j=1;j<=b[0];j++)
{
ans[i+j-1]=a[i]*b[j]+x+ans[i+j-1];
x=ans[i+j-1]/10;
ans[i+j-1]=ans[i+j-1]%10;
}
ans[i+b[0]]=x;
}
while(ans[w]==0&&w>1)
w--;
for(int i=w;i>=1;i--)
{
cout<<ans[i];
}
return 0;
} -
02017-03-05 03:30:43@
#include <stdio.h>
#include <string.h>
char anss[110000000];
char * multipy(char * a, char * b)
{
int i,j,la,lb,l,k;
la=strlen(a);
lb=strlen(b);
l=la+lb;
char ans[l+1];
memset(ans,0,sizeof(ans));
//for(i=0;i<l;i++)printf("%d",ans[i]);
for (i=la-1;i>=0;i--)
{
//printf("i=%d\n",i);
for(j=lb-1;j>=0;j--)
{
//printf("j=%d\n",j);
k=l-la-lb+i+j+1;//printf("pre_ans[%d]=%d\n",k,ans[k]);
ans[k]+=(a[i]-'0')*(b[j]-'0');
ans[k-1]+=ans[k]/10;
ans[k]=ans[k]%10;
//printf("ans[%d]=%d\n",k,ans[k]);
}
}
k=0;
while(ans[k]!=0) k--;
if(ans[0]==0)i=1; else i=0;
for (j=i;j<l;j++) anss[j-i]=ans[j]+'0';//printf("%d",ans[i]);
return anss;
}
int main()
{
char input1[10001],input2[10001];
scanf("%s %s",input1,input2);
printf("%s",multipy(input1,input2));
return 0;
} -
02016-11-17 18:52:12@
调了半天 才发现result开的10000+位
幡然醒悟应该开20000位
#include <cstdio>
#include <cstring>
#define Q 6000
#define clear(a) memset(a,0,Q*sizeof(int))void input(int a[]){
clear(a);
char c[11000];
scanf("%s",c);
int len=1,k=1;
for(int i=strlen(c)-1;i>=0;i--){
if(k==10000)
k=1,len++;
a[len]+=k*(c[i]-'0');
k*=10;
}
a[0]=len;
}void mult(int a[],int b[],int c[]){
clear(c);
for(int i=1;i<=a[0];i++)
for(int j=1;j<=b[0];j++){
c[i+j-1]+=a[i]*b[j];
c[i+j]+=c[i+j-1]/10000;
c[i+j-1]%=10000;
}
int len=a[0]+b[0];
while(len>1&&c[len]==0)len--;
c[0]=len;
}void output(int a[]){
printf("%d",a[a[0]]);
for(int i=a[0]-1;i>=1;i--)
printf("%04d",a[i]);
}int main(){
freopen("in.txt","r",stdin);
int x[Q],y[Q],r[Q];
input(x);
input(y);
mult(x,y,r);
output(r);
return 0;
} -
02016-11-05 11:04:49@
蒟蒻写的非常简单的代码,并没有用FFT或者压位什么的
但是居然AC了......
供参考
c++
#include <cstdio>
#include <cstring>
using namespace std;
int a[10001],b[10001],c[20001];
char s1[10001],s2[10001];
int main() {
gets(s1);gets(s2);
int i,j,l1=strlen(s1),l2=strlen(s2),x=0;
for (i=1;i<=l1;i++) a[i]=s1[l1-i]-48;
for (i=1;i<=l2;i++) b[i]=s2[l2-i]-48;
for (i=1;i<=l1;i++)
for (j=1;j<=l2;j++)
c[i+j]+=a[i]*b[j];
for (i=1;i<=l1+l2;i++) { //统一整理进位
c[i+1]+=c[i]/10;
c[i]%=10;
}
int max=l1+l2+1;
while (c[max]==0) max--; //去除数字前的零
for (i=max;i>=2;i--) printf("%d",c[i]);
printf("\n");
return 0;
}
-
02016-10-26 13:42:11@
#include<bits/stdc++.h>
using namespace std;
char A[50000000],B[5000000];
long long a[5000000],b[5000000],c[5000000];int main()
{
int i,j,k=0,n,m,x;
long long l;
cin>>A>>B;
n=strlen(A); m=strlen(B);
strrev(A); strrev(B);
memset(a,0,sizeof(a)) ; memset(b,0,sizeof(b)) ; memset(c,0,sizeof(c)) ;
for(i=0;i<n;i++) a[i]=A[i]-48; for(i=0;i<m;i++) b[i]=B[i]-48;
k=0;
for (i=0;i<n;i++)
for (j=0;j<m+1;j++)
{
c[i+j] = c[i+j] + a[i] * b[j] +k;
k= c[i+j] / 10 ;
c[i+j] = c[i+j] % 10 ;
}
k=22000;
while (c[k]==0) k--;
for(i=k;i>=0;i--) cout<<c[i];
cout<<endl;
return 0;
} -
02016-10-22 23:13:45@
某语言一行
print int(raw_input())*int(raw_input())