A 大数运算
大数模版写得不熟,然后出了不少错误,准备自己写一个模版。
G 拓展欧几里德求逆元
这道题公式没推错,代入参数搞错了,然后一直在调,浪费不少时间。
I 求线段交点
很普通的计算题,WA了6次,最后才A掉,计算时没有考虑double比较大小的细节,手写的fabs没过。
今天就A了三道最水的题,比较让我失望,I题不应该错这么多次,细节决定成败,这句话放在哪都没错,因为这样WA掉罚时真的很不值。希望明天比赛顺利,然后进入校队。
A:
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<iostream> using namespace std; const int base = 10000; const int width = 4; const int N = 11000; char buf[11000]; struct bint { int ln, v[N]; bint(int r = 0){ for(ln = 0; r > 0; r /= base) v[ln ++] = r % base; } bint& operator = (const bint& r){ memcpy(this, &r, (r.ln + 1) * sizeof(int)); return * this; } }; bint a, b, c; bool operator < (const bint& a, const bint& b){ int i; if(a.ln != b.ln) return a.ln < b.ln; for(i = a.ln - 1; i >= 0 && a.v[i] == b.v[i]; i --); return i < 0 ? 0 : a.v[i] < b.v[i]; } bool operator <= (const bint& a, const bint& b){ return !(b < a); } bint operator + (const bint& a, const bint& b) { bint res; int i, cy = 0; for(i = 0; i < a.ln || cy > 0; i ++){ if(i < a.ln) cy += a.v[i]; if(i < b.ln) cy += b.v[i]; res.v[i] = cy % base; cy /= base; } res.ln = i; return res; } bint operator - (const bint& a, const bint& b){ bint res; int i, cy = 0; for(res.ln = a.ln, i = 0; i < res.ln; i ++){ res.v[i] = a.v[i] - cy; if(i < b.ln) res.v[i] -= b.v[i]; if(res.v[i] < 0) cy = 1, res.v[i] += base; else cy = 0; } while(res.ln > 0 && res.v[res.ln - 1] == 0) res.ln --; return res; } bint operator * (const bint& a, const bint& b) { bint res;res.ln = 0; if(0 == b.ln) {res.v[0] = 0; return res;} int i, j, cy; for(i = 0; i < a.ln; i ++){ for(j = cy = 0; j < b.ln || cy > 0; j ++, cy /= base) { if(j < b.ln) cy += a.v[i] * b.v[j]; if(i + j < res.ln) cy += res.v[i + j]; if(i + j >= res.ln) res.v[res.ln ++] = cy % base; else res.v[i + j] = cy % base; } } return res; } int digits(bint& a) { if(a.ln == 0) return 0; int l = (a.ln - 1) * 4; for(int t = a.v[a.ln - 1]; t; ++ l, t /= 10); return l; } bool read(bint& b, char buf[]) { if(1 != scanf("%s", buf)) return 0; int w, u, ln = strlen(buf); memset(&b, 0, sizeof(bint)); if('0' == buf[0] && 0 == buf[1]) return 1; for(w = 1, u = 0; ln; ) { u += (buf[-- ln] - '0') * w; if(w * 10 == base){ b.v[b.ln ++] = u; u = 0; w = 1; } else w *= 10; } if(w != 1) b.v[b.ln ++] = u; return 1; } void write(const bint & b){ int i; printf("%d", b.ln == 0 ? 0 : b.v[b.ln - 1]); for(i = b.ln - 2; i >= 0; i --) printf("%04d", b.v[i]); printf("\n"); } int main() { char op[5]; while(true) { if(!read(a, buf)) break; scanf("%s", op); memset(buf, 0, sizeof buf); read(b, buf); memset(&c, 0, sizeof(bint)); if('+' == op[0]) { c = a + b; } else if('-' == op[0]) { c = a - b; } else if('*' == op[0]) { c = a * b; } write(c); } return 0; }
G:
// n * x - m * y = 1; d = gcd(n, m); #include<stdio.h> #include<string.h> #include<stdlib.h> typedef long long LL; LL n, m, x, y; LL extgcd( LL a, LL b, LL &x, LL &y) { if( b == 0) { x = 1; y = 0; return a;} LL d = extgcd( b, a % b, x, y); LL t = x; x = y; y = t - a / b * y; return d; } int main() { while(scanf("%lld%lld", &n, &m) == 2) { LL d = extgcd(n, m, x, y); LL t = 1; if( t % d != 0) { printf( "No Solution.\n"); } else{ x = t / d * x; x = ( x % (m / d) + (m / d)) % (m / d); printf( "%lld\n", x) ; } } return 0; }
I:
#include<stdio.h> #include<math.h> const double eps = 1e-8; double x1, yy1, x2, y2, x3, y3, x4, y4; double k1, k2, b1, b2; double x, y; int main() { while(scanf("%lf%lf%lf%lf", &x1, &yy1, &x2, &y2) != EOF) { scanf("%lf%lf%lf%lf", &x3, &y3, &x4, &y4); if(fabs(x1 - x2) < eps || fabs(x3 - x4) < eps) { if(fabs(x1 - x2) < eps && fabs(x3 - x4) < eps) { printf("No Solution!\n"); continue; } if(fabs(x1 - x2) < eps) { k2 = (y3 - y4) / (x3 - x4); b2 = y3 - k2 * x3; y = x1 * k2 + b2; x = x1; } if(fabs(x3 - x4) < eps) { k1 = (yy1 - y2) / (x1 - x2); b1 = yy1 - k1 * x1; y = x3 * k1 + b1; x = x3; } printf("%.6f %.6f\n", x, y); continue; } else { k1 = (yy1 - y2) / (x1 - x2); b1 = yy1 - k1 * x1; k2 = (y3 - y4) / (x3 - x4); b2 = y3 - k2 * x3; if(fabs(k1 - k2) < eps) { printf("No Solution!\n"); continue; } x = (b2 - b1) / (k1 - k2); y = k1 * x + b1; printf("%.6f %.6f\n", x, y); } } return 0; }