2016年9月3日 星期六

TIOJ 1006 - 大數除法

因為各種心血來潮 所以就跑去寫了TIOJ 1006 : http://tioj.infor.org/problems/1006
然後因為想說可以當模板之類的用(?) 所以就試著寫的完整了一點
大概就先把加減乘等於大小於輸入輸出之類的然後再開工除法這樣
一樣是用直式除法的概念去寫(吧
不過重點就是不管怎樣就WA 6 QwQ
各種測資都測過了 不過因為找不到bug所以懶得找(# 於是就貼上來了(?

亂渣code下收


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include<bits/stdc++.h>
#define ll long long

using namespace std;

struct BIG;
BIG _10pow(int);

struct BIG{
    int n[555];
    int length;
    BIG(){
        memset(n,0,sizeof(n));
        length = 0;
    }
    BIG(int in){
        memset(n,0,sizeof(n));
        length = 0;
        int ptr=0;
        while(in){
            this->n[ptr++]=in%10;
            in/=10;
        }
        this->length=ptr;
    }
    BIG(long in){
        memset(n,0,sizeof(n));
        length = 0;
        int ptr=0;
        while(in){
            this->n[ptr++]=in%10;
            in/=10;
        }
        this->length=ptr;
    }
    BIG(ll in){
        memset(n,0,sizeof(n));
        length = 0;
        int ptr=0;
        while(in){
            this->n[ptr++]=in%10;
            in/=10;
        }
        this->length=ptr;
    }
    BIG(string s){
        memset(n,0,sizeof(n));
        length = 0;
        int i;
        for(i=0;i<(int)s.length();i++){
            if(s[i]>'9'||s[i]<'0'){
                printf("WTF are you input?\n");
                break;
            }
            this->n[i]=s[s.length()-1-i]-'0';
        }
        this->length=i;
    }

    void operator=(string const &s){
        stringstream ss;
        ss<<s;
        ss>>*this;
        return;
    }
    void operator=(BIG const &big){
        this->length=big.length;
        for(int i=0;i<this->length;i++){
            this->n[i]=big.n[i];
        }
        return;
    }
    // void operator=(string const &s){
    //     stringstream ss;
    //     ss<<s;
    //     ss>>*this;
    //     return;
    // }

    friend ostream &operator<<(ostream &ostm,BIG big){
        for(int i=big.length-1;i>=0;i--){
            ostm<<big.n[i];
        }
        // ostm<<" ("<<big.length<<")";
        if(big.length==0){
            ostm<<0;
        }
        return ostm;
    }
    friend istream &operator>>(istream &istm,BIG &big){
        memset(&big,0,sizeof(big));
        string s;
        istm>>s;
        int i;
        for(i=0;i<(int)s.length();i++){
            if(s[i]>'9'||s[i]<'0'){
                printf("WTF are you input?\n");
                break;
            }
            big.n[i]=s[s.length()-1-i]-'0';
        }
        big.length=i;
        // ostm<<" ("<<big.length<<")";
        return istm;
    }

    operator bool() const{
        return this->length;
    }

    bool operator<(BIG const &a){
        if(a.length!=this->length)return this->length<a.length;
        for(int i=a.length-1;i>=0;i--){
            if(a.n[i]!=this->n[i])return this->n[i]<a.n[i];
        }
        return false;
    }
    bool operator<=(BIG const &a){
        if(a.length!=this->length)return this->length<=a.length;
        for(int i=a.length-1;i>=0;i--){
            if(a.n[i]!=this->n[i])return this->n[i]<=a.n[i];
        }
        return true;
    }

    bool operator>(BIG const &a){
        return !(*this<=a);
    }

    friend BIG operator+(BIG const &a,int b){
        BIG t;
        int i=0;
        t.n[0]+=b;
        for(;;i++){
            t.n[i]+=a.n[i];
            if(!t.n[i])break;
            if(t.n[i]>9){
                t.n[i+1]+=t.n[i]/10;
                t.n[i]%=10;
            }
        }
        t.length=i;
        return t;
    }
    friend BIG operator+(int b,BIG const &a){
        BIG t;
        int i=0;
        t.n[0]+=b;
        for(;;i++){
            t.n[i]+=a.n[i];
            if(!t.n[i])break;
            if(t.n[i]>9){
                t.n[i+1]+=t.n[i]/10;
                t.n[i]%=10;
            }
        }
        t.length=i;
        return t;
    }
    friend BIG operator+(BIG const &a,BIG const &b){
        BIG t;
        int i=0;
        for(;;i++){
            t.n[i]+=(a.n[i]+b.n[i]);
            if(!t.n[i])break;
            if(t.n[i]>9){
                t.n[i+1]+=t.n[i]/10;
                t.n[i]%=10;
            }
        }
        t.length=i;
        return t;
    }
    BIG& operator++(){
        this->n[0]++;
        for(int i=0;i<this->length;i++){
            if(n[i]>9){
                n[i+1]+=n[i]/10;
                n[i]%=10;
            }
            else{
                break;
            }
        }
        return *this;
    }
    BIG& operator++(int){
        BIG tmp(*this);
        operator++();
        return tmp;
    }

    BIG operator-(int b){
        // cout<<"in BIG - int, int b="<<b<<endl;
        BIG t;
        int i=0;
        t.n[0]-=b;
        for(;;i++){
            t.n[i]+=this->n[i];
            if(t.n[i]==0)break;
            if(t.n[i]<0){
                t.n[i+1]+=((t.n[i]+1)/10-1);
                // cout<<t.n[i]/10<<endl;
                t.n[i]%=10;
                if(t.n[i])t.n[i]+=10;
            }
        }
        t.length=i;
        return t;
    }
    friend BIG operator-(BIG &a,BIG &b){
        // cout<<"in BIG - BIG, a,b="<<a<<" "<<b;
        BIG t=a;
        // cout<<t<<endl;
        if(b>a){
            printf("Wtf are you doing?\n");
            return 0;
        }
        for(int i=0;i<b.length;i++){
            t.n[i]-=b.n[i];
        }
        for(int i=0;i<t.length-1;i++){
            if(t.n[i]<0){
                t.n[i+1]+=((t.n[i]+1)/10-1);
                t.n[i]%=10;
                if(t.n[i])t.n[i]+=10;
            }
        }
        int i;
        for(i=t.length-1;i>=0;i--){
            if(t.n[i])break;
        }
        t.length=i+1;
        // cout<<a.length<<endl;
        // cout<<", t ="<<t<<endl;
        return t;
    }

    friend BIG operator*(BIG const &a,BIG const &b){
        BIG t;
        int mxl=a.length+b.length;
        for(int i=0;i<a.length;i++){
            for(int j=0;j<b.length;j++){
                t.n[i+j]+=a.n[i]*b.n[j];
            }
        }
        for(int i=0;i<mxl+1;i++){
            t.n[i+1]+=t.n[i]/10;
            t.n[i]%=10;
        }
        int i;
        for(i=mxl+1;i>=0;i--){
            if(t.n[i])break;
        }
        t.length=i+1;
        return t;
    }
    friend BIG operator*(BIG const &a,int const &bi){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(BIG const &a,long const &bi){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(BIG const &a,ll const &bi){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(int const &bi,BIG const &a){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(long const &bi,BIG const &a){
        BIG b(bi);
        return a*b;
    }
    friend BIG operator*(ll const &bi,BIG const &a){
        BIG b(bi);
        return a*b;
    }

    friend BIG operator/(BIG &a,BIG &b){
        BIG t;
        int mxl=a.length-b.length+1;
        // cout<<mxl<<endl;
        // int de=0;
        while(a>=b){
            int _l=a.length-b.length;
            nodid:
            if(a<b)break;
            int k=1;
            // cout<<k*b*_10pow(_l)<<endl;
            // cout<<k<<" "<<a<<" "<<b<<" "<<_l<<" "<<k*b*_10pow(_l)<<endl;
            while(k*b*_10pow(_l)<=a){
                k++;
                // cout<<k<<" "<<a<<" "<<b<<" "<<_l<<" "<<k*b*_10pow(_l)<<endl;
                // cout<<"not here"<<endl;
            }
            // cout<<a<<" "<<b<<" ";
            // cout<<"t=";for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
            // cout<<" k="<<k<<endl;
            // assert(de<5);
            // cout<<"_l="<<_l<<", k-1="<<k-1<<endl;
            t.n[_l]+=k-1;
            // cout<<"t=";for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
            // cout<<"-"<<((k-1)*b)*_10pow(_l)<<endl;
            // cout<<"a = "<<a<<", ((k-1)*b)*_10pow(_l) = "<<((k-1)*b)*_10pow(_l)<<" mmm "<<a-(((k-1)*b)*_10pow(_l))<<endl;
            // cout<<"old a = "<<a<<", ";
            // cout<<typeid(((k-1)*b)*_10pow(_l)).name()<<endl;
            // cout<<"making tt"<<endl;
            BIG tt = b*_10pow(_l)*(k-1);
            if(tt.length==0){
                _l--;
                goto nodid;
            }
            a=a-tt;
            // cout<<"tt returned, a="<<a<<endl;
            // cout<<"new a = "<<a<<endl;
            // a.length--;
            // de++;
        }
        // cout<<"t=";for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
        for(int i=0;i<mxl+2;i++){
            t.n[i+1]+=t.n[i]/10;
            t.n[i]%=10;
        }
        // cout<<"t=";for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
        int i;
        for(i=mxl+4;i>=0;i--){
            if(t.n[i])break;
        }
        // for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
        t.length=i+1;
        // for(int i=2;i>=0;i--)putchar(t.n[i]+'0');putchar('\n');
        return t;
    }
};

BIG _10pow(int l){
    BIG t;
    t.n[l]=1;
    t.length=l+1;
    return t;
}

BIG a,b;

int main(){
    cin>>a>>b;
    // if(as==bs){
    //     cout<<1<<endl;
    //     return 0;
    // }
    // BIG a(as);
    // BIG b(bs);
    // a=6546;b=123;
    // c.length+=5;
    // if(a.length>50||b.length>50)return 0;
    if(b==0)return 0;
    cout<<a/b<<endl;
}

沒有留言:

張貼留言