이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int ldir[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int cdir[8] = { 0, 1, 1, 1, 0, -1, -1, -1};
int ans;
const int NMAX = 1e4;
bitset<NMAX> isCoprime[NMAX]; // isc[i][j] = gcd(i, j)
bool gcd(int a, int b) {
// cout << a + 1 << ' ' << b + 1 << endl;
if(a < b) swap(a, b);
while(a >= NMAX) {
if(b == 0)
return false;
int r = a % b;
a = b;
b = r;
}
return isCoprime[a][b];
}
void generateFractal(int l, int c, int dir, int size) {
if(size == 0)
return;
//cout << "tree : " << endl;
for(int i = 0; i < size; i++) {
l += ldir[dir];
c += cdir[dir];
ans += gcd(l, c);
}
//cout << endl;
int dir1 = (dir - 1 + 8) % 8, dir2 = (dir + 1) % 8;
int l1 = l, c1 = c;
//cout << dir1 << " Branch 1 : " << endl;
for(int i = 0; i < size / 2; i++) {
l1 += ldir[dir1];
c1 += cdir[dir1];
ans += gcd(l1, c1);
}
// cout << dir2 << " Branch 2 : " << endl;
int l2 = l, c2 = c;
for(int i = 0; i < size / 2; i++) {
l2 += ldir[dir2];
c2 += cdir[dir2];
ans += gcd(l2, c2);
}
// cout << endl << endl;
generateFractal(l1, c1, (dir1 - 1 + 8) % 8, size / 2);
generateFractal(l1, c1, (dir1 + 1 + 8) % 8, size / 2);
generateFractal(l2, c2, (dir2 - 1 + 8) % 8, size / 2);
generateFractal(l2, c2, (dir2 + 1 + 8) % 8, size / 2);
}
int main() {
int m, n, f;
cin >> m >> n >> f;
ans = 0;
for(int i = 0; i < NMAX; i++)
isCoprime[0][i] = 0, isCoprime[i][0] = 0;
isCoprime[0][1] = isCoprime[1][0] = 1;
for(int i = 1; i < NMAX; i++) {
for(int j = 1; j <= i; j++) {
if ( i - j < j ) {
isCoprime[i][j] = isCoprime[j][i - j];
} else {
isCoprime[i][j] = isCoprime[i - j][j];
}
}
}
for(int i = 0; i < f; i++) {
int l, c, size;
cin >> l >> c >> size;
l--;
c--;
ans += gcd(l, c);
generateFractal(l, c, 0, size);
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |