TopCoderã®ã¹ã¿ãã¯ãµã¤ãºã®å¢ããæ¹
ãªãã»ã©â¦â¦ã試ãã¦ã¿ããã
#include <string> #include <vector> #include <utility> #include <set> #include <cstdlib> using namespace std; set<pair<int,int> > S; void f1(int a, int b, int x, int y) { if (a==0 || b==0) { S.insert(make_pair(x,y)); return; } S.insert(make_pair(a,b)); if (a<b) f1(a, b-a, x, y); else f1(a-b, b, x, y); } int f2(int c, int d) { if (c==0 || d==0) return -1; if (S.count(make_pair(c,d)) > 0) return c+d; return c<d ? f2(c, d-c) : f2(c-d, d); } class PairGame{public: int maxSum( int a, int b, int c, int d ) { f1(a, b, 1234567, 1234567); return f2(c, d); }};
ãã®ããã°ã©ã ã¯ãTopCoder SRM 620 Div1 Easy PairGameãå帰ã§æ¸ãããã®ãf1ã®å¼æ°ã®x,yãæ¶ãã°éããã©ãå¼æ°ãå¢ããã¨ã¹ã¿ãã¯ã®æ¶è²»éãå¢ãã¦ã¹ã¿ãã¯ãªã¼ãã¼ããã¼ã§è½ã¡ããåã«å¼æ°ã追å ããã ãã ã¨æé©åã§æ¶ãã¦ãã¾ã£ãã®ã§ãæå¾ã«æå³ããªãSã«è¿½å ãã¦ããã
int a, b, c, d, r; long long esp_org, esp_new; class PairGame{public: int maxSum( int a, int b, int c, int d ) { // æ°ããã¹ã¿ãã¯é åã確ä¿ãã const int size = 128*1024*1024; void *p = malloc(size); esp_new = (long long)p + size - 1; // ã¹ã¿ãã¯ä¸ã®å¤æ°ãå¾ é¿ ::a = a, ::b = b, ::c = c, ::d = d; // ã¹ã¿ãã¯ãç½®ãæãã __asm__("mov %rsp, esp_org"); __asm__("mov esp_new, %rsp"); // ã¡ã¤ã³ã®å¦ç f1(::a, ::b, 1234567, 1234567); r = f2(::c, ::d); // ã¹ã¿ãã¯ãå ã«æ»ã __asm__("mov esp_org, %rsp"); return r; }};
ãã®ããã«æ¸ãæãããéã£ããç°¡å(ï½¥âï½¥)ã次ã®ã¨ããã§ããã£ãã
- 64ããããªã®ã§ãespã§ã¯ãªãrspã使ããC++å´ã®å¤æ°ã64ãããã«ããå¿ è¦ããã
- rspã¯ç¢ºä¿ããé åã®å é ã§ã¯ãªããæ«å°¾ãæãããã«ããå¿ è¦ãããï¼ã¹ã¿ãã¯ã¯ä¸ä½ããä¸ä½ã«ä½¿ã£ã¦ããã®ã§ï¼
å帰ãæ·±ããªããããå ´åãå帰ã使ããªãããã«æ¸ãã¦ããããã©ãããã¾ã§ç°¡åã«ã§ãããªãã°ã¹ã¿ãã¯ãµã¤ãºãå¢ããã®ã¯ãããããããªãã
Google Code Jam Round 1B 2014
A. The Repeater
æååã®çµãä¸ããããã¨ããaâaaãaaâaã¨ããæä½ãç¹°ãè¿ãã¦ãå ¨ã¦ã®æååãçããããæå°ææ°ã¯ï¼
åæåãã¨ã«ä½æåã«æããã®ããå ¨æ¢ç´¢ã
â»âãã®ã³ã¼ãã¯Largeè½ã¡ããæåºãã¹ã ã£ãã
def minify(s): r = "" for i in range(len(s)): if i==0 or s[i-1]!=s[i]: r += s[i] return r def toarray(s): a = [] for i in range(len(s)): if i==0 or s[i-1]!=s[i]: a += [1] else: a[-1] += 1 return a def solve(S): if len(set(minify(s) for s in S)) > 1: return "Fegla Won" A = [toarray(s) for s in S] ans = 0 for p in range(len(A[0])): a = 99999999 for x in range( min(a[p] for a in A), max(a[p] for a in A)+1): a = min(a, sum(abs(a[p]-x) for a in A)) ans += a return ans for i in range(input()): N = input() S = [raw_input() for _ in range(N)] a = solve(S) print "Case #%s: %s" % (i+1, a)
B. New Lottery Game
æ£æ°A, B, Kã«å¯¾ãã¦ãa
xãXããå°ããã¨ã¯ãXã®iãããç®ã1ã§ãxã®iãããç®ã0ã§ãiããä¸ä½ã®ãããã§ã¯Xã¨xãçããã¨ããiãåå¨ãããã¨ã§ãããA, B, Kããããã«å¯¾ãã¦ãã®ãããªãããã®ä½ç½®ã決ãããããæºãã(a,b)ãä½éãããããæ°ãããå ´ååããç ©éã«ãªã£ã¦ãã¾ã£ãããGCJã§ã¯ã¾ãã¯ãã¤ã¼ããªããã°ã©ã ã§SmallãéããLargeã®ããã°ã©ã ã¯Smallã®å ¥åºåã§ãã¹ãããã¨ããæã使ããã
Small
def solve(A, B, K): r = 0 for a in range(A): for b in range(B): if (a&b)<K: r += 1 return r for t in range(input()): A, B, K = map(int, raw_input().split()) print "Case #%s: %s" % (t+1, solve(A,B,K))
Large
def solve(A, B, K): r = 0 for a in range(32): if A>>a&1: for b in range(32): if B>>b&1: for k in range(32): if K>>k&1: t = 1 for i in range(32): if i<k: if i<a and i<b: t *= 4 elif i<a or i<b: t *= 2 else: t *= 1 else: x = K>>i&1 if i==k: x = 0 if i<a and i<b: t *= [3, 1][x] elif i<a or i<b: if i<a: y = 0 if i==b else B>>i&1 else: # i<b y = 0 if i==a else A>>i&1 if x==0 and y==0: t *= 2 elif x==0 and y==1: t *= 1 elif x==1 and y==0: t *= 0 elif x==1 and y==1: t *= 1 else: y = (0 if i==a else A>>i&1) & (0 if i==b else B>>i&1) if x==y: t *= 1 else: t *= 0 r += t return r for t in range(input()): A, B, K = map(int, raw_input().split()) print "Case #%s: %s" % (t+1, solve(A,B,K))
C. The Bored Traveling Salesman
åé¡æã®æ¡ä»¶ãåããã¥ãããã©ãè¦ã¯ã
ã°ã©ãã®å ¨åæ¨ã辿ãè¡ãããé ã§åãã¼ãã«ä¸ããããæååãé£çµãããè¾æ¸é æå°ã®æååãçãã
ã©ã®ãã¼ããæ ¹ã¨ãã¦ãå½ç¶å ¨åæ¨ã¯å¾ãããã®ã§ãæ ¹ã¯æååãä¸çªå°ãããã¼ããé¸ã¹ã°è¯ããè¾æ¸é æå°ã®æååãçããã®ã§ããã¨ã¯ç§»åå¯è½ãªãã¼ãã®ãã¡æååãæå°ã®ãã®ãé¸ãã§è¡ãã°è¯ãã移åå¯è½ãªãã¼ãã¨ã¯ãç¾å¨å°ç¹ããã®å¸°ãéã®éä¸ãã移åã§ãã¦ããã®ãã¼ãã«ç§»åããã¨ãã¦ãæ®ãã®å ¨ã¦ã®ãã¼ãã辿ãããã®ãæ®ãå ¨ã¦ã®ãã¼ãã辿ãããã©ããã¯ãããã¾ã§ã«éã£ããã¼ãã®ãã¡å¸°ãé以å¤ã®ãã®ãå ¨ã¦åé¤ãã¦ãã°ã©ããé£çµãã©ããã調ã¹ãã°è¯ãã
import itertools def solve(N, Z, F): p = Z.index(min(Z)) ans = Z[p] S = [p] V = [False]*N V[p] = True for _ in range(N-1): next = -1 for p in range(N): if V[p]: continue s = S[:] while len(s)>0 and not F[s[-1]][p]: s.pop() if len(s)==0: continue s += [p] v = V[:] c = [False]*N while len(s)>0: q = s.pop() v[q] = True for i in range(N): if F[q][i] and not v[i]: s += [i] c[q] = True ok = True for i in range(N): if not v[i] and not V[i]: ok = False if ok and (next<0 or Z[p]<Z[next]): next = p ans += Z[next] while not F[S[-1]][next]: S.pop() S += [next] V[next] = True return ans for t in range(input()): N, M = map(int, raw_input().split()) Z = [raw_input() for _ in range(N)] F = [[False]*N for _ in range(N)] for _ in range(M): a, b = map(lambda x: int(x)-1, raw_input().split()) F[a][b] = F[b][a] = True print "Case #%s: %s" % (t+1, solve(N,Z,F))
SRM614
ãªã¼ãã¼ããã¼ã§Smallãè½ã¨ããorz
MinimumSquare
æ£æ¹å½¢ã®ä½ç½®ã¨ãµã¤ãºãä¸ããããç¹ãã決ãã¦å«ã¾ããç¹ã®åæ°ã調ã¹ãããµã¤ãºãäºåæ¢ç´¢ãããã¨ã§é«éåã
#include <string> #include <vector> #include <algorithm> #include <climits> using namespace std; class MinimumSquare{public: long long minArea( vector <int> x, vector <int> y, int K ) { int n = (int)x.size(); long long ans = LLONG_MAX; for (int xi=0; xi<n; xi++) for (int yi=0; yi<n; yi++) { long long x0 = x[xi]-1; long long y0 = y[yi]-1; vector<long long> s; for (int i=0; i<n; i++) { if (x[i]+1>x0) s.push_back(x[i]+1-x0); if (y[i]+1>y0) s.push_back(y[i]+1-y0); } sort(s.begin(), s.end()); int l=0; int r=(int)s.size()-1; auto check = [&](int p) -> bool { int c = 0; for (int i=0; i<n; i++) if (x0<x[i] && x[i]<x0+s[p] && y0<y[i] && y[i]<y0+s[p]) c++; return c >= K; }; if (!check(r)) continue; while (r-l>1) { int m = (l+r)/2; (check(m)?r:l) = m; } ans = min(ans, s[r]*s[r]); } return ans; }};
CycleColoring
å°ãããµã¤ã¯ã«ã«ã¯ç¹ç·ã®è¾ºã2æ¬çãã¦ããããã®2æ¬ãåãè²ã«ãªãå ´åã¨ç°ãªãè²ã«ãªãå ´åãä½éãããããæ±ããããã使ã£ã¦çããæ±ãããå°ãããµã¤ã¯ã«ã大ãããµã¤ã¯ã«ããå§ç¹ã¨åãè²ã«ãªãå ´åã¨ç°ãªãè²ã«ãªãå ´åãè¦ãã¦ããã¦åçè¨ç»æ³ã
#include <string> #include <vector> using namespace std; // a^b mod m long long powm(long long a, long long b, long long m) { long long r=1,t=a; for(;b>0;t=t*t%m,b>>=1)if(b&1)r=r*t%m; return r; } class CycleColoring{public: int countColorings( vector <int> vertexCount, vector <int> fromVertex, vector <int> toVertex, int K ) { const int M = 1000000007; int N = (int)vertexCount.size(); vector<long long> S(N); // fromã¨toãåãè²ã«ãªãå ´åã®æ° vector<long long> D(N); // fromã¨toãç°ãªãè²ã«ãªãå ´åã®æ° for (int i=0; i<N; i++) { // type=0ãªãS[i]ããtype=1ãªãD[i]ãè¨ç®ãã for (int type=0; type<2; type++) { long long s = 1; long long d = 0; int n = vertexCount[i]; int dist = (fromVertex[i] - toVertex[(i-1+N)%N] + n) % n; if (dist==0 && type==1) { D[i] = 0; continue; } for (int j=1; j<n; j++) { long long ps = s; long long pd = d; s = pd; d = (ps*(K-1) + pd*(K-2))%M; // toã®ä½ç½®ã0ã¨ãã¦fromã®ä½ç½®ã«ãªã£ããè²ãæ¡ä»¶ãæºãããç¢ºèª if (j==dist) (type==0 ? d : s) = 0; } (type==0 ? S[i] : D[i]) = d; } } long long s = K; long long d = 0; long long Ki = powm(K-1,M-2,M); // 1/(K-1) for (int i=0; i<N; i++) { long long ps = s; long long pd = d; s = (ps*S[i] + pd*D[i]%M*Ki) % M; d = (ps*D[i] + pd*D[i]%M*Ki%M*(K-2)%M + pd*S[i]%M) % M; } return (int)s; }};
SRM585 Div2 Medium(500) TrafficCongestionDivTwo
class TrafficCongestionDivTwo: def theMinCars(self, treeHeight): T = [1,1] while len(T)<=treeHeight: T += [(T[-1]+2*T[-2])] return T[treeHeight]
SRM585 Div2 Easy(250) LISNumberDivTwo
class LISNumberDivTwo: def calculate(self, seq): return sum(seq[i]>=seq[i+1] for i in range(len(seq)-1))+1
SRM585 Div1 Medium(500) LISNumber
åçè¨ç»æ³ãå°ããé ã«næã®ã«ã¼ããé¸ãã ã¨ããa[i]â§a[i+1]ã¨ãªãiãã¡ããã©kåã«ãªã並ã¹æ¹ã®æ°ãè¦ãã¦ãããããã¯åãæ°åã®ã«ã¼ããåºå¥ããå ´åã®æ°ãªã®ã§ãæå¾ã«æ°åãã¨ã«ãã®æ°åã®ã«ã¼ãã®ææ°ã®éä¹ã§å²ããæ³Mãç´ æ°ãªãã°ã1/x = xM-2ã«ãªããPythonãªãã°ãpow(x,M-2,M)ã§é«éã«è¨ç®ã§ããã
class LISNumber: def count(self, cardsnum, K): M = 1000000007 S = sum(cardsnum) if K>S: return 0 T = [1]+[0]*S l = 0 for c in cardsnum: for i in range(c): P = T T = [0]*(S+1) for k in range(S): T[k] = (T[k]+P[k]*(k-i+1))%M T[k+1] = (T[k+1]+P[k]*(l-k+i))%M l += 1 ans = T[K-1] for c in cardsnum: t = reduce(lambda a,b: a*b%M, range(1,c+1)) ans = ans*pow(t,M-2,M)%M return ans