cafelier のSRM参加記録です。コンテスト中に考えてたことを執拗に全部書き残すとどうなるだろうかという試み本番中にこういうコードが書きたかったなあ、という後で書いた反省コードを書き残す試み
スパムが来たのでしばらくコメント欄をはてなユーザ限定にしています、すみません、
その他 | |
CodeCraft 2010 : ooo-oo-o, 7位。Username は catbat
#include <iostream> using namespace std; typedef long long LL; int naiveCount(LL A, LL B, LL C, LL from, LL to) { int cnt = 0; for(LL D=from; D<=to; ++D) { LL G = ((D-A)*B + C/2)/C; LL D2 = (G*C + B/2)/B + A; if( D2 == D ) ++cnt; } return cnt; } int main() { for(LL A,B,C,N; cin>>A>>B>>C>>N; ) { LL LOOP = C; //lcm(C,B); LL Z = (N+1-A) % LOOP; LL X = (N+1-A) / LOOP; LL a = naiveCount(A,B,C, A, A+Z-1); LL b = naiveCount(A,B,C, A+Z, A+Z+LOOP-1); cout << a+b*X << endl; } }
(A 0)^i (1) (0 1) (0)
(A 0)^i (1) (1 1) (0)
(A 0 0)^N (1) (1 1 0) (0) (0 1 1) (0)
#include <iostream> #include <vector> using namespace std; typedef long long LL; static const LL MODVAL = 10000; LL ADD(LL x, LL y) { return (x+y)%MODVAL; } LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } LL MUL(LL x, LL y) { return (x*y)%MODVAL; } LL POW(LL x, LL e) { LL v = 1; for(;e;x=MUL(x,x),e>>=1) if(e&1) v = MUL(v, x); return v; } vector< vector<LL> > MATMUL(vector< vector<LL> >& a, vector< vector<LL> >& b) { int N = a.size(); vector< vector<LL> > c(N, vector<LL>(N)); for(int i=0; i<N; ++i) for(int j=0; j<N; ++j) for(int k=0; k<N; ++k) c[i][j] = ADD(c[i][j], MUL(a[i][k],b[k][j])); return c; } LL GEO(LL x_, LL e) { // x^0 + x^1 + ... x^e-1 // (x 0)e (1) // (1 1) (0) vector< vector<LL> > v(2, vector<LL>(2)); vector< vector<LL> > x(2, vector<LL>(2)); v[0][0] = v[1][1] = 1; x[0][0] = x_; x[0][1] = 0; x[1][0] = 1 ; x[1][1] = 1; for(;e;x=MATMUL(x,x),e>>=1) if(e&1) v = MATMUL(v, x); return v[1][0]; } LL HYP(LL x_, LL e) { // e x^0 + (e-1) x^1 + ... + 1 x^e-1 //= // GEO(x,1) + GEO(x,2) + ... + GEO(x,e) // (x 0 0)e (1) // (1 1 0) (0) // (0 1 1) (0) vector< vector<LL> > v(3, vector<LL>(3)); vector< vector<LL> > x(3, vector<LL>(3)); v[0][0] = v[1][1] = v[2][2] = 1; x[0][0] = x_; x[0][1] = 0; x[0][2] = 0; x[1][0] = 1 ; x[1][1] = 1; x[1][2] = 0; x[2][0] = 0 ; x[2][1] = 1; x[2][2] = 1; e++; for(;e;x=MATMUL(x,x),e>>=1) if(e&1) v = MATMUL(v, x); return v[2][0]; } int main() { int nCase; cin>>nCase; while( nCase-- ) { LL A, B, C, N; cin >> A >> B >> C >> N; LL total = ADD( POW(A, N), ADD( MUL(C, GEO(A,N)), MUL(B, HYP(A,N)) ) ); cerr << total << endl; } }
import java.io.*; import java.util.*; import java.math.*; public class Main { public static void main(String[] arg) throws Exception { StreamTokenizer tok = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); while( tok.nextToken() != StreamTokenizer.TT_EOF ) { int a = (int)tok.nval; tok.nextToken(); int b = (int)tok.nval; tok.nextToken(); int n = (int)tok.nval; int last = 2>n ? 2 : n; BigInteger[] poly = new BigInteger[last+1]; for(int i=0; i<=last; ++i) poly[i] = BigInteger.ZERO; poly[n] = BigInteger.ONE; while( last >= 3 ) { poly[last-3] = poly[last-3].subtract( poly[last].multiply(BigInteger.valueOf(b)) ); poly[last-2] = poly[last-2].subtract( poly[last].multiply(BigInteger.valueOf(a)) ); --last; } BigInteger v = poly[0].multiply(BigInteger.valueOf(3)).subtract( poly[2].multiply( BigInteger.valueOf(2) ).multiply( BigInteger.valueOf(a) ) ); System.out.println(v); } } }
#include <iostream> #include <cmath> #include <vector> using namespace std; typedef long long LL; LL MODVAL; LL ADD(LL x, LL y) { return (x+y)%MODVAL; } LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } LL MUL(LL x, LL y) { return (x*y)%MODVAL; } LL POW(LL x, LL e) { LL v = 1; for(;e;x=MUL(x,x),e>>=1) if(e&1) v = MUL(v, x); return v; } vector< vector<LL> > MATMUL(vector< vector<LL> >& a, vector< vector<LL> >& b) { int N = a.size(); vector< vector<LL> > c(N, vector<LL>(N)); for(int i=0; i<N; ++i) for(int j=0; j<N; ++j) for(int k=0; k<N; ++k) c[i][j] = ADD(c[i][j], MUL(a[i][k],b[k][j])); return c; } LL GEO(LL x_, LL e) { vector< vector<LL> > v(2, vector<LL>(2)); vector< vector<LL> > x(2, vector<LL>(2)); v[0][0] = v[1][1] = 1; x[0][0] = x_; x[0][1] = 0; x[1][0] = 1 ; x[1][1] = 1; for(;e;x=MATMUL(x,x),e>>=1) if(e&1) v = MATMUL(v, x); return v[1][0]; } LL HYP(LL x_, LL e) { vector< vector<LL> > v(3, vector<LL>(3)); vector< vector<LL> > x(3, vector<LL>(3)); v[0][0] = v[1][1] = v[2][2] = 1; x[0][0] = x_; x[0][1] = 0; x[0][2] = 0; x[1][0] = 1 ; x[1][1] = 1; x[1][2] = 0; x[2][0] = 0 ; x[2][1] = 1; x[2][2] = 1; e++; for(;e;x=MATMUL(x,x),e>>=1) if(e&1) v = MATMUL(v, x); return v[2][0]; } LL countSame( LL a, LL b, LL k, LL v ) { LL tk = POW(10,k); return ADD( MUL(v, POW(tk, b-a+1)), ADD( MUL(a%MODVAL, GEO(tk, b-a+1)), SUB(HYP(tk, b-a+1), GEO(tk,b-a+1)) ) ); } int main() { for(LL a,b,p; cin>>a>>b>>p; ) { MODVAL = p; LL v = 0; LL k = 1; LL lo = 1; unsigned long long hi = 10; while( k <= 19 ) { if( a<hi && lo<=b ) v = countSame( max(a,lo), min<unsigned long long>(b,hi-1), k, v ); k += 1; lo *= 10; hi *= 10; } cerr << v << endl; } }
#include <iostream> #include <cmath> #include <complex> #include <algorithm> using namespace std; typedef complex<double> CMP; int solve( const vector<CMP>& p ) { const int N = p.size(); int best = 2; for(int i=0; i<N; ++i) for(int j=i+1; j<N; ++j) { CMP A = p[i]; CMP B = p[j]; double na = norm(A); double nb = norm(B); bool hor = (abs(sin(arg(B-A))) < 0.5); double Xs[401]; int XsN = 0; for(int k=j+1; k<N; ++k) { CMP C = p[k]; double D = 2 * (A.real()*(B.imag()-C.imag()) +B.real()*(C.imag()-A.imag()) +C.real()*(A.imag()-B.imag())); if( abs(D)<1e-20 ) continue; double nc = norm(C); double Xx = na*(B.imag()-C.imag()) + nb*(C.imag()-A.imag()) + nc*(A.imag()-B.imag()); double Xy = na*(C.real()-B.real()) + nb*(A.real()-C.real()) + nc*(B.real()-A.real()); Xs[XsN++] = (hor ? Xy : Xx)/D; } sort(Xs+0, Xs+XsN); int runLength=0; for(int x=0; x<XsN; ++x) { if( x>0 && Xs[x]-Xs[x-1]<1e-9 ) ++runLength; else runLength = 1; best = max(best, 2+runLength); } } return best; } int main() { int nCase; cin>>nCase; while( nCase-- ) { vector<CMP> v; int N; cin >> N; while( N-- ) { double x, y; cin >> x >> y; v.push_back( CMP(x,y) ); } cout << solve(v) << endl; } }
#include <iostream> #include <cmath> #include <vector> #include <complex> #include <algorithm> using namespace std; typedef complex<double> CMP; double outer_prod(const CMP& a, const CMP& b) { return imag(conj(a)*b); } double inner_prod(const CMP& a, const CMP& b) { return real(conj(a)*b); } int ccw(const CMP& a, CMP b, CMP c) { b -= a; c -= a; if( outer_prod(b,c) > 0 ) return +1; // counter clockwise if( outer_prod(b,c) < 0 ) return -1; // clockwise if( inner_prod(b,c) < 0 ) return +2; // c--a--b on line if( norm(b) < norm(c) ) return -2; // a--b--c on line return 0; } bool byX( const CMP& a, const CMP& b ) { if( a.real() != b.real() ) return a.real() < b.real(); return a.imag() < b.imag(); } vector<CMP> convex_hull( vector<CMP> p ) { #define IS_RIGHT <0 // skip on-line verts //#define IS_RIGHT ==-1 // take all sort(p.begin(), p.end(), &byX); vector<CMP> ans; for(int i=0; i<p.size(); ans.push_back(p[i++])) // left-to-right while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1], p[i]) IS_RIGHT ) ans.pop_back(); if( ans.size() == p.size() ) return ans; for(int i=p.size()-2; i>=0; ans.push_back(p[i--])) // right-to-left while( ans.size()>=2 && ccw(ans[ans.size()-2], ans[ans.size()-1], p[i]) IS_RIGHT ) ans.pop_back(); ans.pop_back(); return ans; } inline bool is_in( const CMP& p, const CMP& q0, const CMP& q1 ) { return arg(p-q0) > arg(q1-q0); } int main() { int nCase; cin>>nCase; while( nCase-- ) { int N, M; cin >> N >> M; vector<CMP> v[5]; for(int i=0; i<N; ++i) { double x, y; cin >> x >> y; if(x>0&&y>0) v[1].push_back( CMP(x,y) ); if(x<0&&y>0) v[2].push_back( CMP(-x,y) ); if(x<0&&y<0) v[3].push_back( CMP(-x,-y) ); if(x>0&&y<0) v[4].push_back( CMP(x,-y) ); } vector<double> a[5]; vector<CMP> r[5]; for(int k=1; k<=4; ++k) if( v[k].size() >= 3 ) { v[k] = convex_hull( v[k] ); v[k].push_back( v[k][0] ); a[k].assign(1, 0); r[k].assign(1, v[k][0]); for(int i=0; v[k][i].imag()>v[k][i+1].imag(); ++i) { double a0 = arg( v[k][i] - v[k][i+1] ); a[k].push_back(a0); r[k].push_back(v[k][i+1]); if( v[k][i+1].imag() <= v[k][i+2].imag() ) { a[k].push_back( 1e+300 ); r[k].push_back( v[k][i+1] ); } } } while( M-- ) { double x0,y0,x1,y1; cin >> x0 >> y0 >> x1 >> y1; double x = x0==0 ? x1 : x0; double y = y0==0 ? y1 : y0; if( x==0 || y==0 ) { cout << "N" << endl; continue; } int k = -999999; if(x>0&&y>0) k=1; if(x<0&&y>0) k=2; if(x<0&&y<0) k=3; if(x>0&&y<0) k=4; x = abs(x); y = abs(y); CMP q0(x,0); CMP q1(0,y); if( v[k].size() <= 2 ) { bool in = false; for(int i=0; i!=v[k].size(); ++i) if( is_in(v[k][i], q0, q1) ) in = true; cout << (in ? "Y" : "N") << endl; } else { double ag = arg(q1-q0); vector<double>::iterator it = lower_bound( a[k].begin(), a[k].end(), ag ); --it; CMP p = r[k][it - a[k].begin()]; cout << (is_in(p, q0, q1) ? "Y" : "N") << endl; } } } }
presented by cafelier/k.inaba under