题解

335 条题解

  • 0
    @ 2006-08-16 09:46:41

    就用万进制,不过要小心下标越界(我越了一次,结果多打了5000多位)

  • 0
    @ 2006-08-15 23:34:35

    郁闷啊,我四位乘四位,结果还是超时!

  • 0
    @ 2006-07-24 10:50:49

    哈哈,俺没用万进制也过了...不过还是弄了很长时间...把高位和低位弄翻了...

  • 0
    @ 2006-01-26 09:57:13

    测试int64的结果是 不比longint快

    大概int64乘法运算起来太慢了...

  • 0
    @ 2006-01-25 23:22:14

    就按一般的高精度乘法就可以了

    4位4位乘

    用longint存(因为4+4超出integer范围)

    至于int64没试过,这样可以8位8位乘,但是可能速度会慢不少

  • -1
    @ 2020-03-29 23:43:31
    print(int(input()) * int(input()) )
    

    这才是python的精髓好吗!
    一行搞定,秒杀c/c++/Pascal/java。

  • -1
    @ 2020-03-06 23:24:47
    //<>
    #include <iostream>         //高精度乘法
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int MAXN = 20005;
    
    char n1[MAXN], n2[MAXN];
    int a[MAXN], b[MAXN], c[MAXN], len1, len2;
    
    void char_int(char n[], int a[], int len)
    {
        int j = len - 1;
        for (int i = 1; i <= len; i++)
            a[i] = n[j--] - '0';
    }
    
    void Print_num(int num[], int len)
    {
        for (int i = len; i >= 1; i--)
            cout << num[i];
    }
    
    int Mul_ab(int a[], int b[])
    {
        int len3 = len1 + len2;
        for (int i = 1; i <= len1; i++)
            for (int j = 1; j <= len2; j++)
            {
                c[i + j - 1] += a[i] * b[j];
                c[i + j] += c[i + j - 1] / 10;      //进位
                c[i + j - 1] %= 10; 
            }
        if (c[len3] == 0)
            len3--;
        return len3;
    }
    
    void Print(int len3)
    {
        /*Print_num(a, len1);
        cout << " * ";
        Print_num(b, len2);
        cout << " = ";*/
        Print_num(c, len3);
        cout << endl;
    }
    
    int main()
    {
        scanf("%s", n1);
        scanf("%s", n2);
        len1 = strlen(n1);
        len2 = strlen(n2);
    
        char_int(n1, a, len1);
        char_int(n2, b, len2);
    
        int k = Mul_ab(a, b);
    
        Print(k);
    
        system("pause");
        return 0;
    }
    
  • -1
    @ 2018-11-14 16:59:57
    #include <iostream>
    #include <iomanip>
    #include <vector>
    
    using namespace std;
    
    class bignum{
    
    public:
        vector<long long> a;
    
    public:
        bignum(){}
    
        bignum(string& s) {
            int l = (int)s.length();
            for(int i = l - 1; i >= 0; i -= 4) {
                int d1 = s[i] - '0';
                int d2 = ((i - 1 >= 0)? s[i - 1] - '0': 0);
                int d3 = ((i - 2 >= 0)? s[i - 2] - '0': 0);
                int d4 = ((i - 3 >= 0)? s[i - 3] - '0': 0);
                a.push_back(d1 + d2 * 10ll + d3 * 100ll + d4 * 1000ll);
            }
        }
    
        bignum operator* (const bignum& b){
            bignum ret;
            size_t d1 = (this -> a).size(), d2 = b.a.size();
            ret.a.resize(d1 + d2);
    
            for(int s = 0; s <= d1 + d2 - 2; s++){
                ret.a[s] = 0;
                for(int k = 0; k <= d1 - 1; k++){
                    if(s - k >= 0 && s - k <= d2 - 1) ret.a[s] += a[k] * b.a[s - k];
                }
            }
    
            for(int i = 0; i < d1 + d2 - 2; i++){
                if(ret.a[i] >= 10000) {
                    ret.a[i + 1] += ret.a[i] / 10000ll;
                    ret.a[i] %= 10000ll;
                }
            }
    
            int p = d1 + d2 - 1;
            while(ret.a[p] > 10000ll){
                ret.a.push_back(ret.a[p] / 10000ll);
                ret.a[p] %= 10000ll;
                p++;
            }
    
            for(int i = d1 + d2 - 1; i > 0; i--){
                if(ret.a[i] == 0) ret.a.pop_back();
                else break;
            }
            return ret;
        }
    
        void output() {
            size_t d = a.size();
            cout << a[d - 1];
            for(int i = d - 2; i >= 0; i--){
                cout << setw(4) << setfill('0') << a[i];
            }
        }
    };
    
    
    string a1, a2;
    
    int main(){
        cin >> a1 >> a2;
        bool fu = false;
        if(a1[0] == '-') { fu = !fu; a1 = a1.substr(1, a1.length());}
        if(a2[0] == '-') { fu = !fu; a2 = a2.substr(1, a1.length());}
        bignum b1(a1), b2(a2);
        bignum mu = b1 * b2;
        if(fu) cout << "-";
        mu.output();
        cout << endl;
    }
    

    4位的万进制, 卷积中间有可能爆int, wa了好几次

  • -1
    @ 2018-11-10 09:42:58

    print(int(input())*int(input()))
    无敌的Python

  • -1
    @ 2018-10-19 19:07:11

    #include<algorithm>
    #include<iostream>
    using namespace std;
    const int MAXN=10005;
    int a[MAXN],b[MAXN],c[MAXN],ans[MAXN],len_a,len_b,len_ans;
    void read(int *a,int &len)
    {

    string cur;

    cin>>cur;

    len=cur.length();

    for(int i=0;i<len;i++) a[i]=cur[i]-48;
    reverse(a,a+len);
    }
    int main()
    {
    read(a,len_a);
    read(b,len_b);
    len_ans=len_a+len_b-1;
    for(int i=0;i<=len_a;i++)
    for(int j=0;j<len_b;j++)
    ans[i+j]+=a[i]*b[j];
    for(int i=0;i<=len_ans;i++)
    if(ans[i]>9) ans[i+1]+=ans[i]/10,ans[i]%=10;

    while(ans[len_ans]) len_ans++;

    for(int i=len_ans-1;i>=0;i--)

    cout<<ans[i];

    return 0;
    }

  • -1
    @ 2017-08-02 12:18:41

    手打高精。。

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    int main()
    {
        char a1[10005],b1[10005];
        int a[10005],b[10005],c[1000005],lena,lenb,lenc,i,j,x;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        gets(a1);
        gets(b1);
        lena=strlen(a1);
        lenb=strlen(b1);
        for(i=0;i<=lena-1;i++)
        a[lena-i]=a1[i]-48;
        for(i=0;i<=lenb-1;i++)
        b[lena-i]=b1[i]-48;
        for(i=1;i<=lena;i++)
        {
            x=0;
            for(j=1;j<=lenb;j++)
            {
                c[i+j-1]=a[i]*b[j]+x+c[i+j-1];
                x=c[i+j-1]/10;
                c[i+j-1]%=10;
            }
            c[i+lenb]=x;
        }
        lenc=lena+lenb;
        while(c[lenc]==0&&lenc>1)
        lenc--;
        for(i=lenc;i>=1;i--)
        cout<<c[i];
        cout<<endl;
    }
    
  • -1
    @ 2017-07-21 17:25:51

    #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;
    }

  • -1
    @ 2017-02-27 00:03:13

    虽然用java秒杀
    但是还是不知道为什么这个错了

    #include<algorithm>
    #include<iostream>
    #include<iomanip>
    #include<cstring>
    #include<cstdlib>
    #include<vector>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    using namespace std;
    inline const int Get_Int() {
        int num=0,bj=1;
        char x=getchar();
        while(x<'0'||x>'9') {
            if(x=='-')bj=-1;
            x=getchar();
        }
        while(x>='0'&&x<='9') {
            num=num*10+x-'0';
            x=getchar();
        }
        return num*bj;
    }
    struct BigInteger {
        static const int BASE=10000; //高进制 
        static const int WIDTH=4; //高进制位数 
        vector<int>s;
        int bj; //负数
        BigInteger() { //构造函数:初始赋0
            *this=0;
            bj=0;
        }
        BigInteger(long long num) { // 构造函数
            if(num==0)bj=0;
            else if(num<0)bj=-1;
            else bj=1;
            *this=abs(num);
        }
        //交换BigInteger 
        void swap(BigInteger& a,BigInteger& b) {
            BigInteger t=a;
            a=b;
            b=t;
        } 
        //赋值
        BigInteger operator = (long long num) {
            s.clear();
            bj=1;
            if(num==0)bj=0;
            else if(num<0) {
                num=abs(num);
                bj=-1;
            }
            do {
                s.push_back(num%BASE);
                num/=BASE;
            } while(num>0);
            return *this;
        }
        BigInteger operator = (const string& str) {
            s.clear();
            int x,len=(str.length()-1)/WIDTH+1;
            for(int i=0; i<len; i++) {
                int end=str.length()-i*WIDTH;
                int start=max(0,end-WIDTH);
                sscanf(str.substr(start,end-start).c_str(),"%d",&x);
                s.push_back(x);
            }
            if(*this==0)bj=0;
            return *this;
        }
        //比较
        bool operator < (const BigInteger& b) {
            if(s.size()<b.s.size())return true;
            if(s.size()>b.s.size())return false;
            for(int i=s.size()-1; i>=0; i--) {
                if(s[i]<b.s[i])return true;
                if(s[i]>b.s[i])return false;
            }
            return false;
        }
        bool operator >= (const BigInteger& b) {
            return !(*this<b);
        }
        bool operator <= (const BigInteger& b) {
            return !(*this>b);
        }
        bool operator == (const BigInteger& b) {
            if(s.size()!=b.s.size())return false;
            for(int i=0; i<s.size(); i++)
                if(s[i]!=b.s[i])return false;
            return true;
        }
        bool operator != (const BigInteger& b) {
            return !(*this==b);
        }
        bool operator > (const BigInteger& b) {
            return ((*this>=b)&&(*this!=b));
        }
        //+
        BigInteger operator + (BigInteger& b) {
            if(bj==-1&&b.bj!=-1) {
                BigInteger a=*this;
                a.bj=1;
                return b-a;
            } else if(bj!=-1&&b.bj==-1) {
                BigInteger tmp=b;
                tmp.bj=1;
                return *this-tmp;
            }
            BigInteger c;
            c.bj=1;
            if(bj==-1&&b.bj==-1)c.bj=-1;
            else if(bj==0)c.bj=b.bj;
            else if(b.bj==0)c.bj=bj;
            c.s.resize(max(s.size(),b.s.size())+1);
            for(int i=0; i<c.s.size()-1; i++) {
                int tmp1,tmp2;
                if(i>=s.size())tmp1=0;
                else tmp1=s[i];
                if(i>=b.s.size())tmp2=0;
                else tmp2=b.s[i];
                c.s[i]=tmp1+tmp2;
            }
            for(int i=0; i<c.s.size()-1; i++) {
                c.s[i+1]+=c.s[i]/BASE;
                c.s[i]%=BASE;
            }
            while(c.s.back()==0&&c.s.size()>1)c.s.pop_back();
            return c;
        }
        void operator += (BigInteger& b) {
            *this=*this+b;
        }
        BigInteger operator + (int b) {
            BigInteger c;
            c=b;
            c+=*this;
            return c;
        }
        void operator += (int b) {
            *this=*this+b;
        }
        //-
        BigInteger operator - (BigInteger& b) {
            if(bj==-1&&b.bj!=-1) {
                BigInteger a=*this;
                a.bj=1;
                return -(a+b);
            } else if(bj!=-1&&b.bj==-1) {
                BigInteger tmp=b;
                tmp.bj=1;
                return *this+tmp;
            }
            BigInteger c;
            c=*this;
            if(c==b) {
                c=0;
                return c;
            } else if(c<b) {
                BigInteger tmp=b-c;
                tmp.bj*=-1;
                return tmp;
            }
            for(int i=0; i<c.s.size(); i++) {
                int tmp;
                if(i>=b.s.size())tmp=0;
                else tmp=b.s[i];
                if(c.s[i]<tmp) {
                    c.s[i+1]-=1;
                    c.s[i]+=BASE;
                }
                c.s[i]-=tmp;
            }
            while(c.s.back()==0&&c.s.size()>1)c.s.pop_back();
            return c;
        }
        void operator -= (BigInteger& b) {
            *this=*this-b;
        }
        BigInteger operator - (int b) {
            BigInteger c,d=*this;
            c=b;
            d-=c;
            return d;
        }
        void operator -= (int b) {
            *this=*this-b;
        }
        //*
        BigInteger operator * (int b) {
            BigInteger c;
            c=*this;
            if(b<0) {
                b=abs(b);
                c.bj=-1*c.bj;
            }
            c.s.resize(c.s.size()*10);
            for(int i=0; i<c.s.size(); i++)c.s[i]*=b;
            for(int i=0; i<c.s.size()-1; i++) {
                c.s[i+1]+=c.s[i]/BASE;
                c.s[i]%=BASE;
            }
            while(c.s.back()==0&&c.s.size()>1)c.s.pop_back();
            return c;
        }
        void operator *= (int b) {
            *this=*this*b;
        }
        BigInteger operator * (const BigInteger& b) { 
            BigInteger c;
            c.bj=bj*b.bj;
            c.s.resize(s.size()+b.s.size());
            for(int i=0; i<s.size(); i++)
                for(int j=0; j<b.s.size(); j++)c.s[i+j]+=s[i]*b.s[j];
            for(int i=0; i<c.s.size()-1; i++) {
                c.s[i+1]+=c.s[i]/BASE;
                c.s[i]%=BASE;
            }
            while(c.s.back()==0&&c.s.size()>1)c.s.pop_back();
            return c;
        }
        void operator *= (const BigInteger& b) {
            *this=*this*b;
        }
        // /
        BigInteger operator / (int k) {
            int d=0;
            BigInteger c;
            c=*this;
            if(k<0) {
                k=abs(k);
                c.bj=-1*c.bj;
            }
            for(int i=c.s.size()-1; i>=0; i--) {
                d=d*BASE+c.s[i];
                c.s[i]=d/k;
                d%=k;
            }
            while(c.s.back()==0&&c.s.size()>1)c.s.pop_back();
            return c;
        }
        void operator /= (int k) {
            *this=*this/k;
        }
        BigInteger Copy(const BigInteger& b,int x) {
            BigInteger t;
            t.s.resize(b.s.size()+x);
            for(int i=0; i<b.s.size(); i++)t.s[i+x]=b.s[i];
            return t;
        }
        BigInteger operator / (const BigInteger& b) {
            BigInteger c,d;
            c.s.resize(s.size()-b.s.size()+1);
            d=*this;
            c.bj=bj*b.bj;
            for(int i=c.s.size()-1; i>=0; i--) {
                BigInteger t;
                t=Copy(b,i);
                while(d>=t) {
                    (c.s[i])++;
                    d-=t;
                }
            }
            while(c.s.back()==0&&c.s.size()>1)c.s.pop_back();
            return c;
        }
        void operator /= (const BigInteger& b) {
            *this=*this/b;
        }
        //%
        BigInteger operator % (const BigInteger& b) {
            BigInteger c,d;
            c.s.resize(s.size()-b.s.size()+1);
            d=*this;
            for(int i=c.s.size()-1; i>=0; i--) {
                BigInteger t;
                t=Copy(b,i);
                while(d>=t) {
                    (c.s[i])++;
                    d-=t;
                }
            }
            c=d;
            return c;
        }
        void operator %= (const BigInteger& b) {
            *this=*this%b;
        }
        BigInteger operator % (int k) {
            int d=0;
            BigInteger c;
            c=*this;
            for(int i=c.s.size()-1; i>=0; i--) {
                d=d*BASE+c.s[i];
                c.s[i]=d/k;
                d%=k;
            }
            c=d;
            return c;
        }
        void operator %= (int k) {
            *this=*this%k;
        }
        //±
        BigInteger operator + () {
            return *this;
        }
        BigInteger operator - () {
            BigInteger a=*this;
            a.bj*=-1;
            return a;
        }
        BigInteger operator ++ () {
            *this+=1;
            return *this;
        }
        const BigInteger operator ++ (int) {
            BigInteger tmp=*this;
            *this=*this+1;
            return tmp;
        }
        BigInteger operator -- () {
            *this-=1;
            return *this;
        }
        const BigInteger operator -- (int) {
            BigInteger tmp=*this;
            *this=*this-1;
            return tmp;
        }
    };
    istream& operator >> (istream& input,BigInteger& x) {
        string s;
        if(!(input>>s))return input;
        if(s[0]=='-') {
            x.bj=-1;
            s=s.substr(1);
        } else x.bj=1;
        x=s;
        return input;
    }
    ostream& operator << (ostream& output,const BigInteger& x) {
        if(x.bj==-1)cout<<"-";
        output<<x.s.back();
        for(int i=x.s.size()-2; i>=0; i--) {
            char buf[20];
            sprintf(buf,"%04d",x.s[i]);
            for(int j=0; j<strlen(buf); j++)output<<buf[j];
        }
        return output;
    }
    BigInteger a,b;
    int main() {
        cin>>a>>b;
        cout<<a*b<<endl;
        return 0;
    }
    
  • -1
    @ 2016-11-17 22:36:16

    python。。。
    print int(raw_input())*int(raw_input())

  • -1
    @ 2016-09-25 15:38:10

    测试数据 #0: Accepted, time = 0 ms, mem = 724 KiB, score = 25

    测试数据 #1: Accepted, time = 0 ms, mem = 728 KiB, score = 25

    测试数据 #2: Accepted, time = 0 ms, mem = 724 KiB, score = 25

    测试数据 #3: Accepted, time = 0 ms, mem = 724 KiB, score = 25

    我的递交记录

    • @ 2017-07-05 17:18:38

      大神
      代码给一段吧
      关于压位

    • @ 2017-12-11 22:02:53

      @追梦人: #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;
      }

信息

ID
1040
难度
7
分类
高精度 点击显示
标签
(无)
递交数
16520
已通过
3156
通过率
19%
被复制
24
上传者