Algorithm

パソコン甲子園2010の予選問題を解く

パソコン甲子園は、私自身本来であれば出場資格のある者の筈なのだが、私の通っている高校がたまたま文系一色である故、相方を見付けられない、そもそも過去に出場したためしがないなど、種々の困難に見舞われ、今年は出場する事ができなかった。そこで、す…

TopCoder SRM 483 DIV 2

DIV2においては500点問題が無茶苦茶落ちる回で、うっかりDIV2の1位の人と同じ部屋になったりと個人的にも踏んだり蹴ったりだったが、DIV1においてはtargetのPetrが全問System Test Failedしたり酷い事になっていて話題だったらしい。自分のレートは一応ちょ…

AOJ 0057 The Number of Area

404 Not Found #include <cstdio> int main() { int n; while(~scanf("%d\n", &n)) printf("%d\n", (n * n + n + 2) / 2); return 0; } just implemented a formula that I found on the internet</cstdio>

AOJ 0074 Videotape

404 Not Found #include <cstdio> int main() { int l, m, n; while(~scanf("%d %d %d", &l, &m, &n), ~l && ~m && ~n){ long long total = l * 60LL * 60 + m * 60 + n, remaining = 120 * 60 - total; printf("%02lld:%02lld:%02lld\n", remaining / 60 / 60, rema</cstdio>…

AOJ 0071 Bombs Chain

404 Not Found #include <cstdio> #include <vector> #include <queue> using namespace std; typedef struct {int x, y;} p; char m[8][9]; inline bool valid(int n){return 0 <= n && n < 8;} inline void addqueue(queue<p>& q, int x, int y){ if(valid(x) && valid(y)) if(m[y][x</p></queue></vector></cstdio>…

AOJ 0085 Joseph's Potato

404 Not Found #include <cstdio> #include <vector> #include <algorithm> using namespace std; int main() { int n, m; while(scanf("%d %d\n", &n, &m), n && m){ vector<int> v; for(int i = 1; i <= n; i++) v.push_back(i); vector<int>::iterator it = v.begin(); while(1 < v.size()){ fo</int></int></algorithm></vector></cstdio>…

AOJ 0085 Joseph's Potato

404 Not Found #include <cstdio> #include <vector> #include <algorithm> using namespace std; int main() { int n, m; while(scanf("%d %d\n", &n, &m), n && m){ vector<int> v; for(int i = 1; i <= n; i++) v.push_back(i); vector<int>::iterator it = v.begin(); while(1 < v.size()){ fo</int></int></algorithm></vector></cstdio>…

AOJ 0056 Goldbach's Conjecture

404 Not Found #include <cstdio> int main() { int n; char prime[50001]; for(int i = 0; i <= 50000; i++) prime[i] = 1; prime[0] = 0; prime[1] = 0; for(int i = 2; i <= 50000; i++){ if(!prime[i]) continue; for(int j = i + i; j <= 50000; j += i) prime[</cstdio>…

AOJ 0044 Prime Number II

404 Not Found #include <cstdio> inline int isprime(int n){ for(int i = 2; i <= n / 2; i++){ if(!(n % i)) return 0; } return 1; } int main() { int n; while(~scanf("%d\n", &n)){ for(int i = n - 1; 0 <= i; i--){ if(isprime(i)){ printf("%d ", i); brea</cstdio>…

AOJ 0063 Palindrome

404 Not Found #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string s, t; int cnt = 0; while(cin>>s){ t = s; reverse(t.begin(), t.end()); cnt += s == t; } cout<</algorithm></string></iostream>

AOJ 0502 Dice

404 Not Found #include <iostream> #include <string> using namespace std; class Dice { public: int top, bottom, west, east, south, north; Dice(){ top = 1;bottom = 6; west = 4;east = 3; south = 2;north = 5; } void North(){ int tmp = top; top = south; south = </string></iostream>…

AOJ 0014 Integral

404 Not Found #include <cstdio> int main() { int d; while(~scanf("%d\n", &d)){ long long total = 0; for(int i = d; i < 600; i += d) total += (long long)i * i * d; printf("%lld\n", total); } return 0; }</cstdio>

AOJ 0019 Factorial

404 Not Found #include <cstdio> int main() { int n; scanf("%d\n", &n); long long m = n; while(--n) m *= n; printf("%lld\n", m); return 0; }</cstdio>

AOJ 0100 Sale Result

404 Not Found #include <cstdio> #include <vector> using namespace std; int main() { long long n; while(scanf("%Ld\n", &n), n){ vector<pair<long long, long long> > v; for(long long i = 0; i < n; i++){ long long p, q, r, f = 1; scanf("%Ld %Ld %Ld\n", &p, &q, &r); for(long long j = 0;</pair<long></vector></cstdio>…

AOJ 0500 Card Game

404 Not Found #include <cstdio> int main() { int n; while(scanf("%d\n", &n), n){ int scr_a = 0, scr_b = 0; for(int i = 0; i < n; i++){ int cur_scr_a, cur_scr_b; scanf("%d %d\n", &cur_scr_a, &cur_scr_b); if(cur_scr_a == cur_scr_b){ scr_a += cur_scr</cstdio>…

SRM 481 DIV 2

また0点…どんどん緑コーダーが遠ざかっていく… しかし、さっき手元でデバッグしていたら、本当の恐怖はそんなモンではなかった… 250点問題(誤答) #include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #in</memory></iostream></set></utility></map></cfloat></climits></cmath></cstdlib></cstdio>…

PKU 1012 Joseph

1012 -- Joseph #include <cstdio> int main() { int k , _n; int r[14]; for(int i = 0; i < 14; i++) r[i] = 0; while(scanf("%d", &k), k){ if(!r[k]){ _n = k * 2; for(int i = k; ; i++){ int n = _n, p = 0; while(n != k){ p = (p + i) % n--; if(p < k) goto</cstdio>…

最大の4つ子素数を求める

#include <cstdio> int main() { int n; scanf("%d\n", &n); if(!n) return 0; char *c = new char[n + 1]; for(int i = 0; i <= n; i++) c[i] = 1; c[0] = c[1] = 0; for(int i = 2; i <= n; i++){ if(!c[i]) continue; for(int j = i + i; j <= n; j += i) c[j] = </cstdio>…

TopCoder SRM 480で大敗北

250点問題と500点問題そこそこの速度でサブミットしてよっしゃあって思ってたら2問ともチャレンジされてしかも成功された。 0点だった。レートがグレーになってしまった。ショックだ。一応自分の誤答をはってみる。 250 #include <cstdio> #include <cstdlib> #include <cmath> #incl</cmath></cstdlib></cstdio>…

アルゴリズムC++ P.16 練習問題 1

#include <iostream> #include <algorithm> using namespace std; int gcd_new(int u, int v) { while(u > 0){ if(u < v) swap(u, v); u %= v; } return v; } int gcd_old(int u, int v) { int t; while(u > 0){ if(u < v) swap(u, v); u -= v; } return v; } int main() { int x,</algorithm></iostream>…

AOJ 0045 Sum and Average

404 Not Found #include <cstdio> int main() { int m, n; int total = 0, ave = 0, row = 0; while(~scanf("%d,%d\n", &m, &n)){ total += m * n; ave += n; row++; } printf("%d\n%d\n", total, (int)(ave / (double)row + 0.5)); return 0; } こういう簡単な問題</cstdio>…

AOJ 0050 Apple and Peach

404 Not Found #include <iostream> #include <vector> #include <string> using namespace std; int main() { string s; vector<int> pe, ap; int n; getline(cin, s); pe.push_back(0); ap.push_back(0); while(n=s.find("peach", pe.back() + 5), ~n) pe.push_back(n); while(n=s.find("a</int></string></vector></iostream>…

AOJ 0064 Secret Number

404 Not Found #include <cstdio> int main(){ int n = 0, sum = 0; char c; while(c = getchar(), ~c){ if('0' <= c && c <= '9'){ n *= 10;n += c - '0'; }else{ sum += n;n = 0; } } printf("%d\n", sum); return 0; }</cstdio>

AOJ 0067 The Number of Island

404 Not Found #include <cstdio> char m[14][14]; int fillaround(int x, int y){ if(!m[x][y]) return 0; else m[x][y] = 0; if(m[x - 1][y + 0]) fillaround(x - 1, y + 0); if(m[x + 1][y + 0]) fillaround(x + 1, y + 0); if(m[x + 0][y - 1]) fillaround(x - 0</cstdio>…

AOJ 0501 Data Conversion

404 Not Found #include <cstdio> #include <map> using namespace std; int main() { int n; while(scanf("%d\n", &n), n){ map<char, char> m; char a, b; for(int i = 0; i < n; i++){ scanf("%c %c\n", &a, &b); m[a] = b; } scanf("%d\n", &n); for(int i = 0; i < n; i++){ scan</char,></map></cstdio>…

AOJ 0084 Search Engine

404 Not Found #include <cstdio> #include <vector> #include <string> using namespace std; int main() { vector<string> v,w; string s(""); char c; while(c = getchar(), ~c){ switch(c){ case ' ': case ',': case '.': if(s != "") v.push_back(s); s = c; v.push_back(s); s = ""; b</string></string></vector></cstdio>…

AOJ 0046 Differential

404 Not Found #include <vector> #include <cstdio> #include <algorithm> using namespace std; int main() { double n; vector<double> v; while(~scanf("%lf", &n)) v.push_back(n); sort(v.begin(), v.end()); printf("%.1lf\n", *v.rbegin() - *v.begin()); return 0; }</double></algorithm></cstdio></vector>

AOJ 0077 Run Length

404 Not Found #include <cstdio> #include <cstring> int main() { char s[101]; while(fgets(s, 101, stdin)){ for(int i = 0, l = strlen(s); i < l; i++){ if(s[i] == '@'){ for(int j = 0; j < s[i + 1] - '0'; j++) putchar(s[i + 2]); i += 2; }else{ putchar(s[i]); }</cstring></cstdio>…

AOJ 0051 Differential II

404 Not Found #include <cstdio> #include <vector> #include <cstdlib> #include <algorithm> using namespace std; int main() { int n; char s[9]; scanf("%d\n", &n); for(int i = 0; i < n; i++){ int max, min; vector<int> v; scanf("%8s", s); for(int j = 0; j < 8; j++) v.push_back(s[j] -</int></algorithm></cstdlib></vector></cstdio>…

AOJ 0028 Mode Value

404 Not Found #include <cstdio> #include <map> #include <vector> #include <algorithm> using namespace std; int main() { map <int, int> m; map <int, vector<int> > l; int n; while(~scanf("%d\n", &n)) m[n]++; for(map<int, int>::iterator it = m.begin(); it != m.end(); it++) l[(*it).second].push_back((*i…</int,></int,></int,></algorithm></vector></map></cstdio>