buggy_code
stringlengths 11
625k
| fixed_code
stringlengths 17
625k
| bug_type
stringlengths 2
4.45k
| language
int64 0
8
| token_count
int64 5
200k
|
|---|---|---|---|---|
#include <iostream>
#define N 10
using namespace std;
/*
Hanafuda Shuffle
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1129&lang=jp
*/
int array[N];
void makearray(int n) {
for (int i = 0; i < N; i++) {
array[i] = n;
if (n > 0)
n -= 1;
}
}
void shuffle(int p, int c) {
int sarray[N];
// copy unshuffled deck
for (int i = p - 1 + c; i < N; i++) {
sarray[i] = array[i];
}
// copy the c part
int j = 0;
for (int i = p - 1; i < p - 1 + c; i++) {
sarray[j] = array[i];
j++;
}
// copy the p part
j = c;
for (int i = 0; i < p - 1; i++) {
sarray[j] = array[i];
j++;
}
for (int i = 0; i < N; i++) {
array[i] = sarray[i];
}
}
int main() {
int n, r, p, c;
while (1) {
cin >> n >> r;
makearray(n);
if (n == 0 && r == 0)
break;
for (int i = 0; i < r; i++) {
cin >> p >> c;
shuffle(p, c);
}
cout << array[0] << endl;
}
}
|
#include <iostream>
#define N 50
using namespace std;
/*
Hanafuda Shuffle
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1129&lang=jp
*/
int array[N];
void makearray(int n) {
for (int i = 0; i < N; i++) {
array[i] = n;
if (n > 0)
n -= 1;
}
}
void shuffle(int p, int c) {
int sarray[N];
// copy unshuffled deck
for (int i = p - 1 + c; i < N; i++) {
sarray[i] = array[i];
}
// copy the c part
int j = 0;
for (int i = p - 1; i < p - 1 + c; i++) {
sarray[j] = array[i];
j++;
}
// copy the p part
j = c;
for (int i = 0; i < p - 1; i++) {
sarray[j] = array[i];
j++;
}
for (int i = 0; i < N; i++) {
array[i] = sarray[i];
}
}
int main() {
int n, r, p, c;
while (1) {
cin >> n >> r;
makearray(n);
if (n == 0 && r == 0)
break;
for (int i = 0; i < r; i++) {
cin >> p >> c;
shuffle(p, c);
}
cout << array[0] << endl;
}
}
|
[["-", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59], ["+", 36, 36, 36, 36, 0, 30, 0, 58, 51, 59]]
| 1
| 290
|
#include <stdio.h>
int n, r, p, c, d[50], e[50];
int main() {
while (true) {
scanf("%d%d", &n, &r);
if (n == 0 && r == 0)
break;
for (int i = 0; i < n; i++)
d[i] = n - i;
for (int i = 0; i < r; i++) {
scanf("%d%d", &p, &c);
for (int j = 0; j < c; j++) {
e[j] = d[j + p];
}
for (int j = c; j < p + c; j++) {
e[j] = d[j - c];
}
for (int j = 0; j < p + c; j++) {
d[j] = e[j];
}
}
printf("%d\n", d[0]);
}
return 0;
}
|
#include <stdio.h>
int n, r, p, c, d[50], e[50];
int main() {
while (true) {
scanf("%d%d", &n, &r);
if (n == 0 && r == 0)
break;
for (int i = 0; i < n; i++)
d[i] = n - i;
for (int i = 0; i < r; i++) {
scanf("%d%d", &p, &c);
p--;
for (int j = 0; j < c; j++) {
e[j] = d[j + p];
}
for (int j = c; j < p + c; j++) {
e[j] = d[j - c];
}
for (int j = 0; j < p + c; j++) {
d[j] = e[j];
}
}
printf("%d\n", d[0]);
}
return 0;
}
|
[["+", 0, 7, 8, 9, 0, 1, 0, 27, 28, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 27, 17, 68], ["+", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35]]
| 1
| 212
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int main() {
int n, r, c, p, ar[50];
while (cin >> n >> r) {
if (n == 0 && r == 0)
return 0;
for (int i = 0; i < n; i++)
ar[i] = n - i;
for (int i = 0; i < r; i++) {
cin >> p >> c;
int t1[50], t2[50];
for (int i = 0; i < p - 1; i++) {
t1[i] = ar[i];
}
for (int i = p - 1; i < p + c - 1; i++) {
t2[i - p + 1] = ar[i];
}
for (int i = 0; i < c; i++) {
ar[i] = t2[i];
}
for (int i = c; i < p + c; i++) {
ar[i] = t1[i - c];
}
}
cout << ar[0] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
int main() {
int n, r, c, p, ar[50];
while (cin >> n >> r) {
if (n == 0 && r == 0)
return 0;
for (int i = 0; i < n; i++)
ar[i] = n - i;
for (int i = 0; i < r; i++) {
cin >> p >> c;
int t1[50], t2[50];
for (int i = 0; i < p - 1; i++) {
t1[i] = ar[i];
}
for (int i = p - 1; i < p + c - 1; i++) {
t2[i - p + 1] = ar[i];
}
for (int i = 0; i < c; i++) {
ar[i] = t2[i];
}
for (int i = c; i < p + c - 1; i++) {
ar[i] = t1[i - c];
}
}
cout << ar[0] << endl;
}
return 0;
}
|
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
| 1
| 247
|
#include <iostream>
#include <utility>
using namespace std;
int main() {
int n, r, p, c;
int card[50];
int ncard[50];
cin >> n >> r;
while (n || r) {
for (int i = 0; i < n; ++i) {
card[i] = i;
ncard[i] = i;
}
for (int i = 0; i < r; ++i) {
cin >> p >> c;
for (int j = 0; j < c; ++j) {
ncard[j] = card[p + j - 1];
}
for (int j = c; j < c + p; ++j) {
ncard[j] = card[j - c];
}
for (int j = c + p; j < n; ++j) {
ncard[j] = card[j];
}
for (int j = 0; j < n; ++j) {
card[j] = ncard[j];
}
}
cout << n - card[0] << endl;
cin >> n >> r;
}
}
|
#include <iostream>
#include <utility>
using namespace std;
int main() {
int n, r, p, c;
int card[50];
int ncard[50];
cin >> n >> r;
while (n || r) {
for (int i = 0; i < n; ++i) {
card[i] = i;
ncard[i] = i;
}
for (int i = 0; i < r; ++i) {
cin >> p >> c;
for (int j = 0; j < c; ++j) {
ncard[j] = card[p + j - 1];
}
for (int j = c; j < c + p - 1; ++j) {
ncard[j] = card[j - c];
}
for (int j = c + p - 1; j < n; ++j) {
ncard[j] = card[j];
}
for (int j = 0; j < n; ++j) {
card[j] = ncard[j];
}
}
cout << n - card[0] << endl;
cin >> n >> r;
}
}
|
[["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 17, 33], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
| 1
| 232
|
#include <iostream>
using namespace std;
int main() {
int a[51] = {};
int st[51] = {};
int n, r;
int p, c;
while (1) {
// in
cin >> n >> r;
if (n == 0 && r == 0)
break;
for (int i = 0; i < n; i++) {
a[i] = n - i;
}
for (int i = 0; i < r; i++) {
cin >> p >> c;
for (int j = 0; j < n; j++) {
st[j] = a[j];
}
for (int j = 0; j < c + 1; j++) {
a[j] = st[p + j - 1];
}
for (int j = 0; j < p; j++) {
a[j + c] = st[j];
}
}
cout << a[0] << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int a[51] = {};
int st[51] = {};
int n, r;
int p, c;
while (1) {
// in
cin >> n >> r;
if (n == 0 && r == 0)
break;
for (int i = 0; i < n; i++) {
a[i] = n - i;
}
for (int i = 0; i < r; i++) {
cin >> p >> c;
for (int j = 0; j < n; j++) {
st[j] = a[j];
}
for (int j = 0; j < c; j++) {
a[j] = st[p + j - 1];
}
for (int j = 0; j < p - 1; j++) {
a[j + c] = st[j];
}
}
cout << a[0] << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 7, 15, 16, 12, 16, 17, 72], ["-", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 17, 33], ["+", 8, 9, 0, 7, 15, 16, 12, 16, 12, 13]]
| 1
| 210
|
#include <bits/stdc++.h>
using namespace std;
#define first fs
#define second se
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
using pii = pair<int, int>;
using vi = vector<int>;
using lint = long long;
const int inf = 1001001001;
const lint linf = 1001001001001001001ll;
const int mod = 1e9 + 7;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
}
return a > b;
}
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
}
return a < b;
}
template <typename T> inline void print(const T &x, char s = '\n') {
cout << x << s;
}
template <typename T> inline void print(const vector<T> &v, string s = " ") {
rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\n" : s);
}
inline bool inside(int y, int x, int H, int W) {
return 0 <= y && y < H && 0 <= x && x < W;
}
inline lint in() {
lint x;
std::cin >> x;
return x;
}
int main() {
int n, r;
while (cin >> n >> r, n) {
vi v;
rep(i, n) v.push_back(n - i);
rep(_, r) {
int p = in(), c = in();
vi beet, ei;
for (int i = p - 1; i < p + c - 1; ++i)
beet.push_back(v[i]);
rep(i, p - 1) beet.push_back(v[i]);
rep(i, p + c) v[i] = beet[i];
}
print(v[0]);
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define first fs
#define second se
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
using pii = pair<int, int>;
using vi = vector<int>;
using lint = long long;
const int inf = 1001001001;
const lint linf = 1001001001001001001ll;
const int mod = 1e9 + 7;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
}
return a > b;
}
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
}
return a < b;
}
template <typename T> inline void print(const T &x, char s = '\n') {
cout << x << s;
}
template <typename T> inline void print(const vector<T> &v, string s = " ") {
rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\n" : s);
}
inline bool inside(int y, int x, int H, int W) {
return 0 <= y && y < H && 0 <= x && x < W;
}
inline lint in() {
lint x;
std::cin >> x;
return x;
}
int main() {
int n, r;
while (cin >> n >> r, n) {
vi v;
rep(i, n) v.push_back(n - i);
rep(_, r) {
int p = in(), c = in();
vi beet, ei;
for (int i = p - 1; i < p + c - 1; ++i)
beet.push_back(v[i]);
rep(i, p - 1) beet.push_back(v[i]);
rep(i, beet.size()) v[i] = beet[i];
}
print(v[0]);
}
}
|
[["-", 31, 69, 28, 2, 3, 4, 0, 16, 31, 22], ["-", 31, 69, 28, 2, 3, 4, 0, 16, 17, 72], ["-", 31, 69, 28, 2, 3, 4, 0, 16, 12, 22], ["+", 28, 2, 3, 4, 0, 2, 63, 118, 28, 22], ["+", 28, 2, 3, 4, 0, 2, 63, 118, 17, 131], ["+", 28, 2, 3, 4, 0, 2, 63, 118, 119, 120], ["+", 28, 2, 3, 4, 0, 2, 3, 4, 0, 24], ["+", 28, 2, 3, 4, 0, 2, 3, 4, 0, 25]]
| 1
| 471
|
#include <iostream>
using namespace std;
#include <vector>
int main() {
while (1) {
int n, r;
cin >> n >> r;
if (n == 0 && r == 0)
break;
vector<int> a(n);
for (int i = 0; i < n; i++) {
a[i] = n - i - 1;
}
for (int i = 0; i < r; i++) {
int p, c;
cin >> p >> c;
for (int j = 0; j < n; j++) {
if (a[j] < p - 1) {
a[j] += c;
} else if (a[j] < p + c) {
a[j] -= (p - 1);
}
}
}
// cout<<"a"<<endl;
for (int i = 0; i < n; i++) {
if (a[i] == 0)
cout << i + 1 << endl;
}
}
return 0;
}
|
#include <iostream>
using namespace std;
#include <vector>
int main() {
while (1) {
int n, r;
cin >> n >> r;
if (n == 0 && r == 0)
break;
vector<int> a(n);
for (int i = 0; i < n; i++) {
a[i] = n - i - 1;
}
for (int i = 0; i < r; i++) {
int p, c;
cin >> p >> c;
for (int j = 0; j < n; j++) {
if (a[j] < p - 1) {
a[j] += c;
} else if (a[j] < p - 1 + c) {
a[j] -= (p - 1);
}
}
// cout<<i<<endl;
}
// cout<<"a"<<endl;
for (int i = 0; i < n; i++) {
if (a[i] == 0)
cout << i + 1 << endl;
}
}
return 0;
}
|
[["+", 15, 339, 51, 16, 12, 16, 31, 16, 17, 33], ["+", 15, 339, 51, 16, 12, 16, 31, 16, 12, 13]]
| 1
| 204
|
#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
using namespace std;
int main() {
int n, r;
while (cin >> n >> r, n | r) {
vector<int> now(100), next(100);
for (int i = 0; i < n; i++) {
now[n - 1 - i] = i + 1;
}
for (int i = 0; i < r; i++) {
int p, c;
cin >> p >> c;
for (int j = 0; j < n; j++) {
if (j < c) {
next[j] = now[p - 1 + j];
} else if (j < c + p) {
next[j] = now[j - c];
} else {
next[j] = now[j];
}
}
now = next;
}
cout << now[0] << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
using namespace std;
int main() {
int n, r;
while (cin >> n >> r, n | r) {
vector<int> now(100), next(100);
for (int i = 0; i < n; i++) {
now[n - 1 - i] = i + 1;
}
for (int i = 0; i < r; i++) {
int p, c;
cin >> p >> c;
for (int j = 0; j < n; j++) {
if (j < c) {
next[j] = now[p - 1 + j];
} else if (j < c + p - 1) {
next[j] = now[j - c];
} else {
next[j] = now[j];
}
}
now = next;
}
cout << now[0] << endl;
}
return 0;
}
|
[["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 33], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
| 1
| 199
|
#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000 // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;
int main(void) {
while (1) {
int n, r;
int p, c;
int a[51] = {};
int b[51] = {};
cin >> n >> r;
if (n == 0 && r == 0) {
break;
}
FOR(i, 1, n) { a[i] = n - i + 1; }
FOR(i, 1, r) {
cin >> p >> c;
FOR(t, 1, p - 1) { b[t] = a[t]; }
FOR(t, 1, c) { a[t] = a[p + t - 1]; }
FOR(t, 1, p - 1) { a[p + t - 1] = b[t]; }
}
cout << a[1] << endl;
}
}
|
#include <bits/stdc++.h>
#define FOR(i, a, b) for (long long int i = (a); i <= (b); i++)
#define RFOR(i, a, b) for (long long int i = (a); i >= (b); i--)
#define MOD 1000000007
#define INF 1000000000 // 2000000000
#define LLINF 1000000000000000000 // 9000000000000000000
#define PI 3.14159265358979
using namespace std;
typedef long long int ll;
typedef pair<long long int, long long int> P;
int main(void) {
while (1) {
int n, r;
int p, c;
int a[51] = {};
int b[51] = {};
cin >> n >> r;
if (n == 0 && r == 0) {
break;
}
FOR(i, 1, n) { a[i] = n - i + 1; }
FOR(i, 1, r) {
cin >> p >> c;
FOR(t, 1, p - 1) { b[t] = a[t]; }
FOR(t, 1, c) { a[t] = a[p + t - 1]; }
FOR(t, 1, p - 1) { a[c + t] = b[t]; }
}
cout << a[1] << endl;
}
}
|
[["-", 31, 69, 341, 342, 0, 16, 31, 16, 31, 22], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 31, 22], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["-", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 241
|
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n; i >= 0; i--)
#define reps(i, m, n) for (int i = m; i < n; i++)
#define repsb(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
int main() {
int n, p, c, r;
while (true) {
cin >> n >> r;
if (n == 0)
return 0;
int b[50];
rep(i, n) { b[i] = n - i; }
rep(s, r) {
int a[50], j;
cin >> p >> c;
for (j = 0; j < c; j++) {
a[j] = b[c + j - 1];
}
for (; j < p + c - 1; j++) {
a[j] = b[j - c];
}
for (; j < n; j++) {
a[j] = b[j];
}
rep(t, n) { b[t] = a[t]; }
}
cout << b[0] << endl;
}
return 0;
}
|
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n; i >= 0; i--)
#define reps(i, m, n) for (int i = m; i < n; i++)
#define repsb(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
int main() {
int n, p, c, r;
while (true) {
cin >> n >> r;
if (n == 0)
return 0;
int b[50];
rep(i, n) { b[i] = n - i; }
rep(s, r) {
int a[50], j;
cin >> p >> c;
for (j = 0; j < c; j++) {
a[j] = b[p + j - 1];
}
for (; j < p + c - 1; j++) {
a[j] = b[j - c];
}
for (; j < n; j++) {
a[j] = b[j];
}
rep(t, n) { b[t] = a[t]; }
}
cout << b[0] << endl;
}
return 0;
}
|
[["-", 12, 69, 341, 342, 0, 16, 31, 16, 31, 22], ["+", 12, 69, 341, 342, 0, 16, 31, 16, 31, 22]]
| 1
| 288
|
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
void f(int n, int r) {
int a[100];
rep(i, n) { a[i + 1] = n - i; }
rep(i, r) {
int p, c;
cin >> p >> c;
int b[100];
rep(j, p) { b[j] = a[j + 1]; }
rep(j, c) { a[j + 1] = a[p + j]; }
rep(j, p) { a[j + c + 1] = b[j]; }
}
cout << a[1] << endl;
}
int main() {
while (1) {
int n, r;
cin >> n >> r;
if (n == 0)
break;
f(n, r);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int, int>
#define PLL pair<ll, ll>
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define RFOR(i, m, n) for (int i = m; i >= n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define PI 3.14159265
void f(int n, int r) {
int a[100];
rep(i, n) { a[i + 1] = n - i; }
rep(i, r) {
int p, c;
cin >> p >> c;
int b[100];
rep(j, p - 1) { b[j] = a[j + 1]; }
rep(j, c) { a[j + 1] = a[p + j]; }
rep(j, p - 1) { a[j + c + 1] = b[j]; }
}
cout << a[1] << endl;
}
int main() {
while (1) {
int n, r;
cin >> n >> r;
if (n == 0)
break;
f(n, r);
}
return 0;
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13], ["+", 0, 1, 0, 2, 3, 4, 0, 25, 0, 35]]
| 1
| 278
|
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
int yama[51];
int n, r;
int tmp[51];
while (std::cin >> n >> r && (n || r)) {
int c, p;
rep(i, n) { yama[i] = n - i; }
rep(i, r) {
std::cin >> p >> c;
rep(j, c) { tmp[j] = yama[p - 1 + j]; }
rep(k, p - 1) { yama[p - 1 + c - k] = yama[p - 1 - k]; }
rep(l, c) { yama[l] = tmp[l]; }
}
std::cout << yama[0] << std::endl;
}
return 0;
}
|
#include <iostream>
#define rep(i, n) for (int i = 0; i < n; i++)
int main() {
int yama[51];
int n, r;
int tmp[51];
while (std::cin >> n >> r && (n || r)) {
int c, p;
rep(i, n) { yama[i] = n - i; }
rep(i, r) {
std::cin >> p >> c;
rep(j, c) { tmp[j] = yama[p - 1 + j]; }
rep(k, p - 1) { yama[p - 1 + c - k - 1] = yama[p - 1 - k - 1]; }
rep(l, c) { yama[l] = tmp[l]; }
}
std::cout << yama[0] << std::endl;
}
return 0;
}
|
[["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13], ["+", 0, 11, 12, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 12, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 176
|
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int solve(int n, int r) {
vector<pair<int, int>> p;
for (int i = 0; i < r; i++) {
int a, b;
cin >> a >> b;
p.push_back(make_pair(a, b));
}
reverse(p.begin(), p.end());
int x = n;
for (vector<pair<int, int>>::iterator it = p.begin(); it != p.end(); ++it) {
int a = (*it).first;
int b = (*it).second;
if (x >= (n - b + 1)) {
x = x - (a - 1);
} else if (x >= (n - b) - (a - 1)) {
x = x + b;
}
}
cout << x << endl;
}
int main() {
int n, r;
while (cin >> n >> r) {
if (n == 0 && r == 0) {
break;
}
solve(n, r);
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
int solve(int n, int r) {
vector<pair<int, int>> p;
for (int i = 0; i < r; i++) {
int a, b;
cin >> a >> b;
p.push_back(make_pair(a, b));
}
reverse(p.begin(), p.end());
int x = n;
for (vector<pair<int, int>>::iterator it = p.begin(); it != p.end(); ++it) {
int a = (*it).first;
int b = (*it).second;
if (x >= (n - b + 1)) {
x = x - (a - 1);
} else if (x >= (n - b) - (a - 1) + 1) {
x = x + b;
}
}
cout << x << endl;
}
int main() {
int n, r;
while (cin >> n >> r) {
if (n == 0 && r == 0) {
break;
}
solve(n, r);
}
return 0;
}
|
[["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 72], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 12, 13]]
| 1
| 247
|
#include <iostream>
using namespace std;
int card[50];
int buffer[50];
int main() {
int n, r;
while (cin >> n >> r, n != 0) {
for (int i = 0; i < n; i++)
card[i] = i + 1;
for (int i = 0; i < r; i++) {
int p, c;
cin >> p >> c;
for (int j = 0; j < p - 1; j++)
buffer[j] = card[j];
for (int j = 0; j < c; j++)
card[j] = card[j + p - 1];
for (int j = 0; j < p - 1; j++)
card[j + c] = buffer[j];
}
cout << card[0] << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
int card[50];
int buffer[50];
int main() {
int n, r;
while (cin >> n >> r, n != 0) {
for (int i = 0; i < n; i++)
card[i] = n - i;
for (int i = 0; i < r; i++) {
int p, c;
cin >> p >> c;
for (int j = 0; j < p - 1; j++)
buffer[j] = card[j];
for (int j = 0; j < c; j++)
card[j] = card[j + p - 1];
for (int j = 0; j < p - 1; j++)
card[j + c] = buffer[j];
}
cout << card[0] << endl;
}
return 0;
}
|
[["+", 0, 7, 8, 1, 0, 11, 12, 16, 31, 22], ["+", 0, 7, 8, 1, 0, 11, 12, 16, 17, 33], ["-", 0, 7, 8, 1, 0, 11, 12, 16, 17, 72], ["-", 0, 7, 8, 1, 0, 11, 12, 16, 12, 13]]
| 1
| 187
|
#include <iostream>
using namespace std;
int main() {
int card[50], tmp[50];
int n, r, p, c;
while (cin >> n >> r, n) {
for (int i = 0; i < n; i++)
card[i] = n - i;
for (int x = 0; x < r; x++) {
cin >> p >> c;
for (int i = 0; i < c; i++)
tmp[i] = card[p - 1 + i];
for (int i = p - 1; i >= 0; i--)
card[i + c] = card[i];
for (int i = 0; i < c; i++)
card[i] = tmp[i];
}
cout << card[0] << endl;
}
}
|
#include <iostream>
using namespace std;
int main() {
int card[50], tmp[50];
int n, r, p, c;
while (cin >> n >> r, n) {
for (int i = 0; i < n; i++)
card[i] = n - i;
for (int x = 0; x < r; x++) {
cin >> p >> c;
for (int i = 0; i < c; i++)
tmp[i] = card[p - 1 + i];
for (int i = p - 2; i >= 0; i--)
card[i + c] = card[i];
for (int i = 0; i < c; i++)
card[i] = tmp[i];
}
cout << card[0] << endl;
}
}
|
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["+", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
| 1
| 178
|
#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n, r, p, c;
int before[50], after[50];
void init() {
for (int i = 0; i < 50; i++)
before[i] = after[i] = 0;
}
bool input() {
int i;
cin >> n;
cin >> r;
if ((n == 0) && (r == 0))
return false;
for (i = 0; i < n; i++)
before[i] = n - i;
after[i] = n - i;
return true;
}
void Shuffle(int b[], int a[]) {
int j;
for (j = 0; j < c; j++)
a[j] = b[p - 1 + j];
for (j = 0; j < p - 1; j++)
a[c + j] = b[j];
for (j = 0; j < n; j++)
b[j] = a[j];
}
int solve() {
for (int i = 0; i < r; i++) {
cin >> p >> c;
Shuffle(before, after);
}
return after[0];
}
int main() {
while (init(), input()) {
cout << solve() << endl;
}
return 0;
}
|
#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int n, r, p, c;
int before[50], after[50];
void init() {
for (int i = 0; i < 50; i++)
before[i] = after[i] = 0;
}
bool input() {
int i;
cin >> n;
cin >> r;
if ((n == 0) && (r == 0))
return false;
for (i = 0; i < n; i++) {
before[i] = n - i;
after[i] = n - i;
}
return true;
}
void Shuffle(int b[], int a[]) {
int j;
for (j = 0; j < c; j++)
a[j] = b[p - 1 + j];
for (j = 0; j < p - 1; j++)
a[c + j] = b[j];
for (j = 0; j < n; j++)
b[j] = a[j];
}
int solve() {
for (int i = 0; i < r; i++) {
cin >> p >> c;
Shuffle(before, after);
}
return after[0];
}
int main() {
while (init(), input()) {
cout << solve() << endl;
}
return 0;
}
|
[["+", 0, 14, 8, 9, 0, 7, 8, 9, 0, 45], ["+", 0, 14, 8, 9, 0, 7, 8, 9, 0, 46]]
| 1
| 303
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int Card[60];
int temp[60];
int main() {
int n, r;
while (cin >> n >> r, n || r) {
int k = n;
for (int i = 1; i <= n; i++) {
Card[i] = k--;
}
int p, c;
for (int i = 0; i < r; i++) {
cin >> p >> c;
int l = 1;
int m = 1;
for (int j = 1; j <= p + c - 1; j++) {
temp[j] = Card[j];
}
for (int j = p; j <= p + c - 1; j++) {
Card[l++] = Card[j];
}
for (int j = 0; j <= p - 1; j++) {
Card[l + j] = temp[m++];
}
}
cout << Card[1] << endl;
}
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define lengthof(x) (sizeof(x) / sizeof(*(x)))
#define FILL(ptr, value) FILL_((ptr), sizeof(ptr) / sizeof(value), (value))
template <typename T> void FILL_(void *ptr, size_t size, T value) {
std::fill((T *)ptr, (T *)ptr + size, value);
}
// 4方向ベクトル→↑←↓
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};
int Card[60];
int temp[60];
int main() {
int n, r;
while (cin >> n >> r, n || r) {
int k = n;
for (int i = 1; i <= n; i++) {
Card[i] = k--;
}
int p, c;
for (int i = 0; i < r; i++) {
cin >> p >> c;
int l = 1;
int m = 1;
for (int j = 1; j <= p + c - 1; j++) {
temp[j] = Card[j];
}
for (int j = p; j <= p + c - 1; j++) {
Card[l++] = Card[j];
}
for (int j = 1; j <= p - 1; j++) {
Card[l + j - 1] = temp[m++];
}
}
cout << Card[1] << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 17, 33], ["+", 0, 11, 31, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 365
|
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = 0;
int y = 0;
int m_dist = 0;
int m_x = 0, m_y = 0;
while (true) {
final int dx = sc.nextInt();
final int dy = sc.nextInt();
if (dx == 0 && dy == 0) {
break;
}
x += dx;
y += dy;
if (m_dist < x * x + y * y) {
m_dist = x * x + y * y;
m_x = x;
m_y = y;
} else if (m_dist == x * x + y * y && m_x < x) {
m_x = x;
}
}
System.out.println(m_x + " " + m_y);
}
}
}
|
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
final int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = 0;
int y = 0;
int m_dist = 0;
int m_x = 0, m_y = 0;
while (true) {
final int dx = sc.nextInt();
final int dy = sc.nextInt();
if (dx == 0 && dy == 0) {
break;
}
x += dx;
y += dy;
if (m_dist < x * x + y * y) {
m_dist = x * x + y * y;
m_x = x;
m_y = y;
} else if (m_dist == x * x + y * y && m_x < x) {
m_x = x;
m_y = y;
}
}
System.out.println(m_x + " " + m_y);
}
}
}
|
[["+", 75, 57, 64, 196, 0, 1, 0, 11, 31, 22], ["+", 75, 57, 64, 196, 0, 1, 0, 11, 17, 32], ["+", 75, 57, 64, 196, 0, 1, 0, 11, 12, 22], ["+", 0, 57, 75, 57, 64, 196, 0, 1, 0, 35]]
| 3
| 224
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int N;
ll twi(ll a) { return a * a; }
int main() {
cin >> N;
for (int t = 0; t < N; t++) {
int x = 0, y = 0;
int dx, dy;
ll maxk = 0;
int ax = 0, ay = 0;
while (1) {
cin >> dx >> dy;
if (!dx && !dy)
break;
x += dx;
y += dy;
ll at = twi((ll)x) + twi((ll)y);
if (maxk < at) {
maxk = at;
ax = x;
ay = y;
} else if (maxk == at || ax < x) {
maxk = at;
ax = x;
ay = y;
}
}
cout << ax << " " << ay << endl;
}
}
|
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int N;
ll twi(ll a) { return a * a; }
int main() {
cin >> N;
for (int t = 0; t < N; t++) {
int x = 0, y = 0;
int dx, dy;
ll maxk = 0;
int ax = 0, ay = 0;
while (1) {
cin >> dx >> dy;
if (!dx && !dy)
break;
x += dx;
y += dy;
ll at = twi((ll)x) + twi((ll)y);
if (maxk < at) {
maxk = at;
ax = x;
ay = y;
} else if (maxk == at && ax < x) {
maxk = at;
ax = x;
ay = y;
}
}
cout << ax << " " << ay << endl;
}
}
|
[["-", 75, 76, 0, 57, 15, 339, 51, 16, 17, 106], ["+", 75, 76, 0, 57, 15, 339, 51, 16, 17, 98]]
| 1
| 197
|
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.TreeSet;
// Pile Up!
public class Main {
void run() {
Scanner sc = new Scanner(System.in);
for (;;) {
int n = sc.nextInt();
if (n == 0)
break;
Set<Integer> floor = new TreeSet<Integer>();
LinkedList<Stack<Integer>> pile = new LinkedList<Stack<Integer>>();
for (int i = 1; i <= n; i++)
floor.add(i);
for (;;) {
int a = sc.nextInt();
int b = sc.nextInt();
if ((a | b) == 0)
break;
if (a == b)
continue;
if (b == 0) {
if (floor.contains(a))
continue;
for (Stack<Integer> s : pile) {
if (s.contains(a)) {
if (s.lastElement() == a)
break;
while (s.peek() != a) {
floor.add(s.pop());
}
floor.add(s.pop());
break;
}
}
continue;
}
int ai = -1;
int bi = -1;
for (int i = 0; i < pile.size(); i++) {
if (pile.get(i).contains(a))
ai = i;
if (pile.get(i).contains(b))
bi = i;
}
if (ai == bi && ai != -1) {
Stack<Integer> s = pile.get(ai);
int aj = s.search(a);
int bj = s.search(b);
if (aj < bj)
continue;
while (s.peek() != a) {
floor.add(s.pop());
}
floor.remove(b);
Stack<Integer> st = new Stack<Integer>();
st.push(b);
st.push(s.pop());
pile.add(st);
if (s.isEmpty())
pile.remove(ai);
continue;
}
if (ai != -1) {
Stack<Integer> s = pile.get(ai);
while (s.peek() != a)
floor.add(s.pop());
s.pop();
if (s.isEmpty())
pile.remove(ai);
for (int i = 0; i < pile.size(); i++)
if (pile.get(i).contains(b))
bi = i;
} else
floor.remove(a);
if (bi != -1)
pile.get(bi).push(a);
else {
Stack<Integer> st = new Stack<Integer>();
floor.remove(b);
st.push(b);
st.push(a);
pile.add(st);
}
}
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
for (int i = 0; i < floor.size(); i++)
q.add(1);
for (Stack<Integer> s : pile)
q.add(s.size());
while (!q.isEmpty())
System.out.println(q.poll());
System.out.println("end");
}
}
public static void main(String[] args) { new Main().run(); }
}
|
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.Stack;
import java.util.TreeSet;
// Pile Up!
public class Main {
void run() {
Scanner sc = new Scanner(System.in);
for (;;) {
int n = sc.nextInt();
if (n == 0)
break;
Set<Integer> floor = new TreeSet<Integer>();
LinkedList<Stack<Integer>> pile = new LinkedList<Stack<Integer>>();
for (int i = 1; i <= n; i++)
floor.add(i);
for (;;) {
int a = sc.nextInt();
int b = sc.nextInt();
if ((a | b) == 0)
break;
if (a == b)
continue;
if (b == 0) {
if (floor.contains(a))
continue;
for (Stack<Integer> s : pile) {
if (s.contains(a)) {
if (s.get(0) == a)
break;
while (s.peek() != a) {
floor.add(s.pop());
}
floor.add(s.pop());
break;
}
}
continue;
}
int ai = -1;
int bi = -1;
for (int i = 0; i < pile.size(); i++) {
if (pile.get(i).contains(a))
ai = i;
if (pile.get(i).contains(b))
bi = i;
}
if (ai == bi && ai != -1) {
Stack<Integer> s = pile.get(ai);
int aj = s.search(a);
int bj = s.search(b);
if (aj < bj)
continue;
while (s.peek() != a) {
floor.add(s.pop());
}
floor.remove(b);
Stack<Integer> st = new Stack<Integer>();
st.push(b);
st.push(s.pop());
pile.add(st);
if (s.isEmpty())
pile.remove(ai);
continue;
}
if (ai != -1) {
Stack<Integer> s = pile.get(ai);
while (s.peek() != a)
floor.add(s.pop());
s.pop();
if (s.isEmpty())
pile.remove(ai);
for (int i = 0; i < pile.size(); i++)
if (pile.get(i).contains(b))
bi = i;
} else
floor.remove(a);
if (bi != -1)
pile.get(bi).push(a);
else {
Stack<Integer> st = new Stack<Integer>();
floor.remove(b);
st.push(b);
st.push(a);
pile.add(st);
}
}
PriorityQueue<Integer> q = new PriorityQueue<Integer>();
for (int i = 0; i < floor.size(); i++)
q.add(1);
for (Stack<Integer> s : pile)
q.add(s.size());
while (!q.isEmpty())
System.out.println(q.poll());
System.out.println("end");
}
}
public static void main(String[] args) { new Main().run(); }
}
|
[["-", 0, 57, 15, 15, 0, 16, 31, 492, 141, 22], ["+", 0, 57, 15, 15, 0, 16, 31, 492, 141, 22], ["+", 15, 15, 0, 16, 31, 492, 3, 4, 0, 499]]
| 3
| 775
|
#include <stdio.h>
#include <stdlib.h>
int cube_all;
int cube_num[100];
int cube_map[100][100];
int cube_where[100];
/* syo-zyun */
int qsort_comp(const void *x, const void *y) {
int *a = (int *)x;
int *b = (int *)y;
if (*a > *b)
return 1;
if (*a < *b)
return -1;
return 0;
}
int main(void) {
int i, j;
int which, where;
int which_x, which_y, where_x, where_y;
while (1) {
scanf("%d", &cube_all);
if (cube_all == 0)
break;
for (i = 0; i < cube_all; i++) {
cube_num[i] = 1;
cube_map[i][0] = i + 1;
}
while (1) {
scanf("%d%d", &which, &where);
if (which == 0 && where == 0)
break;
if (which == where)
continue;
which_x = which_y = where_x = where_y = 0;
for (i = 0; i < cube_all; i++) {
for (j = 0; j < cube_num[i]; j++) {
if (cube_map[i][j] == which) {
which_x = i;
which_y = j;
} else if (cube_map[i][j] == where) {
where_x = i;
where_y = j;
}
}
}
if (where == 0) {
if (which_y > 0) {
for (i = which_y + 1; i < cube_num[which_x]; i++) {
for (j = 0; j < cube_all; j++) {
if (cube_num[j] <= 0) {
cube_num[j] = 1;
cube_map[j][0] = cube_map[which_x][i];
break;
}
}
}
cube_num[which_x] = which_y;
}
} else {
if (which_x != where_x) {
for (i = which_y + 1; i < cube_num[which_x]; i++) {
for (j = 0; j < cube_all; j++) {
if (cube_num[j] <= 0) {
cube_num[j] = 1;
cube_map[j][0] = cube_map[which_x][i];
break;
}
}
}
cube_num[which_x] = which_y;
cube_map[where_x][cube_num[where_x]++] = which;
} else if (which_y < where_y) {
for (i = which_y + 1; i < cube_num[which_x]; i++) {
for (j = 0; j < cube_all; j++) {
if (cube_num[j] <= 0) {
cube_num[j] = 1;
cube_map[j][0] = cube_map[which_x][i];
if (cube_map[j][0] == where) {
where_x = j;
}
break;
}
}
}
cube_num[which_x] = which_y;
cube_map[where_x][cube_num[where_x]++] = which;
}
}
}
qsort(cube_num, cube_all, sizeof(int), qsort_comp);
for (i = 0; i < cube_all; i++) {
if (cube_num[i] > 0)
printf("%d\n", cube_num[i]);
}
puts("end");
}
return 0;
}
|
#include <stdio.h>
#include <stdlib.h>
int cube_all;
int cube_num[100];
int cube_map[100][100];
int cube_where[100];
/* syo-zyun */
int qsort_comp(const void *x, const void *y) {
int *a = (int *)x;
int *b = (int *)y;
if (*a > *b)
return 1;
if (*a < *b)
return -1;
return 0;
}
int main(void) {
int i, j;
int which, where;
int which_x, which_y, where_x, where_y;
while (1) {
scanf("%d", &cube_all);
if (cube_all == 0)
break;
for (i = 0; i < cube_all; i++) {
cube_num[i] = 1;
cube_map[i][0] = i + 1;
}
while (1) {
scanf("%d%d", &which, &where);
if (which == 0 && where == 0)
break;
if (which == where)
continue;
which_x = which_y = where_x = where_y = 0;
for (i = 0; i < cube_all; i++) {
for (j = 0; j < cube_num[i]; j++) {
if (cube_map[i][j] == which) {
which_x = i;
which_y = j;
} else if (cube_map[i][j] == where) {
where_x = i;
where_y = j;
}
}
}
if (where == 0) {
if (which_y > 0) {
for (i = which_y; i < cube_num[which_x]; i++) {
for (j = 0; j < cube_all; j++) {
if (cube_num[j] <= 0) {
cube_num[j] = 1;
cube_map[j][0] = cube_map[which_x][i];
break;
}
}
}
cube_num[which_x] = which_y;
}
} else {
if (which_x != where_x) {
for (i = which_y + 1; i < cube_num[which_x]; i++) {
for (j = 0; j < cube_all; j++) {
if (cube_num[j] <= 0) {
cube_num[j] = 1;
cube_map[j][0] = cube_map[which_x][i];
break;
}
}
}
cube_num[which_x] = which_y;
cube_map[where_x][cube_num[where_x]++] = which;
} else if (which_y < where_y) {
for (i = which_y + 1; i < cube_num[which_x]; i++) {
for (j = 0; j < cube_all; j++) {
if (cube_num[j] <= 0) {
cube_num[j] = 1;
cube_map[j][0] = cube_map[which_x][i];
if (cube_map[j][0] == where) {
where_x = j;
}
break;
}
}
}
cube_num[which_x] = which_y;
cube_map[where_x][cube_num[where_x]++] = which;
}
}
}
qsort(cube_num, cube_all, sizeof(int), qsort_comp);
for (i = 0; i < cube_all; i++) {
if (cube_num[i] > 0)
printf("%d\n", cube_num[i]);
}
puts("end");
}
return 0;
}
|
[["-", 64, 9, 0, 7, 10, 11, 12, 16, 17, 72], ["-", 64, 9, 0, 7, 10, 11, 12, 16, 12, 13]]
| 0
| 677
|
#include <bits/stdc++.h>
using namespace std;
int aidx, ah, bidx, bh;
vector<int> pile[105];
int m;
void put_floor(int x) {
for (int i = 0; i < m; i++)
if (!pile[i].size())
pile[i].push_back(x);
}
void get_place_a(int x) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < pile[i].size(); j++) {
if (pile[i][j] == x) {
aidx = i;
ah = j;
return;
}
}
}
}
void get_place_b(int x) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < pile[i].size(); j++) {
if (pile[i][j] == x) {
bidx = i;
bh = j;
return;
}
}
}
}
int main() {
while (1) {
cin >> m;
if (!m)
break;
for (int i = 0; i < m; i++)
pile[i].clear();
for (int i = 0; i < m; i++)
pile[i].push_back(i + 1);
while (1) {
int a, b;
cin >> a >> b;
if (!a)
break;
if (!b) {
get_place_a(a);
if (ah == 0)
continue;
for (int i = pile[aidx].size() - 1; i >= ah; i--) {
put_floor(pile[aidx][i]);
pile[aidx].erase(pile[aidx].end() - 1);
}
} else {
get_place_a(a);
get_place_b(b);
if (aidx == bidx && ah >= bh)
continue;
for (int i = pile[aidx].size() - 1; i > ah; i--) {
put_floor(pile[aidx][i]);
pile[aidx].erase(pile[aidx].end() - 1);
}
pile[aidx].erase(pile[aidx].end() - 1);
get_place_b(b);
pile[bidx].push_back(a);
}
}
vector<int> ans;
for (int i = 0; i < m; i++)
ans.push_back(pile[i].size());
sort(ans.begin(), ans.end());
for (int i = 0; i < m; i++) {
if (ans[i] == 0)
continue;
cout << ans[i] << endl;
}
cout << "end" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int aidx, ah, bidx, bh;
vector<int> pile[105];
int m;
void put_floor(int x) {
for (int i = 0; i < m; i++)
if (!pile[i].size()) {
pile[i].push_back(x);
return;
}
}
void get_place_a(int x) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < pile[i].size(); j++) {
if (pile[i][j] == x) {
aidx = i;
ah = j;
return;
}
}
}
}
void get_place_b(int x) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < pile[i].size(); j++) {
if (pile[i][j] == x) {
bidx = i;
bh = j;
return;
}
}
}
}
int main() {
while (1) {
cin >> m;
if (!m)
break;
for (int i = 0; i < m; i++)
pile[i].clear();
for (int i = 0; i < m; i++)
pile[i].push_back(i + 1);
while (1) {
int a, b;
cin >> a >> b;
if (!a)
break;
if (!b) {
get_place_a(a);
if (ah == 0)
continue;
for (int i = pile[aidx].size() - 1; i >= ah; i--) {
put_floor(pile[aidx][i]);
pile[aidx].erase(pile[aidx].end() - 1);
}
} else {
get_place_a(a);
get_place_b(b);
if (aidx == bidx && ah >= bh)
continue;
for (int i = pile[aidx].size() - 1; i > ah; i--) {
put_floor(pile[aidx][i]);
pile[aidx].erase(pile[aidx].end() - 1);
}
pile[aidx].erase(pile[aidx].end() - 1);
// pile[aidx].erase( pile[aidx].begin()+ah );
get_place_b(b);
pile[bidx].push_back(a);
}
}
vector<int> ans;
for (int i = 0; i < m; i++)
ans.push_back(pile[i].size());
sort(ans.begin(), ans.end());
for (int i = 0; i < m; i++) {
if (ans[i] == 0)
continue;
cout << ans[i] << endl;
}
cout << "end" << endl;
}
return 0;
}
|
[["+", 8, 9, 0, 7, 8, 57, 64, 9, 0, 45], ["+", 0, 7, 8, 57, 64, 9, 0, 1, 0, 35], ["+", 0, 7, 8, 57, 64, 9, 0, 37, 0, 38], ["+", 8, 9, 0, 7, 8, 57, 64, 9, 0, 46]]
| 1
| 594
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int m;
while (cin >> m && m != 0) {
int s, d;
vector<vector<int>> pileSet;
for (int i = 0; i < m; i++)
pileSet.push_back(vector<int>(1, i + 1));
while (cin >> s >> d && !(s == 0 && d == 0)) {
if (s == d)
continue;
// sðT·
pair<int, int> sp;
for (int i = 0; i < pileSet.size(); i++) {
for (int j = pileSet[i].size() - 1; j >= 0; j--) {
if (pileSet[i][j] == s) {
sp.first = i;
sp.second = j;
}
}
}
// tðT·
pair<int, int> dp;
for (int i = 0; i < pileSet.size(); i++) {
for (int j = pileSet[i].size() - 1; j >= 0; j--) {
if (pileSet[i][j] == d) {
dp.first = i;
dp.second = j;
}
}
}
if (d == 0) {
if (pileSet[sp.first].size() == 1)
continue;
// sÌãÉÂñÅ êÎA»êà¨ë·
for (int i = pileSet[sp.first].size() - 1; i >= 0; i--) {
int num = pileSet[sp.first][i];
// ¨ë·
pileSet[sp.first].erase(pileSet[sp.first].begin() + i);
pileSet.push_back(vector<int>(1, num));
if (num == s)
break;
}
}
// sÆdÌlÉæÁÄêí¯
else if (dp.first == sp.first) {
if (dp.second > sp.second) {
for (int i = pileSet[dp.first].size() - 1; i >= 0; i--) {
int num = pileSet[dp.first][i];
if (num == s) {
pileSet[dp.first].erase(pileSet[dp.first].begin() + i);
break;
}
pileSet[dp.first].erase(pileSet[dp.first].begin() + i);
pileSet.push_back(vector<int>(1, num));
}
// sðæ¹é
bool fin = false;
for (int i = 0; i < pileSet.size(); i++) {
for (int j = 0; j < pileSet[i].size(); j++) {
if (pileSet[i][j] == d) {
pileSet[i].push_back(s);
fin = true;
break;
}
}
if (fin)
break;
}
}
}
// ÊÌRÖÌÚ®Ìê
else {
// ܸsÌãÉ éÉàÂð¨ë·
for (int i = pileSet[sp.first].size() - 1; i >= 0; i--) {
int num = pileSet[sp.first][i];
// ¨ë·
pileSet[sp.first].erase(pileSet[sp.first].begin() + i);
if (num == s)
break;
else
pileSet.push_back(vector<int>(1, num));
}
// dÌRÌãÉæÁ¯é
pileSet[dp.first].push_back(s);
}
// óÌpileÍí
for (int i = pileSet.size() - 1; i >= 0; i--) {
if (pileSet[i].size() == 0)
pileSet.erase(pileSet.begin() + i);
}
}
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < pileSet.size(); i++)
pq.push(pileSet[i].size());
while (!pq.empty()) {
cout << pq.top() << endl;
pq.pop();
}
cout << "end" << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
int m;
while (cin >> m && m != 0) {
int s, d;
vector<vector<int>> pileSet;
for (int i = 0; i < m; i++)
pileSet.push_back(vector<int>(1, i + 1));
while (cin >> s >> d && !(s == 0 && d == 0)) {
if (s == d)
continue;
// sðT·
pair<int, int> sp;
for (int i = 0; i < pileSet.size(); i++) {
for (int j = pileSet[i].size() - 1; j >= 0; j--) {
if (pileSet[i][j] == s) {
sp.first = i;
sp.second = j;
}
}
}
// dðT·
pair<int, int> dp;
for (int i = 0; i < pileSet.size(); i++) {
for (int j = pileSet[i].size() - 1; j >= 0; j--) {
if (pileSet[i][j] == d) {
dp.first = i;
dp.second = j;
}
}
}
if (d == 0) {
if (pileSet[sp.first][0] == s)
continue;
// sÌãÉÂñÅ êÎA»êà¨ë·
for (int i = pileSet[sp.first].size() - 1; i >= 0; i--) {
int num = pileSet[sp.first][i];
// ¨ë·
pileSet[sp.first].erase(pileSet[sp.first].begin() + i);
pileSet.push_back(vector<int>(1, num));
if (num == s)
break;
}
}
// sÆdÌlÉæÁÄêí¯
else if (dp.first == sp.first) {
if (dp.second > sp.second) {
for (int i = pileSet[dp.first].size() - 1; i >= 0; i--) {
int num = pileSet[dp.first][i];
if (num == s) {
pileSet[dp.first].erase(pileSet[dp.first].begin() + i);
break;
}
pileSet[dp.first].erase(pileSet[dp.first].begin() + i);
pileSet.push_back(vector<int>(1, num));
}
// sðæ¹é
bool fin = false;
for (int i = 0; i < pileSet.size(); i++) {
for (int j = 0; j < pileSet[i].size(); j++) {
if (pileSet[i][j] == d) {
pileSet[i].push_back(s);
fin = true;
break;
}
}
if (fin)
break;
}
}
}
// ÊÌRÖÌÚ®Ìê
else {
// ܸsÌãÉ éÉàÂð¨ë·
for (int i = pileSet[sp.first].size() - 1; i >= 0; i--) {
int num = pileSet[sp.first][i];
// ¨ë·
pileSet[sp.first].erase(pileSet[sp.first].begin() + i);
if (num == s)
break;
else
pileSet.push_back(vector<int>(1, num));
}
// dÌRÌãÉæÁ¯é
pileSet[dp.first].push_back(s);
}
// óÌpileÍí
for (int i = pileSet.size() - 1; i >= 0; i--) {
if (pileSet[i].size() == 0)
pileSet.erase(pileSet.begin() + i);
}
}
priority_queue<int, vector<int>, greater<int>> pq;
for (int i = 0; i < pileSet.size(); i++)
pq.push(pileSet[i].size());
while (!pq.empty()) {
cout << pq.top() << endl;
pq.pop();
}
cout << "end" << endl;
}
return 0;
}
|
[["-", 15, 339, 51, 16, 31, 2, 63, 118, 17, 131], ["-", 15, 339, 51, 16, 31, 2, 63, 118, 119, 120], ["-", 15, 339, 51, 16, 31, 2, 3, 4, 0, 24], ["-", 15, 339, 51, 16, 31, 2, 3, 4, 0, 25], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 70], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 13], ["+", 15, 339, 51, 16, 31, 69, 341, 342, 0, 73], ["-", 64, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 12, 22]]
| 1
| 843
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
inline int getInt() {
int s;
scanf("%d", &s);
return s;
}
using namespace std;
int main() {
while (int m = getInt()) {
vector<int> memo(m);
vector<vector<int>> t(m);
queue<int> empty;
REP(i, m) t[i].push_back(memo[i] = i);
while (true) {
const int a = getInt() - 1;
const int b = getInt() - 1;
if (a + b == -2)
break;
if (a == b)
continue;
const int idx = memo[a];
const int dst = b == -1 ? -1 : memo[b];
const int h = find(t[idx].begin(), t[idx].end(), a) - t[idx].begin();
const int h2 =
b == -1 ? -1 : find(t[dst].begin(), t[dst].end(), b) - t[dst].begin();
if (idx == dst && h > h2)
continue;
if (b == -1 && h == 0)
continue;
for (int i = h + 1; i < (int)t[idx].size(); i++) {
int next = empty.front();
empty.pop();
memo[t[idx][i]] = next;
t[next].push_back(t[idx][i]);
}
if (h == 0)
empty.push(idx);
t[idx].resize(h);
const int dst2 = b == -1 ? -1 : memo[b];
if (b == -1) {
int next = empty.front();
empty.pop();
memo[a] = next;
t[next].push_back(a);
} else {
memo[a] = dst;
t[dst2].push_back(a);
}
// printf("%d %d\n", a + 1, b + 1);
// REP(i,m) printf("%d ", t[i].size()); puts("");
}
vector<int> ans;
REP(i, m) if (t[i].size()) ans.push_back(t[i].size());
sort(ans.begin(), ans.end());
REP(i, ans.size())
printf("%d\n", ans[i]);
puts("end");
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
inline int getInt() {
int s;
scanf("%d", &s);
return s;
}
using namespace std;
int main() {
while (int m = getInt()) {
vector<int> memo(m);
vector<vector<int>> t(m);
queue<int> empty;
REP(i, m) t[i].push_back(memo[i] = i);
while (true) {
const int a = getInt() - 1;
const int b = getInt() - 1;
if (a + b == -2)
break;
if (a == b)
continue;
const int idx = memo[a];
const int dst = b == -1 ? -1 : memo[b];
const int h = find(t[idx].begin(), t[idx].end(), a) - t[idx].begin();
const int h2 =
b == -1 ? -1 : find(t[dst].begin(), t[dst].end(), b) - t[dst].begin();
if (idx == dst && h > h2)
continue;
if (b == -1 && h == 0)
continue;
for (int i = h + 1; i < (int)t[idx].size(); i++) {
int next = empty.front();
empty.pop();
memo[t[idx][i]] = next;
t[next].push_back(t[idx][i]);
}
if (h == 0)
empty.push(idx);
t[idx].resize(h);
const int dst2 = b == -1 ? -1 : memo[b];
if (b == -1) {
int next = empty.front();
empty.pop();
memo[a] = next;
t[next].push_back(a);
} else {
memo[a] = dst2;
t[dst2].push_back(a);
}
// printf("%d %d\n", a + 1, b + 1);
// REP(i,m) printf("%d ", t[i].size()); puts("");
}
vector<int> ans;
REP(i, m) if (t[i].size()) ans.push_back(t[i].size());
sort(ans.begin(), ans.end());
REP(i, ans.size())
printf("%d\n", ans[i]);
puts("end");
}
return 0;
}
|
[["-", 75, 76, 0, 9, 0, 1, 0, 11, 12, 22], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 12, 22]]
| 1
| 538
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-5;
const int inf = 1e8;
int main() {
vs st;
st.pb("a");
st.pb("b");
st.pb("c");
st.pb("d");
st.pb("e");
st.pb("f");
st.pb("g");
st.pb("h");
st.pb("i");
st.pb("j");
st.pb("k");
st.pb("l");
st.pb("m");
st.pb("n");
st.pb("o");
st.pb("p");
st.pb("q");
st.pb("r");
st.pb("s");
st.pb("t");
st.pb("u");
st.pb("v");
st.pb("w");
st.pb("x");
st.pb("y");
st.pb("z");
st.pb("ld");
st.pb("mb");
st.pb("mp");
st.pb("nc");
st.pb("nd");
st.pb("ng");
st.pb("nt");
st.pb("nw");
st.pb("ps");
st.pb("qu");
st.pb("cw");
st.pb("ts");
int n;
cin >> n;
string in;
getline(cin, in);
vs fil;
while (getline(cin, in)) {
rep(i, in.size()) {
if (in[i] == ' ')
fil.pb("");
else if (i + 1 == in.size() || in[i + 1] == ' ') {
fil.pb(in.substr(i, 1));
} else {
string s = in.substr(i, 2);
bool h = false;
rep(j, st.size()) if (st[j] == s) {
h = true;
i++;
fil.pb(s);
}
if (!h) {
fil.pb(in.substr(i, 1));
}
}
}
fil.pb("");
}
map<string, map<string, int>> ma;
rep(i, fil.size() - 1) if (fil[i] != "" && fil[i + 1] != "")
ma[fil[i]][fil[i + 1]]++;
rep(i, st.size()) {
int m = 0;
string out;
for (map<string, int>::iterator it = ma[st[i]].begin();
it != ma[st[i]].end(); it++) {
if (m <= it->second) {
m = it->second;
out = it->first;
}
}
if (!m)
out = "a";
cout << st[i] << " " << out << " " << m << endl;
}
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-5;
const int inf = 1e8;
int main() {
vs st;
st.pb("a");
st.pb("b");
st.pb("c");
st.pb("d");
st.pb("e");
st.pb("f");
st.pb("g");
st.pb("h");
st.pb("i");
st.pb("j");
st.pb("k");
st.pb("l");
st.pb("m");
st.pb("n");
st.pb("o");
st.pb("p");
st.pb("q");
st.pb("r");
st.pb("s");
st.pb("t");
st.pb("u");
st.pb("v");
st.pb("w");
st.pb("x");
st.pb("y");
st.pb("z");
st.pb("ld");
st.pb("mb");
st.pb("mp");
st.pb("nc");
st.pb("nd");
st.pb("ng");
st.pb("nt");
st.pb("nw");
st.pb("ps");
st.pb("qu");
st.pb("cw");
st.pb("ts");
int n;
cin >> n;
string in;
getline(cin, in);
vs fil;
while (getline(cin, in)) {
rep(i, in.size()) {
if (in[i] == ' ')
fil.pb("");
else if (i + 1 == in.size() || in[i + 1] == ' ') {
fil.pb(in.substr(i, 1));
} else {
string s = in.substr(i, 2);
bool h = false;
rep(j, st.size()) if (st[j] == s) {
h = true;
i++;
fil.pb(s);
}
if (!h) {
fil.pb(in.substr(i, 1));
}
}
}
fil.pb("");
}
map<string, map<string, int>> ma;
rep(i, fil.size() - 1) if (fil[i] != "" && fil[i + 1] != "")
ma[fil[i]][fil[i + 1]]++;
rep(i, st.size()) {
int m = 0;
string out;
for (map<string, int>::iterator it = ma[st[i]].begin();
it != ma[st[i]].end(); it++) {
if (m < it->second) {
m = it->second;
out = it->first;
}
}
if (!m)
out = "a";
cout << st[i] << " " << out << " " << m << endl;
}
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 17, 19], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 18]]
| 1
| 889
|
#include <algorithm>
#include <bitset>
#include <iostream>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef tuple<vector<int>, int, int> Hint;
enum { TRY, HIT, BLOW };
const int NUMBERS = 10;
vector<vector<int>> subset_combination(int n, int k, vector<int> number) {
vector<vector<int>> result;
unsigned long long int S = (1 << k) - 1;
unsigned long long int E = ~((1 << n) - 1);
while (!(S & E)) {
bitset<NUMBERS> flag(S);
vector<int> v;
for (int i = 0; i < n; i++)
if (flag[i])
v.push_back(number[i]);
result.push_back(v);
unsigned long long int smallest = S & -S;
unsigned long long int ripple = S + smallest;
unsigned long long int nsmallest = ripple & -ripple;
S = ripple | (((nsmallest / smallest) >> 1) - 1);
}
return result;
}
vector<vector<int>> check(vector<int> v, const vector<Hint> &hint) {
sort(v.begin(), v.end());
vector<vector<int>> result;
do {
bool ok = true;
for (const auto &h : hint) {
int hit = 0, blow = 0;
int count[10] = {0};
for (const auto &n : get<TRY>(h))
count[n]++;
for (int i = 0; i < v.size(); i++) {
if (v[i] == get<TRY>(h)[i])
hit++;
else if (count[v[i]])
blow++;
}
if (hit != get<HIT>(h) || blow != get<BLOW>(h)) {
ok = false;
break;
}
}
if (ok) {
result.push_back(v);
if (result.size() > 1)
return result;
}
} while (next_permutation(v.begin(), v.end()));
return result;
}
int main() {
int L, H;
vector<Hint> hint;
vector<vector<int>> candidate;
while (cin >> L >> H, L | H) {
hint.resize(H);
for (auto &element : hint) {
string t;
int h, b;
cin >> t >> h >> b;
vector<int> number;
for (auto &n : t)
number.push_back(n - '0');
element = Hint(number, h, b);
}
vector<int> all = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
candidate = subset_combination(NUMBERS, L, all);
vector<int> answer;
bool ok = true;
for (auto &v : candidate) {
vector<vector<int>> result = check(v, hint);
if (result.size() > 1)
ok = false;
if (result.size() == 1) {
if (answer.size())
ok = false;
else
answer = *result.begin();
}
if (!ok)
break;
}
if (answer.size()) {
for (const auto &n : answer)
cout << n;
cout << endl;
} else {
cout << "NO" << endl;
}
}
}
|
#include <algorithm>
#include <bitset>
#include <iostream>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
typedef tuple<vector<int>, int, int> Hint;
enum { TRY, HIT, BLOW };
const int NUMBERS = 10;
vector<vector<int>> subset_combination(int n, int k, vector<int> number) {
vector<vector<int>> result;
unsigned long long int S = (1 << k) - 1;
unsigned long long int E = ~((1 << n) - 1);
while (!(S & E)) {
bitset<NUMBERS> flag(S);
vector<int> v;
for (int i = 0; i < n; i++)
if (flag[i])
v.push_back(number[i]);
result.push_back(v);
unsigned long long int smallest = S & -S;
unsigned long long int ripple = S + smallest;
unsigned long long int nsmallest = ripple & -ripple;
S = ripple | (((nsmallest / smallest) >> 1) - 1);
}
return result;
}
vector<vector<int>> check(vector<int> v, const vector<Hint> &hint) {
sort(v.begin(), v.end());
vector<vector<int>> result;
do {
bool ok = true;
for (const auto &h : hint) {
int hit = 0, blow = 0;
int count[10] = {0};
for (const auto &n : get<TRY>(h))
count[n]++;
for (int i = 0; i < v.size(); i++) {
if (v[i] == get<TRY>(h)[i])
hit++;
else if (count[v[i]])
blow++;
}
if (hit != get<HIT>(h) || blow != get<BLOW>(h)) {
ok = false;
break;
}
}
if (ok) {
result.push_back(v);
if (result.size() > 1)
return result;
}
} while (next_permutation(v.begin(), v.end()));
return result;
}
int main() {
int L, H;
vector<Hint> hint;
vector<vector<int>> candidate;
while (cin >> L >> H, L | H) {
hint.resize(H);
for (auto &element : hint) {
string t;
int h, b;
cin >> t >> h >> b;
vector<int> number;
for (auto &n : t)
number.push_back(n - '0');
element = Hint(number, h, b);
}
vector<int> all = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
candidate = subset_combination(NUMBERS, L, all);
vector<int> answer;
bool ok = true;
for (auto &v : candidate) {
vector<vector<int>> result = check(v, hint);
if (result.size() > 1)
ok = false;
if (result.size() == 1) {
if (answer.size())
ok = false;
else
answer = *result.begin();
}
if (!ok)
break;
}
if (ok && answer.size()) {
for (const auto &n : answer)
cout << n;
cout << endl;
} else {
cout << "NO" << endl;
}
}
}
|
[["+", 8, 9, 0, 57, 15, 339, 51, 16, 31, 22], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 17, 98]]
| 1
| 743
|
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int N, R, p, c;
while (cin >> N >> R && N) {
int A[100] = {};
for (int i = 0; i < N; i++) {
A[i] = i + 1;
}
reverse(A, A + N);
for (int i = 0; i < R; ++i) {
cin >> p >> c;
rotate(A, A + p, A + p + c);
}
cout << A[0] << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int N, R, p, c;
while (cin >> N >> R && N) {
int A[100] = {};
for (int i = 0; i < N; i++) {
A[i] = i + 1;
}
reverse(A, A + N);
for (int i = 0; i < R; ++i) {
cin >> p >> c;
rotate(A, A + p - 1, A + p + c - 1);
}
cout << A[0] << endl;
}
return 0;
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 127
|
while True:
n, r = map(int, input().split())
if (n, r) == (0, 0):
break
cards = list(range(1, n+1))[::-1]
print(cards)
for i in range(r):
p, c = map(int, input().split())
cards = cards[p-1:p-1+c] + cards[:p-1] + cards[p-1+c:]
print(cards[0])
|
while True:
n, r = map(int, input().split())
if (n, r) == (0, 0):
break
cards = list(range(1, n+1))[::-1]
for i in range(r):
p, c = map(int, input().split())
cards = cards[p-1:p-1+c] + cards[:p-1] + cards[p-1+c:]
print(cards[0])
|
[["-", 0, 52, 8, 196, 0, 1, 0, 652, 63, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25]]
| 5
| 119
|
while True:
n, r = map(int, input().split())
if n == 0 and r == 0:
break
cards = list(range(1, n+1))[::-1]
for _ in range(r):
p, c = map(int, input().split())
cards = cards[p-1:p-1+c] + cards[:p-1] + cards[p+c:]
print(cards[0])
|
while True:
n, r = map(int, input().split())
if n == 0 and r == 0:
break
cards = list(range(1, n+1))[::-1]
for _ in range(r):
p, c = map(int, input().split())
cards = cards[p-1:p-1+c] + cards[:p-1] + cards[p-1+c:]
print(cards[0])
|
[["+", 12, 206, 206, 663, 0, 657, 31, 657, 17, 33], ["+", 12, 206, 206, 663, 0, 657, 31, 657, 12, 612]]
| 5
| 109
|
while True:
sousa=input().sprit() #?????????
sousa=[int(sousa[0]),int(sousa[1])]
if sousa==[0,0]:break
yama=[]
for k in range(sousa[0]):yama.append(k+1)
yama=yama[::-1]
for r in range(sousa[1]):
[p,c]=input().sprit()
[p,c]=[int(p),int(c)]
yama[:p+c-1]=yama[p-1:p+c-1]+yama[:p-2]
print(yama[0])
|
while True:
sousa=input().split()
sousa=[int(sousa[0]),int(sousa[1])]
if sousa==[0,0]:break
yama=[]
for k in range(sousa[0]):yama.append(k+1)
yama=yama[::-1]
for r in range(sousa[1]):
[p,c]=input().split()
[p,c]=[int(p),int(c)]
yama[:p+c-1]=yama[p-1:p+c-1]+yama[:p-1]
print(yama[0])
|
[["-", 0, 1, 0, 662, 12, 652, 63, 319, 319, 22], ["+", 0, 1, 0, 662, 12, 652, 63, 319, 319, 22], ["-", 12, 657, 12, 206, 206, 663, 0, 657, 12, 612], ["+", 12, 657, 12, 206, 206, 663, 0, 657, 12, 612]]
| 5
| 152
|
first = input().split()
n = int(first[0])
r = int(first[1])
while n != 0 or r != 0:
i = 0
card = list(range(1,n+1))[::-1]
while i < r:
sh = input().split()
p, c = list(map(int,sh))
card = card[p-1:p+c-1] + card[:p-1] + card[p+c-1:]
i = i + 1
print(card)
next = input().split()
n, r = list(map(int,next))
|
first = input().split()
n = int(first[0])
r = int(first[1])
while n != 0 or r != 0:
i = 0
card = list(range(1,n+1))[::-1]
while i < r:
sh = input().split()
p, c = list(map(int,sh))
card = card[p-1:p+c-1] + card[:p-1] + card[p+c-1:]
i = i + 1
print(card[0])
next = input().split()
n, r = list(map(int,next))
|
[["+", 0, 1, 0, 652, 3, 4, 0, 206, 0, 70], ["+", 0, 1, 0, 652, 3, 4, 0, 206, 206, 612], ["+", 0, 1, 0, 652, 3, 4, 0, 206, 0, 73]]
| 5
| 148
|
while True:
n, r = list(map(lambda x: int(x), input().split(" ")))
if n == 0 and r == 0:
break
cards = [i for i in range(1,n+1)]
cards = cards[::-1]
for i in range(r):
print(cards)
p, c = list(map(lambda x: int(x), input().split(" ")))
p -= 1
cards = cards[p:p+c] + cards[0:p] + cards[p+c:]
print(cards[0])
|
while True:
n, r = list(map(lambda x: int(x), input().split(" ")))
if n == 0 and r == 0:
break
cards = [i for i in range(1,n+1)]
cards = cards[::-1]
for i in range(r):
p, c = list(map(lambda x: int(x), input().split(" ")))
p -= 1
cards = cards[p:p+c] + cards[0:p] + cards[p+c:]
print(cards[0])
|
[["-", 0, 7, 8, 196, 0, 1, 0, 652, 63, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25]]
| 5
| 141
|
while True:
n, r = list(map(int, input().split()))
if n == 0 and r == 0:
break
Cards = [list(range(n, 0, -1))]
for i in range(r):
p, c = list(map(int, input().split()))
Cards = Cards[p-1:p-1+c] + Cards[:p-1] + Cards[p-1+c:]
print(Cards[0])
|
while True:
n, r = list(map(int, input().split()))
if n == 0 and r == 0:
break
Cards = list(range(n, 0, -1))
for i in range(r):
p, c = list(map(int, input().split()))
Cards = Cards[p-1:p-1+c] + Cards[:p-1] + Cards[p-1+c:]
print(Cards[0])
|
[["-", 8, 196, 0, 1, 0, 662, 12, 634, 0, 70], ["-", 8, 196, 0, 1, 0, 662, 12, 634, 0, 73]]
| 5
| 114
|
function bomb(y, x) {
var dy = [ 0, 0, -1, 1 ];
var dx = [ -1, 1, 0, 0 ];
for (var i = 0; i < 4; i++) {
var yy = y + dy[i];
var xx = x + dx[i];
if (yy < 0 || yy >= H || xx < 0 || xx >= W)
continue;
if (yx[yy][xx] == ".") {
cnt++;
yx[yy][xx] = cnt;
bomb(yy, xx);
}
}
}
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var Arr = (input.trim()).split("\n");
while (true) {
var WH = (Arr.shift()).split(" ").map(Number);
var W = ab[0];
var H = ab[1];
if (W == 0 && H == 0)
break;
var yx = [];
var Y, X;
for (var i = 0; i < H; i++) {
var arr = (Arr.shift()).split("");
if (arr.indexOf("@") != -1) {
Y = i;
X = arr.indexOf("@");
}
yx.push(arr);
}
var cnt = 1;
bomb(Y, X);
console.log(cnt);
}
|
function bomb(y, x) {
var dy = [ 0, 0, -1, 1 ];
var dx = [ -1, 1, 0, 0 ];
for (var i = 0; i < 4; i++) {
var yy = y + dy[i];
var xx = x + dx[i];
if (yy < 0 || yy >= H || xx < 0 || xx >= W)
continue;
if (yx[yy][xx] == ".") {
cnt++;
yx[yy][xx] = cnt;
bomb(yy, xx);
}
}
}
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var Arr = (input.trim()).split("\n");
while (true) {
var WH = (Arr.shift()).split(" ").map(Number);
var W = WH[0];
var H = WH[1];
if (W == 0 && H == 0)
break;
var yx = [];
var Y, X;
for (var i = 0; i < H; i++) {
var arr = (Arr.shift()).split("");
if (arr.indexOf("@") != -1) {
Y = i;
X = arr.indexOf("@");
}
yx.push(arr);
}
var cnt = 1;
bomb(Y, X);
console.log(cnt);
}
|
[["-", 8, 556, 0, 198, 0, 200, 51, 69, 500, 22], ["+", 8, 556, 0, 198, 0, 200, 51, 69, 500, 22]]
| 2
| 326
|
import java.util.*;
public class Main {
int[][] land, looked;
int sx, sy, w, h, count;
int[] dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
Scanner stdin = new Scanner(System.in);
Queue qx = new LinkedList();
Queue qy = new LinkedList();
void Solve() {
while (true) {
w = stdin.nextInt();
h = stdin.nextInt();
if (w == 0 && h == 0)
break;
land = new int[h][w];
looked = new int[h][w];
count = 1;
for (int i = 0; i < h; i++) {
String s = stdin.next();
for (int j = 0; j < w; j++) {
if (s.charAt(j) == '.')
land[i][j] = 1;
else if (s.charAt(j) == '@') {
land[i][j] = 0;
sx = i;
sy = j;
} else
land[i][j] = 0;
}
}
}
looked = land.clone();
// System.out.println(land[5][8]);
bfs(sx, sy);
System.out.println(count);
}
void bfs(int sx, int sy) {
// System.out.println(sx+" "+sy);
looked[sx][sy] = 0;
for (int i = 0; i < 4; i++) {
int xx = sx + dx[i];
int yy = sy + dy[i];
if (0 <= xx && xx < h && 0 <= yy && yy < w) {
if (land[xx][yy] == 1) {
if (looked[xx][yy] == 1)
count++;
bfs(xx, yy);
}
}
}
}
public static void main(String args[]) {
Main m = new Main();
m.Solve();
}
}
|
import java.util.*;
public class Main {
int[][] land, looked;
int sx, sy, w, h, count;
int[] dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1};
Scanner stdin = new Scanner(System.in);
Queue qx = new LinkedList();
Queue qy = new LinkedList();
void Solve() {
while (true) {
w = stdin.nextInt();
h = stdin.nextInt();
if (w == 0 && h == 0)
break;
land = new int[h][w];
looked = new int[h][w];
count = 1;
for (int i = 0; i < h; i++) {
String s = stdin.next();
for (int j = 0; j < w; j++) {
if (s.charAt(j) == '.')
land[i][j] = 1;
else if (s.charAt(j) == '@') {
land[i][j] = 0;
sx = i;
sy = j;
} else
land[i][j] = 0;
}
}
looked = land.clone();
// System.out.println(land[5][8]);
bfs(sx, sy);
System.out.println(count);
}
}
void bfs(int sx, int sy) {
// System.out.println(sx+" "+sy);
looked[sx][sy] = 0;
for (int i = 0; i < 4; i++) {
int xx = sx + dx[i];
int yy = sy + dy[i];
if (0 <= xx && xx < h && 0 <= yy && yy < w) {
if (land[xx][yy] == 1) {
if (looked[xx][yy] == 1)
count++;
bfs(xx, yy);
}
}
}
}
public static void main(String args[]) {
Main m = new Main();
m.Solve();
}
}
|
[["-", 0, 195, 8, 196, 0, 52, 8, 196, 0, 46], ["+", 0, 195, 8, 196, 0, 52, 8, 196, 0, 46]]
| 3
| 424
|
import static java.lang.System.in;
import static java.lang.System.out;
import java.util.*;
public class Main {
Scanner sc = new Scanner(in);
void run() {
StringTokenizer st;
while (true) {
st = new StringTokenizer(sc.nextLine());
int w = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
if (w == 0 && h == 0)
break;
boolean[][] room = new boolean[h][w];
String s;
int posx, posy;
posx = posy = 0;
for (int i = 0; i < h; i++) {
s = sc.nextLine();
for (int j = 0; j < w; j++) {
if (s.substring(j, j + 1).equals("."))
room[i][j] = true;
else if (s.substring(j, j + 1).equals("@")) {
room[i][j] = true;
posx = i;
posy = j;
}
}
}
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, -1, 0, 1};
Queue<Integer> qx = new LinkedList<Integer>();
Queue<Integer> qy = new LinkedList<Integer>();
qx.add(posx);
qy.add(posy);
int ans = 0;
int x, y;
while (!qx.isEmpty()) {
x = qx.poll();
y = qy.poll();
ans++;
for (int i = 0; i < 4; i++) {
if (x + dx[i] >= 0 && x + dx[i] < h && y + dy[i] >= 0 &&
y + dy[i] < w && room[x + dx[i]][y + dy[i]]) {
room[x + dx[i]][y + dy[i]] = false;
qx.add(x + dx[i]);
qy.add(y + dy[i]);
}
}
}
out.println(ans);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main().run();
}
}
|
import static java.lang.System.in;
import static java.lang.System.out;
import java.util.*;
public class Main {
Scanner sc = new Scanner(in);
void run() {
StringTokenizer st;
while (true) {
st = new StringTokenizer(sc.nextLine());
int w = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
if (w == 0 && h == 0)
break;
boolean[][] room = new boolean[h][w];
String s;
int posx, posy;
posx = posy = 0;
for (int i = 0; i < h; i++) {
s = sc.nextLine();
for (int j = 0; j < w; j++) {
if (s.substring(j, j + 1).equals("."))
room[i][j] = true;
else if (s.substring(j, j + 1).equals("@")) {
room[i][j] = false;
posx = i;
posy = j;
}
}
}
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, -1, 0, 1};
Queue<Integer> qx = new LinkedList<Integer>();
Queue<Integer> qy = new LinkedList<Integer>();
qx.add(posx);
qy.add(posy);
int ans = 0;
int x, y;
while (!qx.isEmpty()) {
x = qx.poll();
y = qy.poll();
ans++;
for (int i = 0; i < 4; i++) {
if (x + dx[i] >= 0 && x + dx[i] < h && y + dy[i] >= 0 &&
y + dy[i] < w && room[x + dx[i]][y + dy[i]]) {
room[x + dx[i]][y + dy[i]] = false;
qx.add(x + dx[i]);
qy.add(y + dy[i]);
}
}
}
out.println(ans);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Main().run();
}
}
|
[["-", 75, 57, 64, 196, 0, 1, 0, 11, 12, 146], ["+", 75, 57, 64, 196, 0, 1, 0, 11, 12, 147]]
| 3
| 510
|
#include <iostream>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int H, W, count = 0;
char **map;
bool **isCount;
bool valid(int x, int y) {
return x <= (W - 1) && x >= 0 && y <= (H - 1) && y >= 0;
}
void bfs(int x, int y) {
if (valid(x, y) && map[y][x] != '#' && !isCount[y][x]) {
count++;
isCount[y][x] = true;
for (int i = 0; i < 4; i++)
bfs(x + dx[i], y + dy[i]);
}
/*int D[100];
for(int i=0;i<100;i++)
D[i]=-1;
queue<int>Q;
Q.push(src);
D[src]=0;
while(!Q.empty())
{
int cur=Q.front();
Q.pop();
for(int dst=0;dst<n;dst++)
{
if(M[cur][dst]==1&&D[dst]==-1)
{
D[dst]=D[cur]+1;
Q.push(dst);
}
}
}
for(int i=0;i<n;i++)
cout<<i+1<<' '<<D[i]<<endl;
*/
}
int main() {
int x, y;
cin >> W >> H;
while (W != 0 && H != 0) {
isCount = new bool *[H];
map = new char *[H];
for (int i = 0; i < H; i++) {
isCount[i] = new bool[W];
map[i] = new char[W];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++)
isCount[i][j] = false;
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> map[i][j];
if (map[i][j] == '@') {
x = j;
y = i;
}
}
}
bfs(x, y);
cout << count << endl;
delete map;
delete isCount;
cin >> W >> H;
}
return 0;
}
|
#include <iostream>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int H, W, count = 0;
char **map;
bool **isCount;
bool valid(int x, int y) {
return x <= (W - 1) && x >= 0 && y <= (H - 1) && y >= 0;
}
void bfs(int x, int y) {
if (valid(x, y) && map[y][x] != '#' && !isCount[y][x]) {
count++;
isCount[y][x] = true;
for (int i = 0; i < 4; i++)
bfs(x + dx[i], y + dy[i]);
}
/*int D[100];
for(int i=0;i<100;i++)
D[i]=-1;
queue<int>Q;
Q.push(src);
D[src]=0;
while(!Q.empty())
{
int cur=Q.front();
Q.pop();
for(int dst=0;dst<n;dst++)
{
if(M[cur][dst]==1&&D[dst]==-1)
{
D[dst]=D[cur]+1;
Q.push(dst);
}
}
}
for(int i=0;i<n;i++)
cout<<i+1<<' '<<D[i]<<endl;
*/
}
int main() {
int x, y;
cin >> W >> H;
while (W != 0 && H != 0) {
isCount = new bool *[H];
map = new char *[H];
for (int i = 0; i < H; i++) {
isCount[i] = new bool[W];
map[i] = new char[W];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++)
isCount[i][j] = false;
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> map[i][j];
if (map[i][j] == '@') {
x = j;
y = i;
}
}
}
bfs(x, y);
cout << count << endl;
delete map;
delete isCount;
cin >> W >> H;
count = 0;
}
return 0;
}
|
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
| 1
| 398
|
#include <iostream>
using namespace std;
char a[22][22];
int w, h;
int yy[4] = {-1, 0, 0, 1}, xx[4] = {0, 1, -1, 0};
int solve(int y, int x) {
if (a[y][x] == '#')
return 0;
a[y][x] = '#';
int ans = 0;
for (int i = 0; i < 4; i++) {
ans += solve(y + yy[i], x + xx[i]);
}
return ans + 1;
}
int main() {
int y, x;
while (cin >> w >> h) {
fill((char *)a, (char *)(a + 22), '#');
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++) {
cin >> a[i][j];
if (a[i][j] == '@') {
y = i;
x = j;
}
}
cout << solve(y, x) << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
char a[22][22];
int w, h;
int yy[4] = {-1, 0, 0, 1}, xx[4] = {0, 1, -1, 0};
int solve(int y, int x) {
if (a[y][x] == '#')
return 0;
a[y][x] = '#';
int ans = 0;
for (int i = 0; i < 4; i++) {
ans += solve(y + yy[i], x + xx[i]);
}
return ans + 1;
}
int main() {
int y, x;
while (cin >> w >> h, w) {
fill((char *)a, (char *)(a + 22), '#');
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++) {
cin >> a[i][j];
if (a[i][j] == '@') {
y = i;
x = j;
}
}
cout << solve(y, x) << endl;
}
return 0;
}
|
[["+", 8, 9, 0, 52, 15, 339, 51, 34, 0, 21], ["+", 8, 9, 0, 52, 15, 339, 51, 34, 12, 22]]
| 1
| 258
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int W, H;
char board[30][30];
int dfs(int y, int x) {
board[y][x] = '#';
int ret = 1;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny >= 0 && ny < H && nx >= 0 && nx < W && board[ny][nx] == '.') {
ret += dfs(y, x);
}
}
return ret;
}
int main() {
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
cin >> board[i];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (board[i][j] == '@') {
cout << dfs(i, j) << endl;
}
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int W, H;
char board[30][30];
int dfs(int y, int x) {
board[y][x] = '#';
int ret = 1;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny >= 0 && ny < H && nx >= 0 && nx < W && board[ny][nx] == '.') {
ret += dfs(ny, nx);
}
}
return ret;
}
int main() {
while (cin >> W >> H, W) {
for (int i = 0; i < H; i++) {
cin >> board[i];
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (board[i][j] == '@') {
cout << dfs(i, j) << endl;
}
}
}
}
return 0;
}
|
[["-", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22]]
| 1
| 262
|
#include <stdio.h>
//#include<windows.h>
int masu[22][22];
int ans = 0;
int i, j;
void saiki(int x, int y) {
/*
for(i=0;i<22;i++){
for(j=0;j<22;j++){
if(masu[i][j]==0)printf(" ");
if(masu[i][j]==1)printf("¡");
}
puts("");
}
printf("%d",ans);
Sleep(100);
system("cls");
*/
masu[x][y] = 1;
ans++;
if (masu[x][y - 1] == 0)
saiki(x, y - 1);
if (masu[x + 1][y] == 0)
saiki(x + 1, y);
if (masu[x][y + 1] == 0)
saiki(x, y + 1);
if (masu[x - 1][y] == 0)
saiki(x - 1, y);
}
int main() {
for (;;) {
int W, H;
int x, y;
char str[22];
for (i = 0; i < 22; i++) {
for (j = 0; j < 22; j++) {
masu[i][j] = 1;
}
}
scanf("%d%d", &W, &H);
if (W == 0) {
break;
}
for (i = 0; i < H; i++) {
scanf("%s", str);
for (j = 0; j < W; j++) {
if (str[j] == '.')
masu[i + 1][j + 1] = 0;
if (str[j] == '@') {
x = i + 1;
y = j + 1;
masu[i + 1][j + 1] = 0;
}
}
}
/*
for(i=0;i<22;i++){
for(j=0;j<22;j++){
if(masu[i][j]==0)printf(" ");
if(masu[i][j]==1)printf("¡");
}
puts("");
}
printf("%d\n",ans);
system("cls");
*/
saiki(x, y);
/*
for(i=0;i<22;i++){
for(j=0;j<22;j++){
if(masu[i][j]==0)printf(" ");
if(masu[i][j]==1)printf("¡");
}
puts("");
}
*/
printf("%d\n", ans);
}
return 0;
}
|
#include <stdio.h>
//#include<windows.h>
int masu[22][22];
int ans = 0;
int i, j;
void saiki(int x, int y) {
/*
for(i=0;i<22;i++){
for(j=0;j<22;j++){
if(masu[i][j]==0)printf(" ");
if(masu[i][j]==1)printf("¡");
}
puts("");
}
printf("%d",ans);
Sleep(100);
system("cls");
*/
masu[x][y] = 1;
ans++;
if (masu[x][y - 1] == 0)
saiki(x, y - 1);
if (masu[x + 1][y] == 0)
saiki(x + 1, y);
if (masu[x][y + 1] == 0)
saiki(x, y + 1);
if (masu[x - 1][y] == 0)
saiki(x - 1, y);
}
int main() {
for (;;) {
ans = 0;
int W, H;
int x, y;
char str[22];
for (i = 0; i < 22; i++) {
for (j = 0; j < 22; j++) {
masu[i][j] = 1;
}
}
scanf("%d%d", &W, &H);
if (W == 0) {
break;
}
for (i = 0; i < H; i++) {
scanf("%s", str);
for (j = 0; j < W; j++) {
if (str[j] == '.')
masu[i + 1][j + 1] = 0;
if (str[j] == '@') {
x = i + 1;
y = j + 1;
masu[i + 1][j + 1] = 0;
}
}
}
/*
for(i=0;i<22;i++){
for(j=0;j<22;j++){
if(masu[i][j]==0)printf(" ");
if(masu[i][j]==1)printf("¡");
}
puts("");
}
printf("%d\n",ans);
system("cls");
*/
saiki(x, y);
/*
for(i=0;i<22;i++){
for(j=0;j<22;j++){
if(masu[i][j]==0)printf(" ");
if(masu[i][j]==1)printf("¡");
}
puts("");
}
*/
printf("%d\n", ans);
}
return 0;
}
|
[["+", 0, 7, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 7, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 7, 8, 9, 0, 1, 0, 35]]
| 1
| 356
|
#include <iostream>
using namespace std;
char map[21][21];
int count;
int x, y;
void dfs(int nx, int ny) {
map[ny][nx] = '#';
count++;
if (nx < x && map[ny][nx + 1] == '.') {
dfs(nx + 1, ny);
}
if (nx > 0 && map[ny][nx - 1] == '.') {
dfs(nx - 1, ny);
}
if (ny < y && map[ny + 1][nx] == '.') {
dfs(nx, ny + 1);
}
if (ny > 0 && map[ny - 1][nx] == '.') {
dfs(nx, ny - 1);
}
return;
}
int main() {
while (1) {
cin >> x >> y;
if (!(x || y))
break;
int ix, iy;
count = 0;
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
cin >> map[i][j];
if (map[i][j] == '@') {
ix = j;
iy = i;
}
}
}
dfs(ix, iy);
cout << count << endl;
}
}
|
#include <iostream>
using namespace std;
char map[21][21];
int count;
int x, y;
void dfs(int nx, int ny) {
map[ny][nx] = '#';
count++;
if (nx + 1 < x && map[ny][nx + 1] == '.') {
dfs(nx + 1, ny);
}
if (nx > 0 && map[ny][nx - 1] == '.') {
dfs(nx - 1, ny);
}
if (ny + 1 < y && map[ny + 1][nx] == '.') {
dfs(nx, ny + 1);
}
if (ny > 0 && map[ny - 1][nx] == '.') {
dfs(nx, ny - 1);
}
return;
}
int main() {
while (1) {
cin >> x >> y;
if (!(x || y))
break;
int ix, iy;
count = 0;
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
cin >> map[i][j];
if (map[i][j] == '@') {
ix = j;
iy = i;
}
}
}
dfs(ix, iy);
cout << count << endl;
}
}
|
[["+", 15, 339, 51, 16, 31, 16, 31, 16, 17, 72], ["+", 15, 339, 51, 16, 31, 16, 31, 16, 12, 13]]
| 1
| 292
|
#include <iostream>
#define MAX (256)
using namespace std;
int N, M;
char field[MAX][MAX];
int sx, sy;
int ans = 0;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void dfs(int x, int y) {
if (0 <= x && x < N && 0 <= y && y < M && field[x][y] == '.') {
field[x][y] = '#';
ans++;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
dfs(nx, ny);
}
}
}
int main() {
while (1) {
cin >> M >> N;
if (M == 0 && N == 0)
break;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> field[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (field[i][j] == '@') {
sx = i;
sy = j;
field[sx][sy] = '.';
}
}
}
dfs(sx, sy);
cout << ans << endl;
}
return 0;
}
|
#include <iostream>
#define MAX (256)
using namespace std;
int N, M;
char field[MAX][MAX];
int sx, sy;
int ans = 0;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
void dfs(int x, int y) {
if (0 <= x && x < N && 0 <= y && y < M && field[x][y] == '.') {
field[x][y] = '#';
ans++;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
dfs(nx, ny);
}
}
}
int main() {
while (1) {
ans = 0;
cin >> M >> N;
if (M == 0 && N == 0)
break;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> field[i][j];
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (field[i][j] == '@') {
sx = i;
sy = j;
field[sx][sy] = '.';
}
}
}
dfs(sx, sy);
cout << ans << endl;
}
return 0;
}
|
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
| 1
| 320
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<vector<int>> vector2d;
int main() {
int H, W, count = 0, kaisuu = 0, kosuu, sum, judge;
char map[20][20];
vector2d nextx, nexty; // nextx[回数][個数]
nextx.reserve(50);
nexty.reserve(50);
while (1) {
cin >> W >> H;
if (W == 0 && H == 0)
break;
//マップの入力
for (int cH = 0; cH < H; cH++) {
for (int cW = 0; cW < W; cW++) {
cin >> map[cW][cH];
if (map[cW][cH] == '@') {
nextx[kaisuu].push_back(cW);
nexty[kaisuu].push_back(cH);
}
}
}
sum = 1;
while (1) {
judge = 0;
for (kosuu = 0; kosuu < nextx[kaisuu].size(); kosuu++) {
if (nextx[kaisuu][kosuu] - 1 != -1) {
if (map[nextx[kaisuu][kosuu] - 1][nexty[kaisuu][kosuu]] ==
'.') { //@の左側をチェック
//もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu] - 1);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu]);
map[nextx[kaisuu][kosuu] - 1][nexty[kaisuu][kosuu]] = '@';
sum++;
judge++;
}
}
if (nexty[kaisuu][kosuu] - 1 != -1) {
if (map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] - 1] ==
'.') { //@の上側をチェック
//もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu]);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu] - 1);
map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] - 1] = '@';
sum++;
judge++;
}
}
if (nextx[kaisuu][kosuu] + 1 != W) {
if (map[nextx[kaisuu][kosuu] + 1][nexty[kaisuu][kosuu]] ==
'.') { //@の右側をチェック
//もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu] + 1);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu]);
map[nextx[kaisuu][kosuu] + 1][nexty[kaisuu][kosuu]] = '@';
sum++;
judge++;
}
}
if (nexty[kaisuu][kosuu] + 1 != H) {
if (map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] + 1] ==
'.') { //@の下側をチェック
//もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu]);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu] + 1);
map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] + 1] = '@';
sum++;
judge++;
}
}
}
if (judge == 0)
break;
kaisuu++;
}
kaisuu++;
cout << sum << endl;
}
return 0;
}
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef vector<vector<int>> vector2d;
int main() {
int H, W, count = 0, kaisuu = 0, kosuu, sum, judge;
char map[20][20];
vector2d nextx, nexty; // nextx[回数][個数]
nextx.reserve(10000);
nexty.reserve(10000);
while (1) {
cin >> W >> H;
if (W == 0 && H == 0)
break;
//マップの入力
for (int cH = 0; cH < H; cH++) {
for (int cW = 0; cW < W; cW++) {
cin >> map[cW][cH];
if (map[cW][cH] == '@') {
nextx[kaisuu].push_back(cW);
nexty[kaisuu].push_back(cH);
}
}
}
sum = 1;
while (1) {
judge = 0;
for (kosuu = 0; kosuu < nextx[kaisuu].size(); kosuu++) {
if (nextx[kaisuu][kosuu] - 1 != -1) {
if (map[nextx[kaisuu][kosuu] - 1][nexty[kaisuu][kosuu]] ==
'.') { //@の左側をチェック
//もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu] - 1);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu]);
map[nextx[kaisuu][kosuu] - 1][nexty[kaisuu][kosuu]] = '@';
sum++;
judge++;
}
}
if (nexty[kaisuu][kosuu] - 1 != -1) {
if (map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] - 1] ==
'.') { //@の上側をチェック
//もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu]);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu] - 1);
map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] - 1] = '@';
sum++;
judge++;
}
}
if (nextx[kaisuu][kosuu] + 1 != W) {
if (map[nextx[kaisuu][kosuu] + 1][nexty[kaisuu][kosuu]] ==
'.') { //@の右側をチェック
//もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu] + 1);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu]);
map[nextx[kaisuu][kosuu] + 1][nexty[kaisuu][kosuu]] = '@';
sum++;
judge++;
}
}
if (nexty[kaisuu][kosuu] + 1 != H) {
if (map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] + 1] ==
'.') { //@の下側をチェック
//もし'@'だったらその座標を保存しそれを@に!
nextx[kaisuu + 1].push_back(nextx[kaisuu][kosuu]);
nexty[kaisuu + 1].push_back(nexty[kaisuu][kosuu] + 1);
map[nextx[kaisuu][kosuu]][nexty[kaisuu][kosuu] + 1] = '@';
sum++;
judge++;
}
}
}
if (judge == 0)
break;
kaisuu++;
}
kaisuu++;
cout << sum << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
| 1
| 708
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
int main() {
int nb_lignes, nb_colonnes;
while (cin >> nb_lignes >> nb_colonnes) {
if (nb_lignes == 0)
break;
int res = 1;
vector<string> lab(nb_lignes);
vector<vector<bool>> visites(nb_lignes, vector<bool>(nb_colonnes, false));
for (int c = 0; c < nb_lignes; c++)
cin >> lab[c];
int xdep, ydep = 0;
for (xdep = 0; xdep < nb_lignes && lab[xdep][ydep] != '@'; xdep++) {
for (ydep = 0; ydep < nb_colonnes && lab[xdep][ydep] != '@'; ydep++)
;
if (lab[xdep][ydep] == '@')
break;
}
vector<pair<int, int>> parcours;
visites[xdep][ydep] = true;
parcours.push_back(make_pair(xdep, ydep));
while (parcours.size()) {
int xact = parcours.back().first;
int yact = parcours.back().second;
parcours.pop_back();
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
for (int c = 0; c < 4; c++) {
int x2 = xact + dx[c];
int y2 = yact + dy[c];
if (x2 >= 0 && x2 < nb_lignes && y2 >= 0 && y2 < nb_colonnes &&
lab[x2][y2] != '#' && !visites[x2][y2]) {
visites[x2][y2] = true;
res++;
parcours.push_back(make_pair(x2, y2));
}
}
}
cout << res << endl;
}
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
int main() {
int nb_lignes, nb_colonnes;
while (cin >> nb_colonnes >> nb_lignes) {
if (nb_lignes == 0)
break;
int res = 1;
vector<string> lab(nb_lignes);
vector<vector<bool>> visites(nb_lignes, vector<bool>(nb_colonnes, false));
for (int c = 0; c < nb_lignes; c++)
cin >> lab[c];
int xdep, ydep = 0;
for (xdep = 0; xdep < nb_lignes && lab[xdep][ydep] != '@'; xdep++) {
for (ydep = 0; ydep < nb_colonnes && lab[xdep][ydep] != '@'; ydep++)
;
if (lab[xdep][ydep] == '@')
break;
}
vector<pair<int, int>> parcours;
visites[xdep][ydep] = true;
parcours.push_back(make_pair(xdep, ydep));
while (parcours.size()) {
int xact = parcours.back().first;
int yact = parcours.back().second;
parcours.pop_back();
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
for (int c = 0; c < 4; c++) {
int x2 = xact + dx[c];
int y2 = yact + dy[c];
if (x2 >= 0 && x2 < nb_lignes && y2 >= 0 && y2 < nb_colonnes &&
lab[x2][y2] != '#' && !visites[x2][y2]) {
visites[x2][y2] = true;
res++;
parcours.push_back(make_pair(x2, y2));
}
}
}
cout << res << endl;
}
}
|
[["-", 0, 52, 15, 339, 51, 16, 31, 16, 12, 22], ["-", 8, 9, 0, 52, 15, 339, 51, 16, 17, 152], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 17, 152], ["+", 8, 9, 0, 52, 15, 339, 51, 16, 12, 22]]
| 1
| 407
|
#include <stdio.h>
#include <string.h>
#define W 21
#define H 21
int w, h;
char map[W][H];
int search(int s, int t) {
if (s < 0 || t < 0 || s >= h || t >= w || map[s][t] == '#')
return 0;
int sum = 1;
map[s][t] = '#';
sum += search(s - 1, t);
sum += search(s, t + 1);
sum += search(s + 1, t);
sum += search(s, t - 1);
return sum;
}
int main() {
int i, j;
while (1) {
int st_w, st_h;
scanf("%d %d\n", &w, &h);
if (!w && !h)
break;
memset(map, ' ', sizeof(map));
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
scanf("%c", &map[i][j]);
if (map[i][j] == '@')
st_w = j;
st_h = i;
}
scanf("\n");
}
printf("%d\n", search(st_h, st_w));
}
exit(0);
}
|
#include <stdio.h>
#include <string.h>
#define W 21
#define H 21
int w, h;
char map[W][H];
int search(int s, int t) {
if (s < 0 || t < 0 || s >= h || t >= w || map[s][t] == '#')
return 0;
int sum = 1;
map[s][t] = '#';
sum += search(s - 1, t);
sum += search(s, t + 1);
sum += search(s + 1, t);
sum += search(s, t - 1);
return sum;
}
int main() {
int i, j;
while (1) {
int st_w, st_h;
scanf("%d %d\n", &w, &h);
if (!w && !h)
break;
memset(map, ' ', sizeof(map));
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
scanf("%c", &map[i][j]);
if (map[i][j] == '@') {
st_w = j;
st_h = i;
}
}
scanf("\n");
}
printf("%d\n", search(st_h, st_w));
}
return 0;
}
|
[["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 7, 8, 9, 0, 7, 8, 9, 0, 46], ["-", 0, 14, 8, 9, 0, 1, 0, 2, 63, 22], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 24], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 38], ["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 25]]
| 0
| 287
|
#include <iostream>
#include <vector>
using namespace std;
const int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};
int dfs(int y, int x, vector<string> &g) {
if (g[y][x] == '.')
return 0;
g[y][x] = '.';
int res = 1;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || nx < 0 || ny >= (int)g.size() || nx >= (int)g[0].size())
continue;
res += dfs(ny, nx, g);
}
return res;
}
int main() {
int w, h;
while (cin >> w >> h) {
if (w == 0 && h == 0)
break;
vector<string> g(h);
int y = 0, x = 0;
for (int i = 0; i < h; i++) {
cin >> g[i];
for (int j = 0; j < w; j++) {
if (g[i][j] == '@')
y = i, x = j;
}
}
cout << dfs(y, x, g) << endl;
}
}
|
#include <iostream>
#include <vector>
using namespace std;
const int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};
int dfs(int y, int x, vector<string> &g) {
if (g[y][x] == '#')
return 0;
g[y][x] = '#';
int res = 1;
for (int i = 0; i < 4; i++) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || nx < 0 || ny >= (int)g.size() || nx >= (int)g[0].size())
continue;
res += dfs(ny, nx, g);
}
return res;
}
int main() {
int w, h;
while (cin >> w >> h) {
if (w == 0 && h == 0)
break;
vector<string> g(h);
int y = 0, x = 0;
for (int i = 0; i < h; i++) {
cin >> g[i];
for (int j = 0; j < w; j++) {
if (g[i][j] == '@')
y = i, x = j;
}
}
cout << dfs(y, x, g) << endl;
}
}
|
[["-", 0, 57, 15, 339, 51, 16, 12, 103, 0, 125], ["+", 0, 57, 15, 339, 51, 16, 12, 103, 0, 125], ["-", 8, 9, 0, 1, 0, 11, 12, 103, 0, 125], ["+", 8, 9, 0, 1, 0, 11, 12, 103, 0, 125]]
| 1
| 301
|
#include <iostream>
using namespace std;
char map[20][20];
int W, H, start_x, start_y, ans = 0;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void dfs(int x, int y) {
ans++;
if (map[x][y] != '#') {
map[x][y] = '#';
for (int i = 0; i < 4; i++) {
int next_x = x + dx[i], next_y = y + dy[i];
if (next_x < W && next_x >= 0 && next_y < H && next_y >= 0) {
if (map[next_x][next_y] == '#')
continue;
dfs(next_x, next_y);
} else {
continue;
}
}
}
}
int main() {
while (1) {
cin >> W >> H;
if (W == 0 && H == 0) {
break;
} else {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> map[j][i];
if (map[j][i] == '@') {
start_x = j;
start_y = i;
}
}
}
dfs(start_x, start_y);
cout << ans << endl;
}
}
}
|
#include <iostream>
using namespace std;
char map[20][20];
int W, H, start_x, start_y, ans = 0;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
void dfs(int x, int y) {
ans++;
if (map[x][y] != '#') {
map[x][y] = '#';
for (int i = 0; i < 4; i++) {
int next_x = x + dx[i], next_y = y + dy[i];
if (next_x < W && next_x >= 0 && next_y < H && next_y >= 0) {
if (map[next_x][next_y] == '#')
continue;
dfs(next_x, next_y);
} else {
continue;
}
}
}
}
int main() {
while (1) {
cin >> W >> H;
if (W == 0 && H == 0) {
break;
} else {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> map[j][i];
if (map[j][i] == '@') {
start_x = j;
start_y = i;
}
}
}
dfs(start_x, start_y);
cout << ans << endl;
ans = 0;
}
}
}
|
[["+", 75, 76, 0, 9, 0, 1, 0, 11, 31, 22], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 17, 32], ["+", 75, 76, 0, 9, 0, 1, 0, 11, 12, 13], ["+", 0, 57, 75, 76, 0, 9, 0, 1, 0, 35]]
| 1
| 297
|
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
char grid[100][100];
int w, h;
int ans = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
bool searched[100][100];
int a = 0, b = 0;
bool inrange(int x, int y) {
if (x < 0 || y < 0 || x > h || y > w)
return false;
else
return true;
}
void search(int x, int y) {
if ((grid[x][y] == '.' || grid[x][y] == '@') && searched[x][y] == false &&
inrange(x, y)) {
searched[x][y] = true;
ans++;
REP(i, 4) { search(x + dx[i], y + dy[i]); }
}
}
int main() {
while (1) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
ans = 0;
REP(i, h) {
REP(j, w) {
cin >> grid[i][j];
searched[i][j] = false;
if (grid[i][j] == '@') {
a = i;
b = j;
}
}
}
search(a, b);
cout << ans << endl;
}
}
|
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
char grid[100][100];
int w, h;
int ans = 0;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
bool searched[100][100];
int a = 0, b = 0;
bool inrange(int x, int y) {
if (x < 0 || y < 0 || x >= h || y >= w)
return false;
else
return true;
}
void search(int x, int y) {
if ((grid[x][y] == '.' || grid[x][y] == '@') && searched[x][y] == false &&
inrange(x, y)) {
searched[x][y] = true;
ans++;
// cout <<x <<' '<<y << endl;
REP(i, 4) { search(x + dx[i], y + dy[i]); }
}
}
int main() {
while (1) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
ans = 0;
REP(i, h) {
REP(j, w) {
cin >> grid[i][j];
searched[i][j] = false;
if (grid[i][j] == '@') {
a = i;
b = j;
}
}
}
search(a, b);
cout << ans << endl;
}
}
|
[["-", 15, 339, 51, 16, 31, 16, 12, 16, 17, 47], ["+", 15, 339, 51, 16, 31, 16, 12, 16, 17, 20], ["-", 0, 57, 15, 339, 51, 16, 12, 16, 17, 47], ["+", 0, 57, 15, 339, 51, 16, 12, 16, 17, 20]]
| 1
| 434
|
#include <iostream>
#include <string>
using namespace std;
int w, h;
string map[40];
int vec[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int move(int x, int y) {
int ans = 0;
map[x][y] = '#';
for (int i = 0; i < 4; i++) {
if ((x + vec[i][0] >= 0) && (y + vec[i][1] >= 0) && (x + vec[i][0] < h) &&
(y + vec[i][1] < w)) {
if (map[x + vec[i][0]][y + vec[i][1]] == '.') {
ans += move(x + vec[i][0], y + vec[i][1]);
}
}
}
return ans;
}
int main() {
while (cin >> w >> h, w) {
int fx, fy;
for (int i = 0; i < h; i++) {
cin >> map[i];
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (map[i][j] == '@') {
fx = i;
fy = j;
break;
}
}
}
int ans = move(fx, fy);
cout << ans << endl;
}
}
|
#include <iostream>
#include <string>
using namespace std;
int w, h;
string map[40];
int vec[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int move(int x, int y) {
int ans = 1;
map[x][y] = '#';
for (int i = 0; i < 4; i++) {
if ((x + vec[i][0] >= 0) && (y + vec[i][1] >= 0) && (x + vec[i][0] < h) &&
(y + vec[i][1] < w)) {
if (map[x + vec[i][0]][y + vec[i][1]] == '.') {
ans += move(x + vec[i][0], y + vec[i][1]);
}
}
}
return ans;
}
int main() {
while (cin >> w >> h, w) {
int fx, fy;
for (int i = 0; i < h; i++) {
cin >> map[i];
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (map[i][j] == '@') {
fx = i;
fy = j;
break;
}
}
}
int ans = move(fx, fy);
cout << ans << endl;
}
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 50, 51, 13]]
| 1
| 338
|
//
// RedAndBlack.cpp
//
//
// Created by ??°????????? on 2015/10/30.
//
//
#include <iostream>
using namespace std;
int w, h, num = 0, room[20][20];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 0};
void dfs(int x, int y) {
num++;
room[y][x] = 0;
// ??????????????¢?´¢
for (int i = 0; i < 4; i++) {
int mx = x + dx[i];
int my = y + dy[i];
if (mx < 0 || w <= mx)
continue;
if (my < 0 || h <= my)
continue;
if (room[my][mx] == 0)
continue;
dfs(mx, my);
}
return;
}
int main() {
while (cin >> w >> h, w || h) {
int manX, manY;
for (int i = 0; i < h; i++) {
string s;
cin >> s;
for (int j = 0; j < w; j++) {
if (s[j] == '.') {
room[i][j] = 1;
} else if (s[j] == '#') {
room[i][j] = 0;
} else if (s[j] == '@') {
room[i][j] = 1;
manX = j;
manY = i;
}
}
}
dfs(manX, manY);
cout << num << endl;
num = 0;
}
return 0;
}
|
//
// RedAndBlack.cpp
//
//
// Created by ??°????????? on 2015/10/30.
//
//
#include <iostream>
using namespace std;
int w, h, num = 0, room[20][20];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
void dfs(int x, int y) {
num++;
room[y][x] = 0;
// ??????????????¢?´¢
for (int i = 0; i < 4; i++) {
int mx = x + dx[i];
int my = y + dy[i];
if (mx < 0 || w <= mx)
continue;
if (my < 0 || h <= my)
continue;
if (room[my][mx] == 0)
continue;
dfs(mx, my);
}
return;
}
int main() {
while (cin >> w >> h, w || h) {
int manX, manY;
for (int i = 0; i < h; i++) {
string s;
cin >> s;
for (int j = 0; j < w; j++) {
if (s[j] == '.') {
room[i][j] = 1;
} else if (s[j] == '#') {
room[i][j] = 0;
} else if (s[j] == '@') {
room[i][j] = 1;
manX = j;
manY = i;
}
}
}
dfs(manX, manY);
cout << num << endl;
num = 0;
}
return 0;
}
|
[["-", 0, 30, 0, 43, 49, 50, 51, 83, 0, 13], ["+", 0, 30, 0, 43, 49, 50, 51, 83, 0, 13]]
| 1
| 331
|
#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
struct aa {
int y;
int x;
bool black;
};
int main() {
while (1) {
int w, h;
cin >> w >> h;
if (!w)
break;
vector<vector<int>> field(h + 2, vector<int>(w + 2, 0));
int sx, sy;
for (int i = 0; i < h; ++i) {
string st;
cin >> st;
for (int j = 0; j < w; ++j) {
if (st[j] == '.') {
field[i + 1][j + 1] = 1;
} else {
if (st[j] == '#') {
field[i + 1][j + 1] = 2;
} else {
sx = j + 1;
sy = i + 1;
field[i + 1][j + 1] = 1;
}
}
}
}
vector<aa> que;
que.push_back(aa{sy, sx, true});
int ans = 1;
vector<vector<bool>> used(h + 2, vector<bool>(w + 2, false));
used[sy][sx] = true;
while (!que.empty()) {
aa atop(que.back());
que.pop_back();
for (int i = 0; i < 4; ++i) {
const int ny = atop.y + dy[i];
const int nx = atop.x + dx[i];
if (atop.black) {
if (field[ny][nx] == 2) {
if (!used[ny][nx]) {
used[ny][nx] = true;
ans++;
que.push_back(aa{ny, nx, !atop.black});
}
}
} else {
if (field[ny][nx] == 1) {
if (!used[ny][nx]) {
used[ny][nx] = true;
ans++;
que.push_back(aa{ny, nx, !atop.black});
}
}
}
}
}
cout << ans << endl;
}
return 0;
}
|
#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
struct aa {
int y;
int x;
bool black;
};
int main() {
while (1) {
int w, h;
cin >> w >> h;
if (!w)
break;
vector<vector<int>> field(h + 2, vector<int>(w + 2, 0));
int sx, sy;
for (int i = 0; i < h; ++i) {
string st;
cin >> st;
for (int j = 0; j < w; ++j) {
if (st[j] == '.') {
field[i + 1][j + 1] = 1;
} else {
if (st[j] == '#') {
field[i + 1][j + 1] = 2;
} else {
sx = j + 1;
sy = i + 1;
field[i + 1][j + 1] = 1;
}
}
}
}
vector<aa> que;
que.push_back(aa{sy, sx, true});
int ans = 1;
vector<vector<bool>> used(h + 2, vector<bool>(w + 2, false));
used[sy][sx] = true;
while (!que.empty()) {
aa atop(que.back());
que.pop_back();
for (int i = 0; i < 4; ++i) {
const int ny = atop.y + dy[i];
const int nx = atop.x + dx[i];
if (atop.black) {
if (field[ny][nx] == 1) {
if (!used[ny][nx]) {
used[ny][nx] = true;
ans++;
que.push_back(aa{ny, nx, !atop.black});
}
}
} else {
if (field[ny][nx] == 1) {
if (!used[ny][nx]) {
used[ny][nx] = true;
ans++;
que.push_back(aa{ny, nx, !atop.black});
}
}
}
}
}
cout << ans << endl;
}
return 0;
}
|
[["-", 64, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
| 1
| 510
|
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
int ans = 0;
void bfs(char **tile, int w, int h) {
ans++;
tile[w][h] = '#';
if (tile[w][h - 1] == '.')
bfs(tile, w, h - 1);
if (tile[w][h + 1] == '.')
bfs(tile, w, h + 1);
if (tile[w - 1][h] == '.')
bfs(tile, w - 1, h);
if (tile[w + 1][h] == '.')
bfs(tile, w + 1, h);
return;
}
int main() {
int W, H, index_w, index_h;
while (true) {
cin >> W >> H;
if (W == 0 && H == 0)
break;
char **tile;
tile = new char *[H + 2];
REP(i, H + 2) tile[i] = new char[W + 2];
REP(i, H + 2) {
REP(j, W + 2) {
if (i == 0 || i == H + 1 || j == 0 || j == W + 1)
tile[i][j] = '#'; // sentry?
else
scanf(" %c", &tile[i][j]);
if (tile[i][j] == '@') {
index_h = j;
index_w = i;
}
}
}
bfs(tile, index_w, index_h);
REP(i, H) delete[] tile[i];
delete[] tile;
cout << ans << endl;
}
return 0;
}
|
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
using namespace std;
int ans = 0;
void bfs(char **tile, int w, int h) {
ans++;
tile[w][h] = '#';
if (tile[w][h - 1] == '.')
bfs(tile, w, h - 1);
if (tile[w][h + 1] == '.')
bfs(tile, w, h + 1);
if (tile[w - 1][h] == '.')
bfs(tile, w - 1, h);
if (tile[w + 1][h] == '.')
bfs(tile, w + 1, h);
return;
}
int main() {
int W, H, index_w, index_h;
while (true) {
cin >> W >> H;
if (W == 0 && H == 0)
break;
char **tile;
tile = new char *[H + 2];
REP(i, H + 2) tile[i] = new char[W + 2];
REP(i, H + 2) {
REP(j, W + 2) {
if (i == 0 || i == H + 1 || j == 0 || j == W + 1)
tile[i][j] = '#'; // sentry?
else
scanf(" %c", &tile[i][j]);
if (tile[i][j] == '@') {
index_h = j;
index_w = i;
}
}
}
ans = 0;
bfs(tile, index_w, index_h);
REP(i, H) delete[] tile[i];
delete[] tile;
cout << ans << endl;
}
return 0;
}
|
[["+", 0, 52, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 17, 32], ["+", 0, 52, 8, 9, 0, 1, 0, 11, 12, 13], ["+", 8, 9, 0, 52, 8, 9, 0, 1, 0, 35]]
| 1
| 412
|
/*
* http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp
*/
#include <iostream>
#include <limits>
constexpr size_t MAX_HEIGHT = 20;
const char START_TILE = '@';
const char PASSABLE_TILE = '.';
const char PROHIBITED_TILE = '#';
const char PASSED_TILE = 'P';
bool is_wall(std::string world[MAX_HEIGHT], size_t x, size_t y) {
char tile = world[y][x];
return tile == PROHIBITED_TILE || tile == PASSED_TILE;
}
bool valid(std::string world[MAX_HEIGHT], size_t width, size_t height, size_t x,
size_t y) {
return x < width && y < height // x and y is unsigned
&& !is_wall(world, x, y);
}
int walk(std::string world[MAX_HEIGHT], size_t width, size_t height, size_t x,
size_t y) {
if (!valid(world, width, height, x, y)) {
return 0; // ????????????????????\?????????????£?or??¢?´¢??????
}
int total = 1; // ????????????????????\?????????1?????¨??????????????????
world[y][x] =
PASSED_TILE; // ??????????????¨????????????????????¨?????????????????????????????????
// ?????????????????§??????????????????valid???????????????walk????????§
total += walk(world, width, height, x - 1, y);
total += walk(world, width, height, x + 1, y);
total += walk(world, width, height, x, y - 1);
total += walk(world, width, height, x, y + 1);
return total;
}
void process(size_t width, size_t height) {
std::string world[MAX_HEIGHT];
size_t x = std::numeric_limits<size_t>::max(),
y = std::numeric_limits<size_t>::max();
for (size_t i = 0; i < height; ++i) {
std::cin >> world[i];
size_t start_index = world[i].find(START_TILE);
if (start_index != std::string::npos) {
x = start_index;
y = i;
}
}
std::cout << walk(world, width, height, x, y);
}
int main() {
size_t width, height;
while (true) {
std::cin >> width >> height;
if (width == 0 || height == 0) {
break;
} else {
process(width, height);
}
}
}
|
/*
* http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1130&lang=jp
*/
#include <iostream>
#include <limits>
constexpr size_t MAX_HEIGHT = 20;
const char START_TILE = '@';
const char PASSABLE_TILE = '.';
const char PROHIBITED_TILE = '#';
const char PASSED_TILE = 'P';
bool is_wall(std::string world[MAX_HEIGHT], size_t x, size_t y) {
char tile = world[y][x];
return tile == PROHIBITED_TILE || tile == PASSED_TILE;
}
bool valid(std::string world[MAX_HEIGHT], size_t width, size_t height, size_t x,
size_t y) {
return x < width && y < height // x and y is unsigned
&& !is_wall(world, x, y);
}
int walk(std::string world[MAX_HEIGHT], size_t width, size_t height, size_t x,
size_t y) {
if (!valid(world, width, height, x, y)) {
return 0; // ????????????????????\?????????????£?or??¢?´¢??????
}
int total = 1; // ????????????????????\?????????1?????¨??????????????????
world[y][x] =
PASSED_TILE; // ??????????????¨????????????????????¨?????????????????????????????????
// ?????????????????§??????????????????valid???????????????walk????????§
total += walk(world, width, height, x - 1, y);
total += walk(world, width, height, x + 1, y);
total += walk(world, width, height, x, y - 1);
total += walk(world, width, height, x, y + 1);
return total;
}
void process(size_t width, size_t height) {
std::string world[MAX_HEIGHT];
size_t x = std::numeric_limits<size_t>::max(),
y = std::numeric_limits<size_t>::max();
for (size_t i = 0; i < height; ++i) {
std::cin >> world[i];
size_t start_index = world[i].find(START_TILE);
if (start_index != std::string::npos) {
x = start_index;
y = i;
}
}
std::cout << walk(world, width, height, x, y) << std::endl;
}
int main() {
size_t width, height;
while (true) {
std::cin >> width >> height;
if (width == 0 || height == 0) {
break;
} else {
process(width, height);
}
}
}
|
[["+", 0, 14, 8, 9, 0, 1, 0, 16, 17, 151], ["+", 8, 9, 0, 1, 0, 16, 12, 343, 345, 348], ["+", 8, 9, 0, 1, 0, 16, 12, 343, 0, 349], ["+", 8, 9, 0, 1, 0, 16, 12, 343, 141, 22]]
| 1
| 433
|
#include <iostream>
using namespace std;
typedef pair<int, int> pii;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char fld[20][20];
bool used[20][20];
int W, H;
void rec(int i, int j) {
used[i][j] = 1;
for (int k = 0; k < 4; k++) {
int ni = i + dy[k];
int nj = j + dx[k];
if ((0 <= ni && ni < H && 0 <= nj && nj < W) && fld[ni][nj] == '#' &&
used[ni][nj] == 0) {
rec(ni, nj);
}
}
}
int main() {
while (1) {
cin >> W >> H;
if (!(H || W))
break;
int sx, sy;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> fld[i][j];
if (fld[i][j] == '@') {
sy = i;
sx = j;
}
}
}
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
used[i][j] = 0;
used[sy][sx] = 1;
rec(sy, sx);
int ans = 0;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) {
ans += used[i][j];
}
cout << ans << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
typedef pair<int, int> pii;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
char fld[20][21];
int used[20][20];
int W, H;
void rec(int i, int j) {
used[i][j] = 1;
for (int k = 0; k < 4; k++) {
int ni = i + dy[k];
int nj = j + dx[k];
if (0 <= ni && ni < H && 0 <= nj && nj < W && fld[ni][nj] != '#' &&
used[ni][nj] == 0) {
rec(ni, nj);
}
}
}
int main() {
while (1) {
cin >> W >> H;
if (!(H || W))
break;
int sx, sy;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cin >> fld[i][j];
if (fld[i][j] == '@') {
sy = i;
sx = j;
}
}
}
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
used[i][j] = 0;
used[sy][sx] = 1;
rec(sy, sx);
int ans = 0;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) {
ans += used[i][j];
}
cout << ans << endl;
}
return 0;
}
|
[["-", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 80, 81, 13], ["-", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["+", 36, 36, 36, 36, 0, 30, 0, 43, 39, 40], ["-", 0, 7, 8, 9, 0, 57, 15, 339, 0, 24], ["-", 15, 339, 51, 16, 31, 16, 31, 23, 0, 25], ["-", 15, 339, 51, 16, 31, 16, 12, 16, 17, 60], ["+", 15, 339, 51, 16, 31, 16, 12, 16, 17, 79]]
| 1
| 385
|
#include <iostream>
using namespace std;
int w, h;
int tx[4] = {0, -1, 0, 1};
int ty[4] = {-1, 0, 1, 0};
char map[21][21];
int slv(int x, int y) {
map[y][x] = '#';
int ans = 0;
for (int i = 0; i < 4; ++i)
if (x + tx[i] != -1 || y + ty[i] != -1 || x + tx[i] != w || y + ty[i] != h)
if (map[y + ty[i]][x + tx[i]] == '.')
ans += slv(x + tx[i], y + ty[i]) + 1;
return ans;
}
int main() {
int ix, iy;
while (1) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
for (int i = 0; i < h; ++i)
for (int j = 0; j < w; ++j) {
cin >> map[i][j];
if (map[i][j] == '@') {
ix = j;
iy = i;
}
}
cout << slv(ix, iy) + 1 << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
int w, h;
int tx[4] = {0, -1, 0, 1};
int ty[4] = {-1, 0, 1, 0};
char map[21][21];
int slv(int x, int y) {
map[y][x] = '#';
int ans = 0;
for (int i = 0; i < 4; ++i)
if (x + tx[i] != -1 && y + ty[i] != -1 && x + tx[i] != w && y + ty[i] != h)
if (map[y + ty[i]][x + tx[i]] == '.')
ans += slv(x + tx[i], y + ty[i]) + 1;
return ans;
}
int main() {
int ix, iy;
while (1) {
cin >> w >> h;
if (w == 0 && h == 0)
break;
for (int i = 0; i < h; ++i)
for (int j = 0; j < w; ++j) {
cin >> map[i][j];
if (map[i][j] == '@') {
ix = j;
iy = i;
}
}
cout << slv(ix, iy) + 1 << endl;
}
return 0;
}
|
[["-", 15, 339, 51, 16, 31, 16, 31, 16, 17, 106], ["+", 15, 339, 51, 16, 31, 16, 31, 16, 17, 98], ["-", 8, 57, 15, 339, 51, 16, 31, 16, 17, 106], ["+", 8, 57, 15, 339, 51, 16, 31, 16, 17, 98], ["-", 0, 7, 8, 57, 15, 339, 51, 16, 17, 106], ["+", 0, 7, 8, 57, 15, 339, 51, 16, 17, 98]]
| 1
| 295
|
#include <bits/stdc++.h>
using namespace std;
static int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int h, w, r;
string s[20];
void dfs(int y, int x) {
r++;
s[y][x] = '#';
for (int i = 0; i < 4; i++)
if (0 <= y + dy[i] && y + dy[i] < h && 0 <= x + dx[i] && x + dx[i] < w)
if (s[y + dy[i]][x + dx[i]] == '.')
dfs(y + dy[i], x + dx[i]);
}
int main() {
int y, x;
for (; cin >> w >> h;) {
r = 0;
for (int i = 0; i < h; i++)
cin >> s[i];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (s[i][j] == '@')
y = i, x = j;
dfs(y, x);
cout << r << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
static int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int h, w, r;
string s[20];
void dfs(int y, int x) {
r++;
s[y][x] = '#';
for (int i = 0; i < 4; i++)
if (0 <= y + dy[i] && y + dy[i] < h && 0 <= x + dx[i] && x + dx[i] < w)
if (s[y + dy[i]][x + dx[i]] == '.')
dfs(y + dy[i], x + dx[i]);
}
int main() {
int y, x;
for (; cin >> w >> h, w;) {
r = 0;
for (int i = 0; i < h; i++)
cin >> s[i];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (s[i][j] == '@')
y = i, x = j;
dfs(y, x);
cout << r << endl;
}
return 0;
}
|
[["+", 0, 14, 8, 9, 0, 7, 15, 34, 0, 21], ["+", 0, 14, 8, 9, 0, 7, 15, 34, 12, 22]]
| 1
| 284
|
while True:
N = int(input())
if N == 0: break
W,H = map(int,input().split())
fld = [[0 for w in range(W)] for h in range(H)]
for i in range(N):
x,y = map(lambda s:int(s)-1,raw_input().split())
fld[y][x] = 1
cums = [[0 for w in range(W+1)] for h in range(H+1)]
for y in range(H):
for x in range(W):
cums[y+1][x+1] = fld[y][x] + cums[y][x+1] + cums[y+1][x] - cums[y][x]
S,T = map(int,input().split())
ans = 0
for y in range(H-T+1):
for x in range(W-S+1):
ans = max(ans, cums[y+T][x+S] - cums[y+T][x] - cums[y][x+S] + cums[y][x])
print(ans)
|
while True:
N = int(input())
if N == 0: break
W,H = map(int,input().split())
fld = [[0 for w in range(W)] for h in range(H)]
for i in range(N):
x,y = map(lambda s:int(s)-1,input().split())
fld[y][x] = 1
cums = [[0 for w in range(W+1)] for h in range(H+1)]
for y in range(H):
for x in range(W):
cums[y+1][x+1] = fld[y][x] + cums[y][x+1] + cums[y+1][x] - cums[y][x]
S,T = map(int,input().split())
ans = 0
for y in range(H-T+1):
for x in range(W-S+1):
ans = max(ans, cums[y+T][x+S] - cums[y+T][x] - cums[y][x+S] + cums[y][x])
print(ans)
|
[["-", 3, 4, 0, 652, 63, 319, 500, 652, 63, 22], ["+", 3, 4, 0, 652, 63, 319, 500, 652, 63, 22]]
| 5
| 276
|
import java.util.*;
public class Main {
private void doit() {
Scanner sc = new Scanner(System.in);
while (true) {
int w = sc.nextInt();
int h = sc.nextInt();
if ((w | h) == 0)
break;
char[][] data = new char[h][w];
for (int i = 0; i < h; i++) {
String s = sc.next();
for (int j = 0; j < w; j++) {
data[i][j] = s.charAt(j);
}
}
String[][] dp = new String[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (Character.isDigit(data[i][j]) && (data[i][j] != '0')) {
dp[i][j] = data[i][j] + "";
} else {
dp[i][j] = "";
}
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (!Character.isDigit(data[i][j]))
continue;
if (data[i][j] == '0' && dp[i][j].equals(""))
continue;
if (i + 1 != h) {
if (dp[i + 1][j].length() < dp[i][j].length() + 1) {
dp[i + 1][j] = dp[i][j] + data[i + 1][j];
} else if (dp[i + 1][j].length() == dp[i][j].length() + 1 &&
dp[i + 1][j].compareTo(dp[i][j] + data[i + 1][j]) < 0) {
dp[i + 1][j] = dp[i][j] + data[i + 1][j];
}
}
if (j + 1 != w) {
if (dp[i][j + 1].length() < dp[i][j].length() + 1) {
dp[i][j + 1] = dp[i][j] + data[i][j + 1];
} else if (dp[i][j + 1].length() == dp[i][j].length() + 1 &&
dp[i][j + 1].compareTo(dp[i][j] + data[i][j + 1]) < 0) {
dp[i][j + 1] = dp[i][j] + data[i][j + 1];
}
}
}
}
// debug
// for(int i = 0 ; i < h; i++){
// for(int j = 0 ; j < w; j++){
// if(dp[i][j].equals("")){
// System.out.print(". ");
// }
// else{
// System.out.print(dp[i][j]
//+ " ");
//
// }
// }
// System.out.println();
// }
String ans = "";
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (!Character.isDigit(data[i][j]))
continue;
if (ans.length() < dp[i][j].length() + 1) {
ans = dp[i][j];
} else if (ans.length() == dp[i][j].length() &&
ans.compareTo(dp[i][j]) < 0) {
ans = dp[i][j];
}
}
}
System.out.println(ans);
}
}
public static void main(String[] args) { new Main().doit(); }
}
|
import java.util.*;
public class Main {
private void doit() {
Scanner sc = new Scanner(System.in);
while (true) {
int w = sc.nextInt();
int h = sc.nextInt();
if ((w | h) == 0)
break;
char[][] data = new char[h][w];
for (int i = 0; i < h; i++) {
String s = sc.next();
for (int j = 0; j < w; j++) {
data[i][j] = s.charAt(j);
}
}
String[][] dp = new String[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (Character.isDigit(data[i][j]) && (data[i][j] != '0')) {
dp[i][j] = data[i][j] + "";
} else {
dp[i][j] = "";
}
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (!Character.isDigit(data[i][j]))
continue;
if (data[i][j] == '0' && dp[i][j].equals(""))
continue;
if (i + 1 != h) {
if (dp[i + 1][j].length() < dp[i][j].length() + 1) {
dp[i + 1][j] = dp[i][j] + data[i + 1][j];
} else if (dp[i + 1][j].length() == dp[i][j].length() + 1 &&
dp[i + 1][j].compareTo(dp[i][j] + data[i + 1][j]) < 0) {
dp[i + 1][j] = dp[i][j] + data[i + 1][j];
}
}
if (j + 1 != w) {
if (dp[i][j + 1].length() < dp[i][j].length() + 1) {
dp[i][j + 1] = dp[i][j] + data[i][j + 1];
} else if (dp[i][j + 1].length() == dp[i][j].length() + 1 &&
dp[i][j + 1].compareTo(dp[i][j] + data[i][j + 1]) < 0) {
dp[i][j + 1] = dp[i][j] + data[i][j + 1];
}
}
}
}
// debug
// for(int i = 0 ; i < h; i++){
// for(int j = 0 ; j < w; j++){
// if(dp[i][j].equals("")){
// System.out.print(". ");
// }
// else{
// System.out.print(dp[i][j]
//+ " ");
//
// }
// }
// System.out.println();
// }
String ans = "";
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (!Character.isDigit(data[i][j]))
continue;
if (ans.length() < dp[i][j].length()) {
ans = dp[i][j];
} else if (ans.length() == dp[i][j].length() &&
ans.compareTo(dp[i][j]) < 0) {
ans = dp[i][j];
}
}
}
System.out.println(ans);
}
}
public static void main(String[] args) { new Main().doit(); }
}
|
[["-", 0, 57, 15, 15, 0, 16, 12, 16, 17, 72], ["-", 0, 57, 15, 15, 0, 16, 12, 16, 12, 499]]
| 3
| 836
|
#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
char field[71][71];
string dp[71][71];
bool str_less(const string &a, const string &b) {
if (a.size() < b.size()) {
return true;
}
if (a.size() > b.size()) {
return false;
}
return a < b;
}
int main() {
while (true) {
int W, H;
cin >> W >> H;
if (W == 0 && H == 0) {
break;
}
memset(field, 0, sizeof(field));
for (int i = 0; i < H; ++i) {
cin >> field[i];
}
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
dp[i][j] = "";
}
}
string answer = "";
for (int i = H - 1; i >= 0; --i) {
for (int j = W - 1; j >= 0; --j) {
if (isdigit(field[i][j])) {
if (str_less(dp[i + 1][j], dp[i][j + 1])) {
dp[i][j] = field[i][j] + dp[i][j + 1];
} else {
dp[i][j] = field[i][j] + dp[i + 1][j];
}
string value = "";
for (int k = 0; k < dp[i][j].size(); ++k) {
if (dp[i][j][k] != '0') {
value = string(dp[i][j].c_str() + k);
break;
}
}
if (str_less(answer, value)) {
answer = value;
}
}
}
}
cout << answer << endl;
}
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
typedef long long ll;
char field[71][71];
string dp[71][71];
bool str_less(const string &a, const string &b) {
if (a.size() < b.size()) {
return true;
}
if (a.size() > b.size()) {
return false;
}
return a < b;
}
int main() {
while (true) {
int W, H;
cin >> W >> H;
if (W == 0 && H == 0) {
break;
}
memset(field, 0, sizeof(field));
for (int i = 0; i < H; ++i) {
cin >> field[i];
}
for (int i = 0; i <= H; ++i) {
for (int j = 0; j <= W; ++j) {
dp[i][j] = "";
}
}
string answer = "";
for (int i = H - 1; i >= 0; --i) {
for (int j = W - 1; j >= 0; --j) {
if (isdigit(field[i][j])) {
if (str_less(dp[i + 1][j], dp[i][j + 1])) {
dp[i][j] = field[i][j] + dp[i][j + 1];
} else {
dp[i][j] = field[i][j] + dp[i + 1][j];
}
string value = "";
for (int k = 0; k < dp[i][j].size(); ++k) {
if (dp[i][j][k] != '0') {
value = string(dp[i][j].c_str() + k);
break;
}
}
if (str_less(answer, value)) {
answer = value;
}
}
}
}
cout << answer << endl;
}
return 0;
}
|
[["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 19], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 17, 18], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 17, 19]]
| 1
| 445
|
#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
static const double EPS = 1e-8;
int tx[] = {-1, 0, 1, 0};
int ty[] = {0, 1, 0, -1};
char stage[101][101];
int W, H;
string larger(const string &str1, const string &str2) {
if (str1.size() > str2.size())
return str1;
if (str1.size() < str2.size())
return str2;
for (int i = 0; i < str1.size(); i++) {
if (str1[i] > str2[i])
return str1;
if (str1[i] < str2[i])
return str2;
}
return str1;
}
void dfs(int x, int y, string buf, string &res) {
buf.push_back(stage[y][x]);
if (y + 1 < H && isdigit(stage[y + 1][x])) {
dfs(x, y + 1, buf, res);
}
if (x + 1 < W && isdigit(stage[y][x + 1])) {
dfs(x + 1, y, buf, res);
}
res = larger(res, buf);
}
int main() {
while (~scanf("%d %d", &W, &H)) {
if (W == H && H == 0)
break;
for (int y = 0; y < H; y++) {
char buf[128];
scanf("%s", buf);
for (int x = 0; x < W; x++) {
stage[y][x] = buf[x];
}
}
string dp[101][101];
// for(int x=0;x<W;x++)
// if(isdigit(stage[0][x]) && stage[0][x] != '0'){
// dp[1][x+1] = dp[0][x] + stage[0][x];
// printf("dp[%d][%d]=%s\n",0,x+1,dp[0][x+1].c_str());
//}
// for(int y=0;x<W;y++)
// if(isdigit(stage[0][x]) && stage[0][x] != '0'){
// dp[1][x+1] = dp[0][x] + stage[0][x];
// printf("dp[%d][%d]=%s\n",0,x+1,dp[0][x+1].c_str());
//}
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
string left = "";
if (isdigit(stage[y][x])) {
if (!(dp[y + 1][x].size() == 0 && stage[y][x] == '0')) {
left = dp[y + 1][x] + stage[y][x];
}
}
string up = "";
if (isdigit(stage[y][x])) {
if (!(dp[y][x].size() == 0 && stage[y][x] == '0')) {
up = dp[y][x + 1] + stage[y][x];
}
}
dp[y + 1][x + 1] = larger(left, up);
}
}
string res = "0";
for (int y = 0; y <= H; y++) {
for (int x = 0; x <= W; x++) {
res = larger(res, dp[y][x]);
}
}
cout << res << endl;
}
}
|
#define _USE_MATH_DEFINES
#define INF 100000000
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
static const double EPS = 1e-8;
int tx[] = {-1, 0, 1, 0};
int ty[] = {0, 1, 0, -1};
char stage[101][101];
int W, H;
string larger(const string &str1, const string &str2) {
if (str1.size() > str2.size())
return str1;
if (str1.size() < str2.size())
return str2;
for (int i = 0; i < str1.size(); i++) {
if (str1[i] > str2[i])
return str1;
if (str1[i] < str2[i])
return str2;
}
return str1;
}
void dfs(int x, int y, string buf, string &res) {
buf.push_back(stage[y][x]);
if (y + 1 < H && isdigit(stage[y + 1][x])) {
dfs(x, y + 1, buf, res);
}
if (x + 1 < W && isdigit(stage[y][x + 1])) {
dfs(x + 1, y, buf, res);
}
res = larger(res, buf);
}
int main() {
while (~scanf("%d %d", &W, &H)) {
if (W == H && H == 0)
break;
for (int y = 0; y < H; y++) {
char buf[128];
scanf("%s", buf);
for (int x = 0; x < W; x++) {
stage[y][x] = buf[x];
}
}
string dp[101][101];
// for(int x=0;x<W;x++)
// if(isdigit(stage[0][x]) && stage[0][x] != '0'){
// dp[1][x+1] = dp[0][x] + stage[0][x];
// printf("dp[%d][%d]=%s\n",0,x+1,dp[0][x+1].c_str());
//}
// for(int y=0;x<W;y++)
// if(isdigit(stage[0][x]) && stage[0][x] != '0'){
// dp[1][x+1] = dp[0][x] + stage[0][x];
// printf("dp[%d][%d]=%s\n",0,x+1,dp[0][x+1].c_str());
//}
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
string left = "";
if (isdigit(stage[y][x])) {
if (!(dp[y + 1][x].size() == 0 && stage[y][x] == '0')) {
left = dp[y + 1][x] + stage[y][x];
}
}
string up = "";
if (isdigit(stage[y][x])) {
if (!(dp[y][x + 1].size() == 0 && stage[y][x] == '0')) {
up = dp[y][x + 1] + stage[y][x];
}
}
dp[y + 1][x + 1] = larger(left, up);
}
}
string res = "0";
for (int y = 0; y <= H; y++) {
for (int x = 0; x <= W; x++) {
res = larger(res, dp[y][x]);
}
}
cout << res << endl;
}
}
|
[["+", 63, 118, 28, 69, 341, 342, 0, 16, 17, 72], ["+", 63, 118, 28, 69, 341, 342, 0, 16, 12, 13]]
| 1
| 698
|
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int W, H;
string s[71][71];
char in[71][71];
bool comp2(string s, string r) {
if (s.size() != r.size())
return s.size() > r.size();
return s > r;
}
bool comp(string s, string r) {
while (s[0] == '0' && isdigit(s[1]))
s = s.substr(1);
while (r[0] == '0' && isdigit(r[1]))
r = r.substr(1);
return comp2(s, r);
}
main() {
int i, j, k;
while (cin >> W >> H, W) {
for (i = 0; i < H; i++)
cin >> in[i];
string m = "0";
for (int h = H - 1; h >= 0; h--)
for (int w = W - 1; w >= 0; w--)
s[h][w] = "";
for (int h = H - 1; h >= 0; h--)
for (int w = W - 1; w >= 0; w--) {
if (!isdigit(in[h][w]))
continue;
if (comp(in[h][w] + s[h + 1][w], m))
m = (in[h][w] + s[h + 1][w]);
if (comp(in[h][w] + s[h][w + 1], m))
m = (in[h][w] + s[h][w + 1]);
s[h][w] = (comp2(in[h][w] + s[h + 1][w], in[h][w] + s[h][w + 1]))
? (in[h][w] + s[h + 1][w])
: (in[h][w] + s[h][w + 1]);
}
while (m[0] == '0' && isdigit(m[1]))
m = m.substr(1);
cout << m << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int W, H;
string s[71][71];
char in[71][71];
bool comp2(string s, string r) {
if (s.size() != r.size())
return s.size() > r.size();
return s > r;
}
bool comp(string s, string r) {
while (s[0] == '0' && isdigit(s[1]))
s = s.substr(1);
while (r[0] == '0' && isdigit(r[1]))
r = r.substr(1);
return comp2(s, r);
}
main() {
int i, j, k;
while (cin >> W >> H, W) {
for (i = 0; i < H; i++)
cin >> in[i];
string m = "0";
for (int h = H; h >= 0; h--)
for (int w = W; w >= 0; w--)
s[h][w] = "";
for (int h = H - 1; h >= 0; h--)
for (int w = W - 1; w >= 0; w--) {
if (!isdigit(in[h][w]))
continue;
if (comp(in[h][w] + s[h + 1][w], m))
m = (in[h][w] + s[h + 1][w]);
if (comp(in[h][w] + s[h][w + 1], m))
m = (in[h][w] + s[h][w + 1]);
s[h][w] = (comp2(in[h][w] + s[h + 1][w], in[h][w] + s[h][w + 1]))
? (in[h][w] + s[h + 1][w])
: (in[h][w] + s[h][w + 1]);
}
while (m[0] == '0' && isdigit(m[1]))
m = m.substr(1);
cout << m << endl;
}
return 0;
}
|
[["-", 0, 7, 10, 43, 49, 50, 51, 16, 17, 33], ["-", 0, 7, 10, 43, 49, 50, 51, 16, 12, 13], ["-", 8, 7, 10, 43, 49, 50, 51, 16, 17, 33], ["-", 8, 7, 10, 43, 49, 50, 51, 16, 12, 13]]
| 1
| 515
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int dx[] = {1, 0};
const int dy[] = {0, 1};
vector<string> board(80);
string memo[80][80];
int W, H;
inline bool isDigit(int x, int y) {
if ('0' <= board[y][x] && board[y][x] <= '9')
return true;
else
return false;
}
string Rec(int x, int y) {
if (memo[y][x].size() != 0)
return memo[y][x];
string s, tmp;
for (int d = 0; d < 2; ++d) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx < 0 || nx >= W || ny < 0 || ny >= H || !isDigit(nx, ny))
continue;
tmp = Rec(nx, ny);
if (tmp.size() > s.size() || (tmp.size() == s.size() && tmp > s))
s = tmp;
}
return memo[y][x] = board[y][x] + s;
}
int main() {
while (cin >> W >> H, W || H) {
// Input
for (int i = 0; i < H; ++i)
cin >> board[i];
// Init
for (int i = 0; i < H; ++i)
for (int j = 0; j < W; ++j)
memo[i][j] = "";
// Solve
string tmp, ans;
for (int i = 0; i < H; ++i)
for (int j = 0; j < W; ++j)
if (isDigit(j, i) && memo[j][i].size() == 0) {
tmp = Rec(j, i);
int index = 0;
while (tmp[index] == '0')
index++;
tmp = tmp.substr(index);
if (tmp.size() > ans.size() ||
(tmp.size() == ans.size() && tmp > ans))
ans = tmp;
}
// Output
cout << ans << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int dx[] = {1, 0};
const int dy[] = {0, 1};
vector<string> board(80);
string memo[80][80];
int W, H;
inline bool isDigit(int x, int y) {
if ('0' <= board[y][x] && board[y][x] <= '9')
return true;
else
return false;
}
string Rec(int x, int y) {
if (memo[y][x].size() != 0)
return memo[y][x];
string s, tmp;
for (int d = 0; d < 2; ++d) {
int nx = x + dx[d];
int ny = y + dy[d];
if (nx < 0 || nx >= W || ny < 0 || ny >= H || !isDigit(nx, ny))
continue;
tmp = Rec(nx, ny);
if (tmp.size() > s.size() || (tmp.size() == s.size() && tmp > s))
s = tmp;
}
return memo[y][x] = board[y][x] + s;
}
int main() {
while (cin >> W >> H, W || H) {
// Input
for (int i = 0; i < H; ++i)
cin >> board[i];
// Init
for (int i = 0; i < H; ++i)
for (int j = 0; j < W; ++j)
memo[i][j] = "";
// Solve
string tmp, ans;
for (int i = 0; i < H; ++i)
for (int j = 0; j < W; ++j)
if (isDigit(j, i) && memo[i][j].size() == 0) {
tmp = Rec(j, i);
int index = 0;
while (tmp[index] == '0')
index++;
tmp = tmp.substr(index);
if (tmp.size() > ans.size() ||
(tmp.size() == ans.size() && tmp > ans))
ans = tmp;
}
// Output
cout << ans << endl;
}
return 0;
}
|
[["-", 63, 118, 28, 69, 28, 69, 341, 342, 0, 22], ["+", 63, 118, 28, 69, 28, 69, 341, 342, 0, 22], ["-", 31, 2, 63, 118, 28, 69, 341, 342, 0, 22], ["+", 31, 2, 63, 118, 28, 69, 341, 342, 0, 22]]
| 1
| 495
|
$x=0
$y=0
$bord = []
loop{
line = gets
if line==nil then
break
end
sp = line.split(nil)
if sp.count() != 2 then
break
else
$x = sp[0].to_i()
$y = sp[1].to_i()
if $x == 0 and $y == 0 then
break
end
$bord = Array.new($y).map{Array.new($x,0)}
max = 0
for i in 0..$y-1
line = gets
left = 0
for j in 0..$x-1
tmp = -1
if line[j] =~ /\d/ then
tmp = line[j].to_i()
if i > 0 then
up = $bord[i-1][j]
else
up = 0
end
if up > left then
tmp += up * 10
else
tmp = left * 10 + tmp
end
if tmp > max then
max = tmp
end
end
$bord[i][j] = tmp
left = tmp
end
end
print max
print("\n")
end
}
|
$x=0
$y=0
$bord = []
loop{
line = gets
if line==nil then
break
end
sp = line.split(nil)
if sp.count() != 2 then
break
else
$x = sp[0].to_i()
$y = sp[1].to_i()
if $x == 0 and $y == 0 then
break
end
$bord = Array.new($y).map{Array.new($x,0)}
max = 0
for i in 0..$y-1
line = gets
left = 0
for j in 0..$x-1
tmp = 0
if line[j] =~ /\d/ then
tmp = line[j].to_i()
if i > 0 then
up = $bord[i-1][j]
else
up = 0
end
if up > left then
tmp += up * 10
else
tmp += left * 10
end
if tmp > max then
max = tmp
end
end
$bord[i][j] = tmp
left = tmp
end
end
print max
print("\n")
end
}
|
[["-", 0, 88, 8, 170, 0, 662, 12, 748, 17, 33], ["-", 0, 88, 8, 170, 0, 662, 12, 748, 439, 612], ["+", 8, 170, 0, 88, 8, 170, 0, 662, 12, 612], ["-", 64, 749, 0, 121, 75, 95, 0, 662, 0, 32], ["+", 64, 749, 0, 121, 75, 95, 0, 755, 17, 107], ["-", 0, 121, 75, 95, 0, 662, 12, 738, 17, 72], ["-", 0, 121, 75, 95, 0, 662, 12, 738, 12, 22]]
| 4
| 215
|
while True:
W, H = list(map(int,input().split(" ")))
if W == H == 0:
break
field = [[c for c in input()] for _ in range(H)]
secret = [0, ""]
for j in range(H):
for i in range(W):
if not field[j][i].isdigit():
continue
pre = left = top = ""
if i > 0 and field[j][i-1].isdigit() and field[j][i-1] != "0":
left = field[j][i-1]
if j > 0 and field[j-1][i].isdigit() and field[j-1][i] != "0":
top = field[j-1][i]
if len(left) == len(top):
pre = left if left > top else top
else :
pre = left if len(left) > len(top) else top
field[j][i] = pre + field[j][i]
length = len(field[j][i])
if length > secret[0]:
secret[1] = field[j][i]
secret[0] = length
elif length == secret[0] and field[j][i] < secret[1]:
secret[1] = field[j][i]
print(secret[1])
|
while True:
W, H = list(map(int,input().split(" ")))
if W == H == 0:
break
field = [[c for c in input()] for _ in range(H)]
secret = [0, ""]
for j in range(H):
for i in range(W):
if not field[j][i].isdigit():
continue
pre = left = top = ""
if i > 0 and field[j][i-1].isdigit() and field[j][i-1] != "0":
left = field[j][i-1]
if j > 0 and field[j-1][i].isdigit() and field[j-1][i] != "0":
top = field[j-1][i]
if len(left) == len(top):
pre = left if left > top else top
else :
pre = left if len(left) > len(top) else top
field[j][i] = pre + field[j][i]
length = len(field[j][i])
if length > secret[0]:
secret[1] = field[j][i]
secret[0] = length
elif length == secret[0] and field[j][i] > secret[1]:
secret[1] = field[j][i]
print(secret[1])
|
[["-", 0, 57, 75, 665, 15, 679, 12, 666, 667, 18], ["+", 0, 57, 75, 665, 15, 679, 12, 666, 667, 47]]
| 5
| 320
|
# -*- coding: utf-8 -*-
def recursion(y, x, island, dp):
if island[y][x].isdigit() and int(island[y][x]) in range(10):
if y == 0 and x == 0:
dp[y][x] = int(island[y][x])
elif y == 0:
if dp[y][x-1] != -1:
dp[y][x] = dp[y][x-1] * 10 + int(island[y][x])
else:
dp[y][x] = int(island[y][x])
elif x == 0:
if dp[y-1][x] != -1:
dp[y][x] = dp[y-1][x] * 10 + int(island[y][x])
else:
dp[y][x] = int(island[y][x])
else:
if dp[y-1][x] == -1 and dp[y][x-1] == -1:
dp[y][x] = int(island[y][x])
elif dp[y-1][x] == -1:
dp[y][x] = dp[y][x-1] * 10 + int(island[y][x])
elif dp[y][x-1] == -1:
dp[y][x] = dp[y-1][x] * 10 + int(island[y][x])
else:
dp[y][x] = max(dp[y][x-1] * 10 + int(island[y][x]), dp[y-1][x] * 10 + int(island[y][x]))
else:
dp[y][x] = -1
def solve(w, h, island, dp):
for i in range(h):
for j in range(w):
recursion(i, j, island, dp)
print(dp)
print(max(list(map(max, dp))))
def func():
'''
'''
while True:
global count
w, h = list(map(int, input().split()))
if w == 0 and h == 0:
break
island = []
count = 0
for i in range(h):
row = []
line = input()
for j in line:
row.append(j)
island.append(row)
dp = []
for i in range(h):
dp_row = []
for j in range(w):
dp_row.append(0)
dp.append(dp_row)
solve(w, h, island, dp)
if __name__ == '__main__':
func()
|
# -*- coding: utf-8 -*-
def recursion(y, x, island, dp):
if island[y][x].isdigit() and int(island[y][x]) in range(10):
if y == 0 and x == 0:
dp[y][x] = int(island[y][x])
elif y == 0:
if dp[y][x-1] != -1:
dp[y][x] = dp[y][x-1] * 10 + int(island[y][x])
else:
dp[y][x] = int(island[y][x])
elif x == 0:
if dp[y-1][x] != -1:
dp[y][x] = dp[y-1][x] * 10 + int(island[y][x])
else:
dp[y][x] = int(island[y][x])
else:
if dp[y-1][x] == -1 and dp[y][x-1] == -1:
dp[y][x] = int(island[y][x])
elif dp[y-1][x] == -1:
dp[y][x] = dp[y][x-1] * 10 + int(island[y][x])
elif dp[y][x-1] == -1:
dp[y][x] = dp[y-1][x] * 10 + int(island[y][x])
else:
dp[y][x] = max(dp[y][x-1] * 10 + int(island[y][x]), dp[y-1][x] * 10 + int(island[y][x]))
else:
dp[y][x] = -1
def solve(w, h, island, dp):
for i in range(h):
for j in range(w):
recursion(i, j, island, dp)
#print dp
print(max(list(map(max, dp))))
def func():
'''
'''
while True:
global count
w, h = list(map(int, input().split()))
if w == 0 and h == 0:
break
island = []
count = 0
for i in range(h):
row = []
line = input()
for j in line:
row.append(j)
island.append(row)
dp = []
for i in range(h):
dp_row = []
for j in range(w):
dp_row.append(0)
dp.append(dp_row)
solve(w, h, island, dp)
if __name__ == '__main__':
func()
|
[["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25], ["-", 0, 14, 8, 196, 0, 1, 0, 652, 63, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24]]
| 5
| 608
|
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) { new Main().new P1127().run(); }
class P1127 {
void run() {
Scanner sc = new Scanner(System.in);
for (;;) {
int n = sc.nextInt();
if (n == 0)
break;
Pos[] p = new Pos[n];
Queue<Node> q = new PriorityQueue<Node>();
boolean[] visited = new boolean[n];
int nvis = 0;
double ans = 0;
for (int i = 0; i < n; i++) {
double x, y, z, r;
x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();
r = sc.nextDouble();
p[i] = new Pos(x, y, z, r);
visited[i] = false;
}
// Prim
Node t = new Node(0, 0);
q.offer(t);
while (nvis < n) {
t = q.poll();
if (visited[t.node])
continue;
visited[t.node] = true;
nvis++;
ans += t.d;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
q.offer(new Node((p[t.node]).dist(p[i]), i));
}
}
}
System.out.printf("%.3f", ans);
}
}
}
class Pos {
double x, y, z, r;
Pos(double x_, double y_, double z_, double r_) {
this.x = x_;
this.y = y_;
this.z = z_;
this.r = r_;
}
double dist(Pos o) {
double ret = Math.sqrt((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y) +
(z - o.z) * (z - o.z)) -
(r + o.r);
return ret <= 0 ? 0 : ret;
}
}
class Node implements Comparable<Node> {
double d;
int node;
public int compareTo(Node o) {
if (d < o.d)
return -1;
if (d == o.d)
return 0;
else
return 1;
}
Node(double d_, int node_) {
this.d = d_;
this.node = node_;
}
}
}
|
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) { new Main().new P1127().run(); }
class P1127 {
void run() {
Scanner sc = new Scanner(System.in);
for (;;) {
int n = sc.nextInt();
if (n == 0)
break;
Pos[] p = new Pos[n];
Queue<Node> q = new PriorityQueue<Node>();
boolean[] visited = new boolean[n];
int nvis = 0;
double ans = 0;
for (int i = 0; i < n; i++) {
double x, y, z, r;
x = sc.nextDouble();
y = sc.nextDouble();
z = sc.nextDouble();
r = sc.nextDouble();
p[i] = new Pos(x, y, z, r);
visited[i] = false;
}
// Prim
Node t = new Node(0, 0);
q.offer(t);
while (nvis < n) {
t = q.poll();
if (visited[t.node])
continue;
visited[t.node] = true;
nvis++;
ans += t.d;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
q.offer(new Node((p[t.node]).dist(p[i]), i));
}
}
}
System.out.printf("%.3f\n", ans);
}
}
}
class Pos {
double x, y, z, r;
Pos(double x_, double y_, double z_, double r_) {
this.x = x_;
this.y = y_;
this.z = z_;
this.r = r_;
}
double dist(Pos o) {
double ret = Math.sqrt((x - o.x) * (x - o.x) + (y - o.y) * (y - o.y) +
(z - o.z) * (z - o.z)) -
(r + o.r);
return ret <= 0 ? 0 : ret;
}
}
class Node implements Comparable<Node> {
double d;
int node;
public int compareTo(Node o) {
if (d < o.d)
return -1;
if (d == o.d)
return 0;
else
return 1;
}
Node(double d_, int node_) {
this.d = d_;
this.node = node_;
}
}
}
|
[["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 44]]
| 3
| 554
|
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
Scanner sc = new Scanner(System.in);
public void run() {
while (sc.hasNext()) {
int n = sc.nextInt();
if (n == 0)
break;
else
calc(n);
}
}
double xs[];
double ys[];
double zs[];
double rs[];
double distance[];
int count;
public void calc(int n) {
xs = new double[n];
ys = new double[n];
zs = new double[n];
rs = new double[n];
for (int i = 0; i < n; i++) {
xs[i] = sc.nextDouble();
ys[i] = sc.nextDouble();
zs[i] = sc.nextDouble();
rs[i] = sc.nextDouble();
}
distance = new double[n];
Arrays.fill(distance, Double.MAX_VALUE);
double ans = 0;
distance[0] = -1;
count = 1;
update(0, n);
while (count < n) {
int small = -1;
for (int k = 0; k < n; k++) {
if (distance[k] != -1 &&
(small == -1 || distance[k] < distance[small])) {
small = k;
}
}
ans += distance[small];
distance[small] = -1;
count++;
update(small, n);
}
System.out.printf("%.4f\n", ans);
}
public void update(int a, int n) {
for (int i = 0; i < n; i++) {
if (distance[i] != -1) {
double d = dist(a, i);
if (d == 0) {
distance[i] = -1;
count++;
update(i, n);
} else if (d < distance[i])
distance[i] = d;
}
}
}
public double dist(int a, int b) {
double d = Math.sqrt((xs[a] - xs[b]) * (xs[a] - xs[b]) +
(ys[a] - ys[b]) * (ys[a] - ys[b]) +
(zs[a] - zs[b]) * (zs[a] - zs[b]));
d = d - rs[a] - rs[b];
if (d < 0)
d = 0;
return d;
}
public static void main(String[] args) { new Main().run(); }
}
|
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
Scanner sc = new Scanner(System.in);
public void run() {
while (sc.hasNext()) {
int n = sc.nextInt();
if (n == 0)
break;
else
calc(n);
}
}
double xs[];
double ys[];
double zs[];
double rs[];
double distance[];
int count;
public void calc(int n) {
xs = new double[n];
ys = new double[n];
zs = new double[n];
rs = new double[n];
for (int i = 0; i < n; i++) {
xs[i] = sc.nextDouble();
ys[i] = sc.nextDouble();
zs[i] = sc.nextDouble();
rs[i] = sc.nextDouble();
}
distance = new double[n];
Arrays.fill(distance, Double.MAX_VALUE);
double ans = 0;
distance[0] = -1;
count = 1;
update(0, n);
while (count < n) {
int small = -1;
for (int k = 0; k < n; k++) {
if (distance[k] != -1 &&
(small == -1 || distance[k] < distance[small])) {
small = k;
}
}
ans += distance[small];
distance[small] = -1;
count++;
update(small, n);
}
System.out.printf("%.3f\n", ans);
}
public void update(int a, int n) {
for (int i = 0; i < n; i++) {
if (distance[i] != -1) {
double d = dist(a, i);
if (d == 0) {
distance[i] = -1;
count++;
update(i, n);
} else if (d < distance[i])
distance[i] = d;
}
}
}
public double dist(int a, int b) {
double d = Math.sqrt((xs[a] - xs[b]) * (xs[a] - xs[b]) +
(ys[a] - ys[b]) * (ys[a] - ys[b]) +
(zs[a] - zs[b]) * (zs[a] - zs[b]));
d = d - rs[a] - rs[b];
if (d < 0)
d = 0;
return d;
}
public static void main(String[] args) { new Main().run(); }
}
|
[["-", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491], ["+", 0, 1, 0, 492, 3, 4, 0, 5, 0, 491]]
| 3
| 584
|
//
// main.c
// Numeral System
//
// Created by x15071xx on 2016/06/09.
// Copyright ?? 2016??´ AIT. All rights reserved.
//
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[]) {
int n, i, j, k;
scanf("%d", &n);
for (i = 0; i < n; i++) {
char mcxi1[9] = {0}, mcxi2[9] = {0}, add[9] = {0};
int sum = 0, scalar = 0, x = 0;
unsigned int count;
scanf("%s %s", mcxi1, mcxi2);
//?????????????????´??°????±???????
for (j = 0; j < strlen(mcxi1); j++) {
if (mcxi1[j] >= 97 && mcxi1[j] <= 122) {
if (mcxi1[j - 1] >= 48 && mcxi1[j - 1] <= 57) {
scalar = mcxi1[j - 1] - 48;
} else {
scalar = 1;
}
if (mcxi1[j] == 'm') {
sum += scalar * 1000;
}
if (mcxi1[j] == 'c') {
sum += scalar * 100;
}
if (mcxi1[j] == 'x') {
sum += scalar * 10;
}
if (mcxi1[j] == 'i') {
sum += scalar * 1;
}
}
}
for (j = 0; j < strlen(mcxi2); j++) {
if (mcxi2[j] >= 97 && mcxi2[j] <= 122) {
if (mcxi2[j - 1] >= 48 && mcxi2[j - 1] <= 57) {
scalar = mcxi2[j - 1] - 48;
} else {
scalar = 1;
}
if (mcxi2[j] == 'm') {
sum += scalar * 1000;
}
if (mcxi2[j] == 'c') {
sum += scalar * 100;
}
if (mcxi2[j] == 'x') {
sum += scalar * 10;
}
if (mcxi2[j] == 'i') {
sum += scalar * 1;
}
}
}
//??´??°????????????????????????
if (sum / 1000 >= 1) {
x = sum / 1000;
sum -= x * 1000;
if (x == 1) {
add[0] = 'm';
} else if (x > 1) {
add[0] = x + 48;
add[1] = 'm';
}
}
if (sum / 100 >= 1) {
x = sum / 100;
sum -= x * 100;
count = strlen(add);
if (x == 1) {
add[count] = 'c';
} else if (x > 1) {
add[count] = x + 48;
add[count + 1] = 'c';
}
}
if (sum / 10 >= 1) {
x = sum / 10;
sum -= x * 10;
count = strlen(add);
if (x == 1) {
add[count] = 'x';
} else if (x > 1) {
add[count] = x + 48;
add[count + 1] = 'x';
}
}
if (sum >= 1) {
count = strlen(add);
if (sum == 1) {
add[count] = 'i';
} else if (sum > 1) {
add[count] = x + 48;
add[count + 1] = 'i';
}
}
//??°???
printf("%s\n", add);
}
return 0;
}
|
//
// main.c
// Numeral System
//
// Created by x15071xx on 2016/06/09.
// Copyright ?? 2016??´ AIT. All rights reserved.
//
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[]) {
int n, i, j, k;
scanf("%d", &n);
for (i = 0; i < n; i++) {
char mcxi1[9] = {0}, mcxi2[9] = {0}, add[9] = {0};
int sum = 0, scalar = 0, x = 0;
unsigned int count;
scanf("%s %s", mcxi1, mcxi2);
//?????????????????´??°????±???????
for (j = 0; j < strlen(mcxi1); j++) {
if (mcxi1[j] >= 97 && mcxi1[j] <= 122) {
if (mcxi1[j - 1] >= 48 && mcxi1[j - 1] <= 57) {
scalar = mcxi1[j - 1] - 48;
} else {
scalar = 1;
}
if (mcxi1[j] == 'm') {
sum += scalar * 1000;
}
if (mcxi1[j] == 'c') {
sum += scalar * 100;
}
if (mcxi1[j] == 'x') {
sum += scalar * 10;
}
if (mcxi1[j] == 'i') {
sum += scalar * 1;
}
}
}
for (j = 0; j < strlen(mcxi2); j++) {
if (mcxi2[j] >= 97 && mcxi2[j] <= 122) {
if (mcxi2[j - 1] >= 48 && mcxi2[j - 1] <= 57) {
scalar = mcxi2[j - 1] - 48;
} else {
scalar = 1;
}
if (mcxi2[j] == 'm') {
sum += scalar * 1000;
}
if (mcxi2[j] == 'c') {
sum += scalar * 100;
}
if (mcxi2[j] == 'x') {
sum += scalar * 10;
}
if (mcxi2[j] == 'i') {
sum += scalar * 1;
}
}
}
//??´??°????????????????????????
if (sum / 1000 >= 1) {
x = sum / 1000;
sum -= x * 1000;
if (x == 1) {
add[0] = 'm';
} else if (x > 1) {
add[0] = x + 48;
add[1] = 'm';
}
}
if (sum / 100 >= 1) {
x = sum / 100;
sum -= x * 100;
count = strlen(add);
if (x == 1) {
add[count] = 'c';
} else if (x > 1) {
add[count] = x + 48;
add[count + 1] = 'c';
}
}
if (sum / 10 >= 1) {
x = sum / 10;
sum -= x * 10;
count = strlen(add);
if (x == 1) {
add[count] = 'x';
} else if (x > 1) {
add[count] = x + 48;
add[count + 1] = 'x';
}
}
if (sum >= 1) {
count = strlen(add);
if (sum == 1) {
add[count] = 'i';
} else if (sum > 1) {
add[count] = sum + 48;
add[count + 1] = 'i';
}
}
//??°???
printf("%s\n", add);
}
return 0;
}
|
[["-", 64, 9, 0, 1, 0, 11, 12, 16, 31, 22], ["+", 64, 9, 0, 1, 0, 11, 12, 16, 31, 22]]
| 0
| 713
|
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Integer(char *s) {
int i, n, answer = 0;
char temp;
for (i = 0; i < strlen(s) / 2; i++) {
temp = s[i];
s[i] = s[(strlen(s) - 1) - i];
s[(strlen(s) - 1) - i] = temp;
}
i = 0;
while (i <= strlen(s) - 1) {
if (isdigit(s[i + 1]))
n = atoi(&s[i + 1]);
else
n = 1;
switch (s[i]) {
case 'i':
answer += n;
break;
case 'x':
answer += 10 * n;
break;
case 'c':
answer += 100 * n;
break;
case 'm':
answer += 1000 * n;
break;
}
if (isdigit(s[i + 1]))
i += 2;
else
i++;
}
return answer;
}
int main() {
int n;
char s1[10], s2[10];
int sum, digit;
char answer[8];
int i, j;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%s %s", s1, s2);
sum = Integer(s1) + Integer(s2);
for (j = 0; j < 8; j++)
answer[j] = '\0';
digit = 1000, j = 0;
while (sum > 0) {
if (sum / digit > 0) {
if (sum / digit >= 2) {
sprintf(&answer[j], "%d", sum / digit);
j++;
}
if (digit == 1000)
answer[j] = 'm';
else if (digit == 100)
answer[j] = 'c';
else if (digit == 10)
answer[j] = 'x';
else
answer[j] = 'i';
sum -= (sum / digit) * digit;
j++;
}
digit /= 10;
}
printf("%s\n", answer);
}
return 0;
}
|
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Integer(char *s) {
int i, n, answer = 0;
char temp;
for (i = 0; i < strlen(s) / 2; i++) {
temp = s[i];
s[i] = s[(strlen(s) - 1) - i];
s[(strlen(s) - 1) - i] = temp;
}
i = 0;
while (i <= strlen(s) - 1) {
if (isdigit(s[i + 1]))
n = atoi(&s[i + 1]);
else
n = 1;
switch (s[i]) {
case 'i':
answer += n;
break;
case 'x':
answer += 10 * n;
break;
case 'c':
answer += 100 * n;
break;
case 'm':
answer += 1000 * n;
break;
}
if (isdigit(s[i + 1]))
i += 2;
else
i++;
}
return answer;
}
int main() {
int n;
char s1[10], s2[10];
int sum, digit;
char answer[10];
int i, j;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%s %s", s1, s2);
sum = Integer(s1) + Integer(s2);
for (j = 0; j < 10; j++)
answer[j] = '\0';
digit = 1000, j = 0;
while (sum > 0) {
if (sum / digit > 0) {
if (sum / digit >= 2) {
sprintf(&answer[j], "%d", sum / digit);
j++;
}
if (digit == 1000)
answer[j] = 'm';
else if (digit == 100)
answer[j] = 'c';
else if (digit == 10)
answer[j] = 'x';
else
answer[j] = 'i';
sum -= (sum / digit) * digit;
j++;
}
digit /= 10;
}
printf("%s\n", answer);
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["+", 0, 14, 8, 9, 0, 43, 49, 80, 81, 13], ["-", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 7, 8, 9, 0, 7, 15, 16, 12, 13]]
| 0
| 469
|
#include <bits/stdc++.h>
using namespace std;
int z[26] = {0};
char ch[4] = {'i', 'x', 'c', 'm'};
string s1, s2;
int n;
string enc(int x);
int dec(string s);
int main() {
int a;
cin >> n;
z['m' - 'a'] = 1000;
z['c' - 'a'] = 100;
z['x' - 'a'] = 10;
z['i' - 'a'] = 1;
for (a = 0; a < n; ++a) {
cin >> s1 >> s2;
cout << enc(dec(s1) + dec(s2)) << endl;
}
return 0;
}
string enc(int x) {
int now, xx = x;
string ans;
for (int i = 0; i < 4; ++i) {
now = xx % 10;
xx /= 10;
if (now == 0)
continue;
ans += ch[i];
if (now == 0)
continue;
ans += '0' + now;
}
reverse(ans.begin(), ans.end());
return ans;
}
int dec(string s) {
int ans = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] >= 'a' && s[i] <= 'z')
ans += z[s[i] - 'a'];
else
ans += (s[i] - '0') * z[s[++i] - 'a'];
}
return ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int z[26] = {0};
char ch[4] = {'i', 'x', 'c', 'm'};
string s1, s2;
int n;
string enc(int x);
int dec(string s);
int main() {
int a;
cin >> n;
z['m' - 'a'] = 1000;
z['c' - 'a'] = 100;
z['x' - 'a'] = 10;
z['i' - 'a'] = 1;
for (a = 0; a < n; ++a) {
cin >> s1 >> s2;
cout << enc(dec(s1) + dec(s2)) << endl;
}
return 0;
}
string enc(int x) {
int now, xx = x;
string ans;
for (int i = 0; i < 4; ++i) {
now = xx % 10;
xx /= 10;
if (now == 0)
continue;
ans += ch[i];
if (now == 1)
continue;
ans += '0' + now;
}
reverse(ans.begin(), ans.end());
return ans;
}
int dec(string s) {
int ans = 0;
for (int i = 0; i < s.size(); ++i) {
if (s[i] >= 'a' && s[i] <= 'z')
ans += z[s[i] - 'a'];
else
ans += (s[i] - '0') * z[s[++i] - 'a'];
}
return ans;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
| 1
| 358
|
#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;
int MCXI2D(string str) {
int d = 1;
int sum = 0;
int p = 0;
while (p < str.size()) {
if (isdigit(str[p])) {
d = str[p] - '0';
} else {
if (str[p] == 'm') {
sum += d * 1000;
} else if (str[p] == 'c') {
sum += d * 100;
} else if (str[p] == 'x') {
sum += d * 10;
} else if (str[p] == 'i') {
sum += d * 1;
}
d = 1;
}
p++;
}
return sum;
}
string D2MCXI(int a, int b) {
int sum = a + b;
string ans = "";
int m = sum / 1000;
sum %= 1000;
int c = sum / 100;
sum %= 100;
int x = sum / 10;
sum %= 10;
int i = sum;
if (m != 0) {
if (m == 1) {
ans += "m";
} else {
ans += (m + '0');
ans += "m";
}
}
if (c != 0) {
if (c == 1) {
ans += "c";
} else {
ans += (c + '0');
ans += "c";
}
}
if (x != 0) {
if (x == 1) {
ans += "x";
} else {
ans += (x + '0');
ans += "x";
}
}
if (i != 0) {
if (i == 1) {
ans += "i";
} else {
ans += (c + '0');
ans += "i";
}
}
return ans;
}
int main() {
string str1, str2;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> str1 >> str2;
cout << D2MCXI(MCXI2D(str1), MCXI2D(str2)) << endl;
}
return 0;
}
|
#include <cctype>
#include <cstring>
#include <iostream>
using namespace std;
int MCXI2D(string str) {
int d = 1;
int sum = 0;
int p = 0;
while (p < str.size()) {
if (isdigit(str[p])) {
d = str[p] - '0';
} else {
if (str[p] == 'm') {
sum += d * 1000;
} else if (str[p] == 'c') {
sum += d * 100;
} else if (str[p] == 'x') {
sum += d * 10;
} else if (str[p] == 'i') {
sum += d * 1;
}
d = 1;
}
p++;
}
return sum;
}
string D2MCXI(int a, int b) {
int sum = a + b;
string ans = "";
int m = sum / 1000;
sum %= 1000;
int c = sum / 100;
sum %= 100;
int x = sum / 10;
sum %= 10;
int i = sum;
if (m != 0) {
if (m == 1) {
ans += "m";
} else {
ans += (m + '0');
ans += "m";
}
}
if (c != 0) {
if (c == 1) {
ans += "c";
} else {
ans += (c + '0');
ans += "c";
}
}
if (x != 0) {
if (x == 1) {
ans += "x";
} else {
ans += (x + '0');
ans += "x";
}
}
if (i != 0) {
if (i == 1) {
ans += "i";
} else {
ans += (i + '0');
ans += "i";
}
}
return ans;
}
int main() {
string str1, str2;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> str1 >> str2;
cout << D2MCXI(MCXI2D(str1), MCXI2D(str2)) << endl;
}
return 0;
}
|
[["-", 0, 1, 0, 11, 12, 23, 0, 16, 31, 22], ["+", 0, 1, 0, 11, 12, 23, 0, 16, 31, 22]]
| 1
| 449
|
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int n;
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
string str, str2;
cin >> str >> str2;
int c = 0;
int f = 1;
for (int i = 0; i < str.size(); i++) {
if (str[i] >= '2' && str[i] <= '9') {
f = str[i] - '0';
}
if (str[i] == 'm') {
c += 1000 * f;
f = 1;
}
if (str[i] == 'c') {
c += 100 * f;
f = 1;
}
if (str[i] == 'x') {
c += 10 * f;
f = 1;
}
if (str[i] == 'i') {
c += f;
f = 1;
}
}
f = 1;
for (int i = 0; i < str2.size(); i++) {
if (str2[i] >= '2' && str2[i] <= '9') {
f = str2[i] - '0';
}
if (str2[i] == 'm') {
c += 1000 * f;
f = 1;
}
if (str2[i] == 'c') {
c += 100 * f;
f = 1;
}
if (str2[i] == 'x') {
c += 10 * f;
f = 1;
}
if (str2[i] == 'i') {
c += f;
f = 1;
}
}
int k = 1000;
while (k > 0) {
int d = c / k;
if (d >= 1) {
if (d > 2)
printf("%d", d);
if (k == 1000)
printf("m");
if (k == 100)
printf("c");
if (k == 10)
printf("x");
if (k == 1)
printf("i");
}
c %= k;
k /= 10;
}
printf("\n");
}
return 0;
}
|
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int n;
int main(void) {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
string str, str2;
cin >> str >> str2;
int c = 0;
int f = 1;
for (int i = 0; i < str.size(); i++) {
if (str[i] >= '2' && str[i] <= '9') {
f = str[i] - '0';
}
if (str[i] == 'm') {
c += 1000 * f;
f = 1;
}
if (str[i] == 'c') {
c += 100 * f;
f = 1;
}
if (str[i] == 'x') {
c += 10 * f;
f = 1;
}
if (str[i] == 'i') {
c += f;
f = 1;
}
}
f = 1;
for (int i = 0; i < str2.size(); i++) {
if (str2[i] >= '2' && str2[i] <= '9') {
f = str2[i] - '0';
}
if (str2[i] == 'm') {
c += 1000 * f;
f = 1;
}
if (str2[i] == 'c') {
c += 100 * f;
f = 1;
}
if (str2[i] == 'x') {
c += 10 * f;
f = 1;
}
if (str2[i] == 'i') {
c += f;
f = 1;
}
}
int k = 1000;
while (k > 0) {
int d = c / k;
if (d >= 1) {
if (d >= 2)
printf("%d", d);
if (k == 1000)
printf("m");
if (k == 100)
printf("c");
if (k == 10)
printf("x");
if (k == 1)
printf("i");
}
c %= k;
k /= 10;
}
printf("\n");
}
return 0;
}
|
[["-", 64, 9, 0, 57, 15, 339, 51, 16, 17, 47], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 17, 20]]
| 1
| 472
|
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int main(void) {
// FILE* fp_in = freopen("data.txt", "r", stdin);
int N;
cin >> N;
for (int cnt = 0; cnt < N; cnt++) {
string s[2];
int num[2][4];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 4; j++)
num[i][j] = 0;
stringstream ans;
cin >> s[0] >> s[1];
//数値解析
for (int i = 0; i < 2; i++) {
//千の位
int tmp = s[i].find("m", 0);
if (tmp == string::npos)
num[i][3] = 0;
else if (tmp == 0)
num[i][3] = 1;
else
num[i][3] = s[i].at(0) - '0';
//百の位
tmp = s[i].find("c", 0);
if (tmp == string::npos)
num[i][2] = 0;
else if (tmp == 0 || s[i].at(tmp - 1) > '9')
num[i][2] = 1;
else
num[i][2] = s[i].at(tmp - 1) - '0';
//十の位
tmp = s[i].find("x", 0);
if (tmp == string::npos)
num[i][1] = 0;
else if (tmp == 0 || s[i].at(tmp - 1) > '9')
num[i][1] = 1;
else
num[i][1] = s[i].at(tmp - 1) - '0';
//一の位
tmp = s[i].find("i", 0);
if (tmp == string::npos)
num[i][0] = 0;
else if (tmp == 0 || s[i].at(tmp - 1) > '9')
num[i][0] = 1;
else
num[i][0] = s[i].at(tmp - 1) - '0';
}
int p[2];
for (int i = 0; i < 2; i++)
p[i] = num[i][3] * 1000 + num[i][2] * 100 + num[i][1] * 10 + num[i][0];
int ans_i = p[0] + p[1];
//答えの文字列の生成
if (ans_i / 1000 > 2) {
ans << ans_i / 1000;
ans << "m";
} else if (ans_i / 1000 == 1) {
ans << "m";
}
ans_i -= ans_i / 1000 * 1000;
//答えの文字列の生成
if (ans_i / 100 > 2) {
ans << ans_i / 100;
ans << "c";
} else if (ans_i / 100 == 1) {
ans << "c";
}
ans_i -= ans_i / 100 * 100;
//答えの文字列の生成
if (ans_i / 10 > 2) {
ans << ans_i / 10;
ans << "x";
} else if (ans_i / 10 == 1) {
ans << "x";
}
ans_i -= ans_i / 10 * 10;
//答えの文字列の生成
if (ans_i / 1 > 2) {
ans << ans_i / 1;
ans << "i";
} else if (ans_i / 1 == 1) {
ans << "i";
}
cout << ans.str() << endl;
}
// while(1){}
return 0;
}
|
#include <iostream>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int main(void) {
// FILE* fp_in = freopen("data.txt", "r", stdin);
int N;
cin >> N;
for (int cnt = 0; cnt < N; cnt++) {
string s[2];
int num[2][4];
for (int i = 0; i < 2; i++)
for (int j = 0; j < 4; j++)
num[i][j] = 0;
stringstream ans;
cin >> s[0] >> s[1];
//数値解析
for (int i = 0; i < 2; i++) {
//千の位
int tmp = s[i].find("m", 0);
if (tmp == string::npos)
num[i][3] = 0;
else if (tmp == 0)
num[i][3] = 1;
else
num[i][3] = s[i].at(0) - '0';
//百の位
tmp = s[i].find("c", 0);
if (tmp == string::npos)
num[i][2] = 0;
else if (tmp == 0 || s[i].at(tmp - 1) > '9')
num[i][2] = 1;
else
num[i][2] = s[i].at(tmp - 1) - '0';
//十の位
tmp = s[i].find("x", 0);
if (tmp == string::npos)
num[i][1] = 0;
else if (tmp == 0 || s[i].at(tmp - 1) > '9')
num[i][1] = 1;
else
num[i][1] = s[i].at(tmp - 1) - '0';
//一の位
tmp = s[i].find("i", 0);
if (tmp == string::npos)
num[i][0] = 0;
else if (tmp == 0 || s[i].at(tmp - 1) > '9')
num[i][0] = 1;
else
num[i][0] = s[i].at(tmp - 1) - '0';
}
int p[2];
for (int i = 0; i < 2; i++)
p[i] = num[i][3] * 1000 + num[i][2] * 100 + num[i][1] * 10 + num[i][0];
int ans_i = p[0] + p[1];
//答えの文字列の生成
if (ans_i / 1000 > 1) {
ans << ans_i / 1000;
ans << "m";
} else if (ans_i / 1000 == 1) {
ans << "m";
}
ans_i -= ans_i / 1000 * 1000;
//答えの文字列の生成
if (ans_i / 100 > 1) {
ans << ans_i / 100;
ans << "c";
} else if (ans_i / 100 == 1) {
ans << "c";
}
ans_i -= ans_i / 100 * 100;
//答えの文字列の生成
if (ans_i / 10 > 1) {
ans << ans_i / 10;
ans << "x";
} else if (ans_i / 10 == 1) {
ans << "x";
}
ans_i -= ans_i / 10 * 10;
//答えの文字列の生成
if (ans_i / 1 > 1) {
ans << ans_i / 1;
ans << "i";
} else if (ans_i / 1 == 1) {
ans << "i";
}
cout << ans.str() << endl;
}
// while(1){}
return 0;
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
| 1
| 763
|
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
string itos(int n) {
int a, b, c, d;
a = n / 1000;
n = n % 1000;
b = n / 100;
n = n % 100;
c = n / 10;
c = n % 10;
d = n;
string s = "";
if (a > 1)
s = s + to_string(a);
if (a > 0)
s = s + "m";
if (b > 1)
s = s + to_string(b);
if (b > 0)
s = s + "c";
if (c > 1)
s = s + to_string(c);
if (c > 0)
s = s + "x";
if (d > 1)
s = s + to_string(d);
if (d > 0)
s = s + "i";
return s;
}
int sstoi(string s) {
int l = s.length();
int ans = 0, k = 1;
for (int i = 0; i < l; i++) {
if ('1' < s[i] && s[i] <= '9') {
k = s[i] - '0';
} else if (s[i] == 'm') {
ans = ans + k * 1000;
k = 1;
} else if (s[i] == 'c') {
ans = ans + k * 100;
k = 1;
} else if (s[i] == 'x') {
ans = ans + k * 10;
k = 1;
} else if (s[i] == 'i') {
ans = ans + k;
k = 1;
}
}
return ans;
}
int main() {
string s, t;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s >> t;
int ans = sstoi(s) + sstoi(t);
cout << itos(ans) << endl;
}
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
string itos(int n) {
int a, b, c, d;
a = n / 1000;
n = n % 1000;
b = n / 100;
n = n % 100;
c = n / 10;
n = n % 10;
d = n;
string s = "";
if (a > 1)
s = s + to_string(a);
if (a > 0)
s = s + "m";
if (b > 1)
s = s + to_string(b);
if (b > 0)
s = s + "c";
if (c > 1)
s = s + to_string(c);
if (c > 0)
s = s + "x";
if (d > 1)
s = s + to_string(d);
if (d > 0)
s = s + "i";
return s;
}
int sstoi(string s) {
int l = s.length();
int ans = 0, k = 1;
for (int i = 0; i < l; i++) {
if ('1' < s[i] && s[i] <= '9') {
k = s[i] - '0';
} else if (s[i] == 'm') {
ans = ans + k * 1000;
k = 1;
} else if (s[i] == 'c') {
ans = ans + k * 100;
k = 1;
} else if (s[i] == 'x') {
ans = ans + k * 10;
k = 1;
} else if (s[i] == 'i') {
ans = ans + k;
k = 1;
}
}
return ans;
}
int main() {
string s, t;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> s >> t;
int ans = sstoi(s) + sstoi(t);
// cout<<sstoi(s)<<" "<<sstoi(t)<<endl;
cout << itos(ans) << endl;
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 1, 0, 11, 31, 22], ["+", 0, 14, 8, 9, 0, 1, 0, 11, 31, 22]]
| 1
| 441
|
// #define USE_CPP11
// #define USE_PRINTF
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#ifdef USE_CPP11
#include <functional>
#include <unordered_map>
#include <unordered_set>
#endif
#define PRINT(obj) cout << obj << endl
#define REP(i, n) for (int(i) = 0; (i) < (n); i++)
#define REV(s) (string((s).rbegin(), (s).rend()))
#define SPACE " "
typedef long long ll;
using namespace std;
namespace Scanner {
#ifdef USE_PRINTF
int nextInt() {
int d;
scanf("%d", &d);
return d;
}
float nextFloat() {
float f;
scanf("%f", &f);
return f;
}
double nextDouble() {
double f;
scanf("%lf", &f);
return f;
}
string nextString() {
string s;
scanf("%s", &s);
return s;
}
ll nextll() {
ll l;
scanf("%ll", &l);
return l;
}
#else
int nextInt() {
int d;
cin >> d;
return d;
}
float nextFloat() {
float f;
cin >> f;
return f;
}
double nextDouble() {
double f;
cin >> f;
return f;
}
string nextString() {
string s;
cin >> s;
return s;
}
ll nextll() {
ll l;
cin >> l;
return l;
}
#endif
string getLine() {
string input;
getline(cin, input);
return input;
}
}; // namespace Scanner
namespace myLibrary {
vector<string> split(const string &str, char delim) {
vector<string> ret;
size_t current = 0, found;
while ((found = str.find_first_of(delim, current)) != string::npos) {
ret.push_back(string(str, current, found - current));
current = found + 1;
}
ret.push_back(string(str, current, str.size() - current));
return ret;
}
#ifdef USE_CPP11
///
/// higher-order functional
/// This is like map in haskell, python, etc.
/// Sample usage:
/// vector<int> a = {1, 2, 3, 4, 5};
/// vector<int> b = myLibrary::_Map<int, int>(a, [](int n){
/// return n * n;
/// });
/// Result:
/// b = 1, 4, 9, 16, 25
///
template <typename T_IN, typename T_OUT, class Functor>
vector<T_OUT> _Map(vector<T_IN> src, Functor func) {
vector<T_OUT> ret;
for (int i = 0; i < src.size(); i++) {
ret.push_back(func(src[i]));
}
return ret;
}
#endif
}; // namespace myLibrary
/// BEGIN //////////////////////////////////////////////////////////
int mcxi2int(string mcxi) {
int ret = 0;
int t = 1;
REP(i, mcxi.size()) {
switch (mcxi[i]) {
case 'm': {
ret += t * 1000;
t = 1;
break;
}
case 'c': {
ret += t * 100;
t = 1;
break;
}
case 'x': {
ret += t * 10;
t = 1;
break;
}
case 'i': {
ret += t;
break;
}
default: {
t = mcxi[i] - '0';
}
}
}
return ret;
}
string int2mcxi(int m) {
string ret;
if (m >= 1000) {
if (m / 1000 != 1) {
ret += '0' + m / 1000;
}
ret += 'x';
m %= 1000;
}
if (m >= 100) {
if (m / 100 != 1) {
ret += '0' + m / 100;
}
ret += 'c';
m %= 100;
}
if (m >= 10) {
if (m / 10 != 1) {
ret += '0' + m / 10;
}
ret += 'x';
m %= 10;
}
if (m > 0) {
if (m != 1) {
ret += '0' + m;
}
ret += 'i';
}
return ret;
}
int main() {
int n = Scanner::nextInt();
while (n--) {
int m1 = mcxi2int(Scanner::nextString());
int m2 = mcxi2int(Scanner::nextString());
int m = m1 + m2;
PRINT(int2mcxi(m));
}
return 0;
}
/// END //////////////////////////////////////////////////////////
|
// #define USE_CPP11
// #define USE_PRINTF
#include <algorithm>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#ifdef USE_CPP11
#include <functional>
#include <unordered_map>
#include <unordered_set>
#endif
#define PRINT(obj) cout << obj << endl
#define REP(i, n) for (int(i) = 0; (i) < (n); i++)
#define REV(s) (string((s).rbegin(), (s).rend()))
#define SPACE " "
typedef long long ll;
using namespace std;
namespace Scanner {
#ifdef USE_PRINTF
int nextInt() {
int d;
scanf("%d", &d);
return d;
}
float nextFloat() {
float f;
scanf("%f", &f);
return f;
}
double nextDouble() {
double f;
scanf("%lf", &f);
return f;
}
string nextString() {
string s;
scanf("%s", &s);
return s;
}
ll nextll() {
ll l;
scanf("%ll", &l);
return l;
}
#else
int nextInt() {
int d;
cin >> d;
return d;
}
float nextFloat() {
float f;
cin >> f;
return f;
}
double nextDouble() {
double f;
cin >> f;
return f;
}
string nextString() {
string s;
cin >> s;
return s;
}
ll nextll() {
ll l;
cin >> l;
return l;
}
#endif
string getLine() {
string input;
getline(cin, input);
return input;
}
}; // namespace Scanner
namespace myLibrary {
vector<string> split(const string &str, char delim) {
vector<string> ret;
size_t current = 0, found;
while ((found = str.find_first_of(delim, current)) != string::npos) {
ret.push_back(string(str, current, found - current));
current = found + 1;
}
ret.push_back(string(str, current, str.size() - current));
return ret;
}
#ifdef USE_CPP11
///
/// higher-order functional
/// This is like map in haskell, python, etc.
/// Sample usage:
/// vector<int> a = {1, 2, 3, 4, 5};
/// vector<int> b = myLibrary::_Map<int, int>(a, [](int n){
/// return n * n;
/// });
/// Result:
/// b = 1, 4, 9, 16, 25
///
template <typename T_IN, typename T_OUT, class Functor>
vector<T_OUT> _Map(vector<T_IN> src, Functor func) {
vector<T_OUT> ret;
for (int i = 0; i < src.size(); i++) {
ret.push_back(func(src[i]));
}
return ret;
}
#endif
}; // namespace myLibrary
/// BEGIN //////////////////////////////////////////////////////////
int mcxi2int(string mcxi) {
int ret = 0;
int t = 1;
REP(i, mcxi.size()) {
switch (mcxi[i]) {
case 'm': {
ret += t * 1000;
t = 1;
break;
}
case 'c': {
ret += t * 100;
t = 1;
break;
}
case 'x': {
ret += t * 10;
t = 1;
break;
}
case 'i': {
ret += t;
break;
}
default: {
t = mcxi[i] - '0';
}
}
}
return ret;
}
string int2mcxi(int m) {
string ret;
if (m >= 1000) {
if (m / 1000 != 1) {
ret += '0' + m / 1000;
}
ret += 'm';
m %= 1000;
}
if (m >= 100) {
if (m / 100 != 1) {
ret += '0' + m / 100;
}
ret += 'c';
m %= 100;
}
if (m >= 10) {
if (m / 10 != 1) {
ret += '0' + m / 10;
}
ret += 'x';
m %= 10;
}
if (m > 0) {
if (m != 1) {
ret += '0' + m;
}
ret += 'i';
}
return ret;
}
int main() {
int n = Scanner::nextInt();
while (n--) {
int m1 = mcxi2int(Scanner::nextString());
int m2 = mcxi2int(Scanner::nextString());
int m = m1 + m2;
PRINT(int2mcxi(m));
}
return 0;
}
/// END //////////////////////////////////////////////////////////
|
[["-", 64, 9, 0, 1, 0, 11, 12, 103, 0, 125], ["+", 64, 9, 0, 1, 0, 11, 12, 103, 0, 125]]
| 1
| 842
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
int n;
char mcxi[] = {'m', 'c', 'x', 'i'};
string num;
cin >> n;
while (n--) {
vector<pair<char, int>> vp;
for (int i = 0; i < 4; ++i)
vp.push_back(make_pair(mcxi[i], 0));
for (int i = 0; i < 2; ++i) {
cin >> num;
for (int j = 0; j < num.size(); ++j) {
if (num[j] > 'a' && num[j] < 'z') {
for (int k = 0; k < 4; ++k) {
if (num[j] == vp[k].first) {
if (j - 1 >= 0 && num[j - 1] > '0' && num[j - 1] <= '9')
vp[k].second += num[j - 1] - '0';
else
vp[k].second++;
}
}
}
}
}
for (int i = 0; i < 4; ++i) {
if (vp[i].second > 9) {
vp[i - 1].second++;
vp[i].second %= 10;
}
}
for (int i = 0; i < 4; ++i) {
if (vp[i].second == 0)
continue;
else if (vp[i].second == 1)
cout << vp[i].first;
else
cout << vp[i].second << vp[i].first;
}
cout << endl;
}
return 0;
}
|
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
int n;
char mcxi[] = {'m', 'c', 'x', 'i'};
string num;
cin >> n;
while (n--) {
vector<pair<char, int>> vp;
for (int i = 0; i < 4; ++i)
vp.push_back(make_pair(mcxi[i], 0));
for (int i = 0; i < 2; ++i) {
cin >> num;
for (int j = 0; j < num.size(); ++j) {
if (num[j] > 'a' && num[j] < 'z') {
for (int k = 0; k < 4; ++k) {
if (num[j] == vp[k].first) {
if (j - 1 >= 0 && num[j - 1] > '0' && num[j - 1] <= '9')
vp[k].second += num[j - 1] - '0';
else
vp[k].second++;
}
}
}
}
}
for (int i = 3; i >= 0; --i) {
if (vp[i].second > 9) {
vp[i - 1].second++;
vp[i].second %= 10;
}
}
for (int i = 0; i < 4; ++i) {
if (vp[i].second == 0)
continue;
else if (vp[i].second == 1)
cout << vp[i].first;
else
cout << vp[i].second << vp[i].first;
}
cout << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["+", 8, 9, 0, 7, 10, 43, 49, 50, 51, 13], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 17, 18], ["-", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 17, 20], ["+", 0, 52, 8, 9, 0, 7, 15, 16, 12, 13], ["-", 0, 52, 8, 9, 0, 7, 26, 27, 17, 29], ["+", 0, 52, 8, 9, 0, 7, 26, 27, 17, 68]]
| 1
| 377
|
#include <iostream>
#include <string>
using namespace std;
int m, c, x, ii = 0;
void mcxiDisassembly(string &mcxi) {
int sum = 0;
int number = 0;
for (int i = 0; i <= mcxi.length(); i++) {
if (mcxi[i] != 'm' && mcxi[i] != 'c' && mcxi[i] != 'x' && mcxi[i] != 'i') {
number = (int)(mcxi[i] - '0');
} else {
switch (mcxi[i]) {
case 'm':
if (number == 0) {
m += 1;
} else {
m += number;
}
break;
case 'c':
if (number == 0) {
c += 1;
} else {
c += number;
}
break;
case 'x':
if (number == 0) {
x += 1;
} else {
x += number;
}
break;
case 'i':
if (number == 0) {
ii += 1;
} else {
ii += number;
}
break;
}
number = 0;
}
}
}
void show() {
if (m != 0) {
if (m == 0) {
cout << 'm';
} else {
cout << m << 'm';
}
}
if (c != 0) {
if (c == 1) {
cout << 'c';
} else {
cout << c << 'c';
}
}
if (x != 0) {
if (x == 1) {
cout << 'x';
} else {
cout << x << 'x';
}
}
if (ii != 0) {
if (ii == 1) {
cout << 'i';
} else {
cout << ii << 'i';
}
}
cout << endl;
}
int main() {
string mcxi1, mcxi2;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> mcxi1 >> mcxi2;
//????§£
mcxiDisassembly(mcxi1);
mcxiDisassembly(mcxi2);
if (ii >= 10) {
x += ii / 10;
ii -= (ii / 10) * 10;
}
if (x >= 10) {
c += x / 10;
x -= (x / 10) * 10;
}
if (c >= 10) {
m += c / 10;
c -= (c / 10) * 10;
}
//??¨???
show();
m = c = x = ii = 0;
}
return 0;
}
|
#include <iostream>
#include <string>
using namespace std;
int m, c, x, ii = 0;
void mcxiDisassembly(string &mcxi) {
int sum = 0;
int number = 0;
for (int i = 0; i <= mcxi.length(); i++) {
if (mcxi[i] != 'm' && mcxi[i] != 'c' && mcxi[i] != 'x' && mcxi[i] != 'i') {
number = (int)(mcxi[i] - '0');
} else {
switch (mcxi[i]) {
case 'm':
if (number == 0) {
m += 1;
} else {
m += number;
}
break;
case 'c':
if (number == 0) {
c += 1;
} else {
c += number;
}
break;
case 'x':
if (number == 0) {
x += 1;
} else {
x += number;
}
break;
case 'i':
if (number == 0) {
ii += 1;
} else {
ii += number;
}
break;
}
number = 0;
}
}
}
void show() {
if (m != 0) {
if (m == 1) {
cout << 'm';
} else {
cout << m << 'm';
}
}
if (c != 0) {
if (c == 1) {
cout << 'c';
} else {
cout << c << 'c';
}
}
if (x != 0) {
if (x == 1) {
cout << 'x';
} else {
cout << x << 'x';
}
}
if (ii != 0) {
if (ii == 1) {
cout << 'i';
} else {
cout << ii << 'i';
}
}
cout << endl;
}
int main() {
string mcxi1, mcxi2;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> mcxi1 >> mcxi2;
//????§£
mcxiDisassembly(mcxi1);
mcxiDisassembly(mcxi2);
if (ii >= 10) {
x += ii / 10;
ii -= (ii / 10) * 10;
}
if (x >= 10) {
c += x / 10;
x -= (x / 10) * 10;
}
if (c >= 10) {
m += c / 10;
c -= (c / 10) * 10;
}
//??¨???
show();
m = c = x = ii = 0;
}
return 0;
}
|
[["-", 64, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 64, 9, 0, 57, 15, 339, 51, 16, 12, 13]]
| 1
| 517
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
const int INF = 1 << 28;
int toi(string in) {
int ans = 0;
REP(i, in.size()) {
int num = 0;
if (i != 0)
num = (int)in[i - 1] - '0';
if (in[i] == 'm') {
if (0 < num && num < 10)
ans += num * 1000;
else
ans += 1000;
} else if (in[i] == 'c') {
if (0 < num && num < 10)
ans += num * 100;
else
ans += 100;
} else if (in[i] == 'x') {
if (0 < num && num < 10)
ans += num * 10;
else
ans += 10;
} else if (in[i] == 'i') {
if (0 < num && num < 10)
ans += num;
else
ans += 1;
}
}
return ans;
}
int main() {
int n;
cin >> n;
REP(i, n) {
string in1, in2;
cin >> in1 >> in2;
// cout << toi(in1) << endl;
// cout << toi(in2) << endl;
int ans = toi(in1) + toi(in2);
if (ans / 1000) {
if (ans / 1000 > 1)
cout << ans / 1000;
cout << 'm';
}
if (ans % 1000 / 100) {
if (ans % 1000 > 1)
cout << ans % 1000 / 100;
cout << 'c';
}
if (ans % 100 / 10) {
if (ans % 100 / 10 > 1)
cout << ans % 100 / 10;
cout << 'x';
}
if (ans % 10) {
if (ans % 10 > 1)
cout << ans % 10;
cout << 'i';
}
cout << endl;
}
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
const int INF = 1 << 28;
int toi(string in) {
int ans = 0;
REP(i, in.size()) {
int num = 0;
if (i != 0)
num = (int)in[i - 1] - '0';
if (in[i] == 'm') {
if (0 < num && num < 10)
ans += num * 1000;
else
ans += 1000;
} else if (in[i] == 'c') {
if (0 < num && num < 10)
ans += num * 100;
else
ans += 100;
} else if (in[i] == 'x') {
if (0 < num && num < 10)
ans += num * 10;
else
ans += 10;
} else if (in[i] == 'i') {
if (0 < num && num < 10)
ans += num;
else
ans += 1;
}
}
return ans;
}
int main() {
int n;
cin >> n;
REP(i, n) {
string in1, in2;
cin >> in1 >> in2;
int ans = toi(in1) + toi(in2);
if (ans / 1000) {
if (ans / 1000 > 1)
cout << ans / 1000;
cout << 'm';
}
if (ans % 1000 / 100) {
if (ans % 1000 / 100 > 1)
cout << ans % 1000 / 100;
cout << 'c';
}
if (ans % 100 / 10) {
if (ans % 100 / 10 > 1)
cout << ans % 100 / 10;
cout << 'x';
}
if (ans % 10) {
if (ans % 10 > 1)
cout << ans % 10;
cout << 'i';
}
cout << endl;
}
return 0;
}
|
[["+", 0, 57, 15, 339, 51, 16, 31, 16, 17, 85], ["+", 0, 57, 15, 339, 51, 16, 31, 16, 12, 13]]
| 1
| 435
|
#include <stdio.h>
int main() {
int i, sum, n, j, b, a, k;
char m, m2, o;
scanf("%d%c", &a, &o);
for (i = 0; i < a; i++) {
sum = 0;
while (1) {
if (scanf("%c", &m) == EOF)
break;
if (m == '\n')
break;
if ('2' <= m && m <= '9') {
scanf("%c", &m2);
if (m2 == 'i')
sum = sum + (m - '0');
if (m2 == 'x')
sum = sum + (m - '0') * 10;
if (m2 == 'c')
sum = sum + (m - '0') * 100;
if (m2 == 'm')
sum = sum + (m - '0') * 1000;
} else if (m == 'i' || m == 'x' || m == 'c' || m == 'm') {
if (m == 'i')
sum = sum + 1;
if (m == 'x')
sum = sum + 10;
if (m == 'c')
sum = sum + 100;
if (m == 'm')
sum = sum + 1000;
}
}
if (sum >= 1000) {
b = sum / 1000;
if (b != 1)
printf("%dm", b);
else
printf("m");
sum -= b * 1000;
}
if (sum >= 100) {
b = sum / 100;
if (b != 1)
printf("%dc", b);
else
printf("c");
sum -= b * 100;
}
if (sum >= 10) {
b = sum / 10;
if (b != 1)
printf("%dx", b);
else
printf("x");
sum -= b * 10;
}
if (sum >= 1) {
if (sum != 1)
printf("%di", b);
else
printf("i");
}
printf("\n");
}
return 0;
}
|
#include <stdio.h>
int main() {
int i, sum, n, j, b, a, k;
char m, m2, o;
scanf("%d%c", &a, &o);
for (i = 0; i < a; i++) {
sum = 0;
while (1) {
if (scanf("%c", &m) == EOF)
break;
if (m == '\n')
break;
if ('2' <= m && m <= '9') {
scanf("%c", &m2);
if (m2 == 'i')
sum = sum + (m - '0');
if (m2 == 'x')
sum = sum + (m - '0') * 10;
if (m2 == 'c')
sum = sum + (m - '0') * 100;
if (m2 == 'm')
sum = sum + (m - '0') * 1000;
} else if (m == 'i' || m == 'x' || m == 'c' || m == 'm') {
if (m == 'i')
sum = sum + 1;
if (m == 'x')
sum = sum + 10;
if (m == 'c')
sum = sum + 100;
if (m == 'm')
sum = sum + 1000;
}
}
if (sum >= 1000) {
b = sum / 1000;
if (b != 1)
printf("%dm", b);
else
printf("m");
sum -= b * 1000;
}
if (sum >= 100) {
b = sum / 100;
if (b != 1)
printf("%dc", b);
else
printf("c");
sum -= b * 100;
}
if (sum >= 10) {
b = sum / 10;
if (b != 1)
printf("%dx", b);
else
printf("x");
sum -= b * 10;
}
if (sum >= 1) {
if (sum != 1)
printf("%di", sum);
else
printf("i");
}
printf("\n");
}
return 0;
}
|
[["-", 0, 57, 64, 1, 0, 2, 3, 4, 0, 22], ["+", 0, 57, 64, 1, 0, 2, 3, 4, 0, 22]]
| 1
| 461
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, s1;
int n, i;
cin >> n;
while (n--) {
cin >> s >> s1;
int w1 = 1, w2 = 0, w3 = 0, w4 = 0;
int sum = 0;
for (i = 0; i < s.size(); i++) {
if (s[i] >= '2' && s[i] <= '9')
w1 = s[i] - '0';
else if (s[i] == 'm') {
sum += 1000 * w1;
w1 = 1;
} else if (s[i] == 'c') {
sum += 100 * w1;
w1 = 1;
} else if (s[i] == 'x') {
sum += 10 * w1;
w1 = 1;
} else if (s[i] == 'i') {
sum += w1;
w1 = 1;
}
}
for (i = 0; i < s1.size(); i++) {
if (s1[i] >= '2' && s1[i] <= '9')
w1 = s1[i] - '0';
else if (s1[i] == 'm') {
sum += 1000 * w1;
w1 = 1;
} else if (s1[i] == 'c') {
sum += 100 * w1;
w1 = 1;
} else if (s1[i] == 'x') {
sum += 10 * w1;
w1 = 1;
} else if (s1[i] == 'i') {
sum += w1;
w1 = 1;
}
}
w1 = sum / 1000;
if (w1 == 1)
cout << "m";
else if (w1 > 1)
cout << w1 << "m";
w2 = (sum - w1 * 1000) / 100;
if (w2 == 1)
cout << "c";
else if (w2 > 1)
cout << w2 << "c";
w3 = (sum - w1 * 1000 - w2 * 100) / 10;
if (w3 == 1)
cout << "x";
else if (w3 > 1)
cout << w3 << "x";
w4 = (sum - w1 * 1000 - w2 * 100 - w3 * 10);
if (w4 == 1)
cout << "m";
else if (w4 > 1)
cout << w4 << "m";
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string s, s1;
int n, i;
cin >> n;
while (n--) {
cin >> s >> s1;
int w1 = 1, w2 = 0, w3 = 0, w4 = 0;
int sum = 0;
for (i = 0; i < s.size(); i++) {
if (s[i] >= '2' && s[i] <= '9')
w1 = s[i] - '0';
else if (s[i] == 'm') {
sum += 1000 * w1;
w1 = 1;
} else if (s[i] == 'c') {
sum += 100 * w1;
w1 = 1;
} else if (s[i] == 'x') {
sum += 10 * w1;
w1 = 1;
} else if (s[i] == 'i') {
sum += w1;
w1 = 1;
}
}
for (i = 0; i < s1.size(); i++) {
if (s1[i] >= '2' && s1[i] <= '9')
w1 = s1[i] - '0';
else if (s1[i] == 'm') {
sum += 1000 * w1;
w1 = 1;
} else if (s1[i] == 'c') {
sum += 100 * w1;
w1 = 1;
} else if (s1[i] == 'x') {
sum += 10 * w1;
w1 = 1;
} else if (s1[i] == 'i') {
sum += w1;
w1 = 1;
}
}
w1 = sum / 1000;
if (w1 == 1)
cout << "m";
else if (w1 > 1)
cout << w1 << "m";
w2 = (sum - w1 * 1000) / 100;
if (w2 == 1)
cout << "c";
else if (w2 > 1)
cout << w2 << "c";
w3 = (sum - w1 * 1000 - w2 * 100) / 10;
if (w3 == 1)
cout << "x";
else if (w3 > 1)
cout << w3 << "x";
w4 = (sum - w1 * 1000 - w2 * 100 - w3 * 10);
if (w4 == 1)
cout << "i";
else if (w4 > 1)
cout << w4 << "i";
cout << endl;
}
return 0;
}
|
[["-", 0, 57, 64, 1, 0, 16, 12, 5, 0, 6], ["+", 0, 57, 64, 1, 0, 16, 12, 5, 0, 6]]
| 1
| 516
|
#include <bits/stdc++.h>
#define ll long long
#define inf 100000000
using namespace std;
int n;
int toInt(char a) {
char b[8] = {'2', '3', '4', '5', '6', '7', '8', '9'};
for (int i = 0; i < 8; i++) {
if (a == b[i]) {
return 2 + i;
}
}
}
bool isint(char a) {
char b[8] = {'2', '3', '4', '5', '6', '7', '8', '9'};
for (int i = 0; i < 8; i++) {
if (a == b[i])
return true;
}
return false;
}
int is(string a) {
char o, p, q, r;
int b = inf, c = inf, d = inf, e = inf;
for (int i = 0; i < a.length(); i++) {
if (a[i] == 'm') {
b = i;
}
if (a[i] == 'c') {
c = i;
}
if (a[i] == 'x') {
d = i;
}
if (a[i] == 'i') {
e = i;
}
}
int ans = 0;
if (b == 0) {
ans += 1000;
} else if (b == 1) {
ans += toInt(a[0]) * 1000;
}
if (c != inf) {
if (c == 0) {
ans += 100;
} else if (isint(a[c - 1])) {
ans += toInt(a[c - 1]) * 100;
} else {
ans += 100;
}
}
if (d != inf) {
if (d == 0) {
ans += 10;
} else if (isint(a[d - 1])) {
ans += toInt(a[d - 1]) * 10;
} else {
ans += 10;
}
}
if (e != inf) {
if (e == 0) {
ans += 1;
} else if (isint(a[e - 1])) {
ans += toInt(a[e - 1]);
} else {
ans += 1;
}
}
return ans;
}
char toChar(int a) {
if (a == 2)
return '2';
if (a == 3)
return '3';
if (a == 4)
return '4';
if (a == 5)
return '5';
if (a == 6)
return '6';
if (a == 7)
return '7';
if (a == 8)
return '8';
if (a == 9)
return '9';
}
int main() {
cin >> n;
int cont = 0;
while (cont < n) {
string s;
string t;
cin >> s >> t;
int m = is(s) + is(t);
// cout<<m<<endl;
int a = m / 1000;
m %= 1000;
int b = m / 100;
m %= 100;
int c = m / 10;
m %= 10;
int d = m;
string ans;
if (a == 1) {
ans += "m";
} else if (a > 1) {
ans += toChar(a);
ans += "m";
}
if (b == 1) {
ans += "c";
} else if (b > 1) {
ans += toChar(b);
ans += "c";
}
if (c == 1) {
ans += "x";
} else if (c > 1) {
ans += toChar(c);
ans += "x";
}
if (d == 1) {
ans += "i";
} else if (d > 1) {
ans += toChar(c);
ans += "i";
}
cout << ans << endl;
cont++;
}
}
|
#include <bits/stdc++.h>
#define ll long long
#define inf 100000000
using namespace std;
int n;
int toInt(char a) {
char b[8] = {'2', '3', '4', '5', '6', '7', '8', '9'};
for (int i = 0; i < 8; i++) {
if (a == b[i]) {
return 2 + i;
}
}
}
bool isint(char a) {
char b[8] = {'2', '3', '4', '5', '6', '7', '8', '9'};
for (int i = 0; i < 8; i++) {
if (a == b[i])
return true;
}
return false;
}
int is(string a) {
char o, p, q, r;
int b = inf, c = inf, d = inf, e = inf;
for (int i = 0; i < a.length(); i++) {
if (a[i] == 'm') {
b = i;
}
if (a[i] == 'c') {
c = i;
}
if (a[i] == 'x') {
d = i;
}
if (a[i] == 'i') {
e = i;
}
}
int ans = 0;
if (b == 0) {
ans += 1000;
} else if (b == 1) {
ans += toInt(a[0]) * 1000;
}
if (c != inf) {
if (c == 0) {
ans += 100;
} else if (isint(a[c - 1])) {
ans += toInt(a[c - 1]) * 100;
} else {
ans += 100;
}
}
if (d != inf) {
if (d == 0) {
ans += 10;
} else if (isint(a[d - 1])) {
ans += toInt(a[d - 1]) * 10;
} else {
ans += 10;
}
}
if (e != inf) {
if (e == 0) {
ans += 1;
} else if (isint(a[e - 1])) {
ans += toInt(a[e - 1]);
} else {
ans += 1;
}
}
return ans;
}
char toChar(int a) {
if (a == 2)
return '2';
if (a == 3)
return '3';
if (a == 4)
return '4';
if (a == 5)
return '5';
if (a == 6)
return '6';
if (a == 7)
return '7';
if (a == 8)
return '8';
if (a == 9)
return '9';
}
int main() {
cin >> n;
int cont = 0;
while (cont < n) {
string s;
string t;
cin >> s >> t;
int m = is(s) + is(t);
// cout<<m<<endl;
int a = m / 1000;
m %= 1000;
int b = m / 100;
m %= 100;
int c = m / 10;
m %= 10;
int d = m;
string ans;
if (a == 1) {
ans += "m";
} else if (a > 1) {
ans += toChar(a);
ans += "m";
}
if (b == 1) {
ans += "c";
} else if (b > 1) {
ans += toChar(b);
ans += "c";
}
if (c == 1) {
ans += "x";
} else if (c > 1) {
ans += toChar(c);
ans += "x";
}
if (d == 1) {
ans += "i";
} else if (d > 1) {
ans += toChar(d);
ans += "i";
}
cout << ans << endl;
cont++;
}
}
|
[["-", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22], ["+", 0, 1, 0, 11, 12, 2, 3, 4, 0, 22]]
| 1
| 842
|
#include <bits/stdc++.h>
using namespace std;
#define R(i, n) for (int i = 0; i < n; ++i)
int n, a, b, y[] = {1000, 100, 10, 1};
map<char, int> x;
string s, t = "mcxi";
main() {
x['m'] = 1000, x['c'] = 100, x['x'] = 10, x['i'] = 1;
cin >> n;
R(i, n) {
a = 0;
R(j, 2) {
cin >> s;
R(k, s.size())
t.find(s[k]) != string::npos ? a += x[s[k]]
: a += (s[k] - '0') * x[s[++k]];
}
R(j, 4) {
b = 1;
while (a / y[j])
a -= y[j], ++b;
--b > 2 && cout << b << t[j];
--b || cout << t[j];
}
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define R(i, n) for (int i = 0; i < n; ++i)
int n, a, b, y[] = {1000, 100, 10, 1};
map<char, int> x;
string s, t = "mcxi";
main() {
x['m'] = 1000, x['c'] = 100, x['x'] = 10, x['i'] = 1;
cin >> n;
R(i, n) {
a = 0;
R(j, 2) {
cin >> s;
R(k, s.size())
t.find(s[k]) != string::npos ? a += x[s[k]]
: a += (s[k] - '0') * x[s[++k]];
}
R(j, 4) {
b = 1;
while (a / y[j])
a -= y[j], ++b;
--b > 1 && cout << b << t[j];
--b || cout << t[j];
}
cout << endl;
}
}
|
[["-", 0, 9, 0, 1, 0, 16, 31, 16, 12, 13], ["+", 0, 9, 0, 1, 0, 16, 31, 16, 12, 13]]
| 1
| 239
|
m[10][2], x[10][2], z[10][2], zz[10][2];
rotx[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
roty[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
mov[4][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
main(N, M, Z, t, i, j, k, mxm, mym, mxM, myM, zxm, zym, zxM, zyM) {
for (; scanf("%d", &N), N; puts("+++++")) {
/*input zero*/
zxm = zym = 9999, zxM = zyM = -9999;
for (scanf("%d", &Z), i = 0; i < Z; i++) {
scanf("%d%d", z[i], z[i] + 1);
if (zxm > z[i][0])
zxm = z[i][0];
if (zym > z[i][1])
zym = z[i][1];
if (zxM < z[i][0])
zxM = z[i][0];
if (zyM < z[i][1])
zyM = z[i][1];
}
for (i = 0; i < Z; i++)
z[i][0] -= zxm, z[i][1] -= zym, zz[Z - 1 - i][0] = z[i][0],
zz[Z - 1 - i][0] = z[i][1];
zxM -= zxm, zyM -= zym;
for (j = 0; j < N; j++) {
/*input line*/
mxm = mym = 9999, mxM = myM = -9999;
for (scanf("%d", &M), i = 0; i < M; i++) {
scanf("%d%d", m[i], m[i] + 1);
if (mxm > m[i][0])
mxm = m[i][0];
if (mym > m[i][1])
mym = m[i][1];
if (mxM < m[i][0])
mxM = m[i][0];
if (myM < m[i][1])
myM = m[i][1];
}
for (i = 0; i < M; i++)
m[i][0] -= mxm, m[i][1] -= mym;
mxM -= mxm, myM -= mym;
if (M != Z)
continue;
mov[1][0] = myM, mov[2][0] = mxM, mov[2][1] = myM, mov[3][1] = mxM;
/*compare*/
for (k = 0; k < 4; k++) {
for (i = 0; i < M; i++)
x[i][0] = rotx[k][0] * m[i][0] + rotx[k][1] * m[i][1] + mov[k][0],
x[i][1] = roty[k][0] * m[i][0] + roty[k][1] * m[i][1] + mov[k][1];
if (!memcmp(x, z, 4 * 2 * M) || !memcmp(x, zz, 4 * 2 * M))
printf("%d\n", j + 1), k = 4;
}
}
}
return 0;
}
|
m[10][2], x[10][2], z[10][2], zz[10][2];
rotx[4][2] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
roty[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
mov[4][2] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
main(N, M, Z, t, i, j, k, mxm, mym, mxM, myM, zxm, zym, zxM, zyM) {
for (; scanf("%d", &N), N; puts("+++++")) {
/*input zero*/
zxm = zym = 9999, zxM = zyM = -9999;
for (scanf("%d", &Z), i = 0; i < Z; i++) {
scanf("%d%d", z[i], z[i] + 1);
if (zxm > z[i][0])
zxm = z[i][0];
if (zym > z[i][1])
zym = z[i][1];
if (zxM < z[i][0])
zxM = z[i][0];
if (zyM < z[i][1])
zyM = z[i][1];
}
for (i = 0; i < Z; i++)
z[i][0] -= zxm, z[i][1] -= zym, zz[Z - 1 - i][0] = z[i][0],
zz[Z - 1 - i][1] = z[i][1];
zxM -= zxm, zyM -= zym;
for (j = 0; j < N; j++) {
/*input line*/
mxm = mym = 9999, mxM = myM = -9999;
for (scanf("%d", &M), i = 0; i < M; i++) {
scanf("%d%d", m[i], m[i] + 1);
if (mxm > m[i][0])
mxm = m[i][0];
if (mym > m[i][1])
mym = m[i][1];
if (mxM < m[i][0])
mxM = m[i][0];
if (myM < m[i][1])
myM = m[i][1];
}
for (i = 0; i < M; i++)
m[i][0] -= mxm, m[i][1] -= mym;
mxM -= mxm, myM -= mym;
if (M != Z)
continue;
mov[1][0] = myM, mov[2][0] = mxM, mov[2][1] = myM, mov[3][1] = mxM;
/*compare*/
for (k = 0; k < 4; k++) {
for (i = 0; i < M; i++)
x[i][0] = rotx[k][0] * m[i][0] + rotx[k][1] * m[i][1] + mov[k][0],
x[i][1] = roty[k][0] * m[i][0] + roty[k][1] * m[i][1] + mov[k][1];
if (!memcmp(x, z, 4 * 2 * M) || !memcmp(x, zz, 4 * 2 * M))
printf("%d\n", j + 1), k = 4;
}
}
}
return 0;
}
|
[["-", 12, 34, 12, 34, 12, 11, 31, 69, 71, 13], ["+", 12, 34, 12, 34, 12, 11, 31, 69, 71, 13]]
| 0
| 843
|
#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P;
typedef vector<P> LL;
#define EPS (1e-10)
LL A;
int N;
void viewLine(LL a) {
cout << "view LINE" << endl;
for (int i = 0; i < (int)a.size(); i++) {
printf("%.8lf %.8lf\n", a[i].real(), a[i].imag());
}
}
void inputLine(LL &l) {
int m;
cin >> m;
int px, py;
cin >> px >> py;
for (int i = 1; i < m; i++) {
int x, y;
cin >> x >> y;
l.push_back(P(x, y) - P(px, py));
px = x;
py = y;
}
}
P rotate(P p, double theta) {
theta = theta * M_PI / 180.0;
double x = real(p) * cos(theta) - imag(p) * sin(theta);
double y = real(p) * sin(theta) + imag(p) * cos(theta);
return P(x, y);
}
LL rotate(LL l, double theta) {
for (int j = 0; j < (int)l.size(); j++) {
l[j] = rotate(l[j], 90.0);
}
return l;
}
bool eqv(P a, P b) {
return fabs(a.real() - b.real()) < EPS && fabs(a.imag() - b.imag()) < EPS;
}
LL reverse(LL b) {
LL r;
for (int i = (int)b.size() - 1; i > -1; i--) {
r.push_back(-b[i]);
}
return r;
}
bool check(LL a, LL b) {
if (a.size() != b.size())
return false;
for (int i = 0; i < 4; i++) {
bool f = true;
for (int j = 0; j < (int)a.size(); j++) {
if (!eqv(a[j], b[j]))
f = false;
}
if (f)
return true;
a = rotate(a, 90.0);
}
}
bool isCheck(LL a, LL b) { return check(a, b) || check(a, reverse(b)); }
int main() {
while (cin >> N && N) {
A.clear();
inputLine(A);
/*
viewLine(A);
viewLine(A=rotate(A,90.0));
viewLine(A=rotate(A,90.0));
viewLine(A=rotate(A,90.0));
*/
for (int i = 0; i < N; i++) {
LL B;
inputLine(B);
// view(B);
if (isCheck(A, B))
cout << i + 1 << endl;
}
cout << "+++++" << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef complex<double> P;
typedef vector<P> LL;
#define EPS (1e-10)
LL A;
int N;
void viewLine(LL a) {
cout << "view LINE" << endl;
for (int i = 0; i < (int)a.size(); i++) {
printf("%.8lf %.8lf\n", a[i].real(), a[i].imag());
}
}
void inputLine(LL &l) {
int m;
cin >> m;
int px, py;
cin >> px >> py;
for (int i = 1; i < m; i++) {
int x, y;
cin >> x >> y;
l.push_back(P(x, y) - P(px, py));
px = x;
py = y;
}
}
P rotate(P p, double theta) {
theta = theta * M_PI / 180.0;
double x = real(p) * cos(theta) - imag(p) * sin(theta);
double y = real(p) * sin(theta) + imag(p) * cos(theta);
return P(x, y);
}
LL rotate(LL l, double theta) {
for (int j = 0; j < (int)l.size(); j++) {
l[j] = rotate(l[j], 90.0);
}
return l;
}
bool eqv(P a, P b) {
return fabs(a.real() - b.real()) < EPS && fabs(a.imag() - b.imag()) < EPS;
}
LL reverse(LL b) {
LL r;
for (int i = (int)b.size() - 1; i > -1; i--) {
r.push_back(-b[i]);
}
return r;
}
bool check(LL a, LL b) {
if (a.size() != b.size())
return false;
for (int i = 0; i < 4; i++) {
bool f = true;
for (int j = 0; j < (int)a.size(); j++) {
if (!eqv(a[j], b[j]))
f = false;
}
if (f)
return true;
a = rotate(a, 90.0);
}
return false;
}
bool isCheck(LL a, LL b) { return check(a, b) || check(a, reverse(b)); }
int main() {
while (cin >> N && N) {
A.clear();
inputLine(A);
/*
viewLine(A);
viewLine(A=rotate(A,90.0));
viewLine(A=rotate(A,90.0));
viewLine(A=rotate(A,90.0));
*/
for (int i = 0; i < N; i++) {
LL B;
inputLine(B);
// view(B);
if (isCheck(A, B))
cout << i + 1 << endl;
}
cout << "+++++" << endl;
}
}
|
[["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 38], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 147], ["+", 0, 30, 0, 14, 8, 9, 0, 37, 0, 35]]
| 1
| 611
|
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int GetDir(int x1, int y1, int x2, int y2) {
if (x1 < x2)
return 1; // right
else if (x1 > x2)
return 3; // left
else if (y1 > y2)
return 4; // down
else if (y1 < y2)
return 2; // up
return 0;
}
vi GetMageData(vi x, vi y) {
vi rst;
int olddir = GetDir(x[0], y[0], x[1], y[1]);
FOR(i, 1, x.size()) {
int newdir = GetDir(x[i], y[i], x[i + 1], y[i + 1]);
int mage;
if (olddir == 1) {
if (newdir == 2)
mage = 1; // hantai
else
mage = 0; // clock
} else if (olddir == 2) {
if (newdir == 3)
mage = 1;
else
mage = 0;
} else if (olddir == 3) {
if (newdir == 4)
mage = 1;
else
mage = 0;
} else {
if (newdir == 1)
mage = 1;
else
mage = 0;
}
rst.pb(mage);
olddir = newdir;
}
return rst;
}
vi GetDisData(vi x, vi y) {
vi rst;
int sx = 0, sy = 0;
FOR(i, 1, x.size()) {
x[i] -= x[0];
y[i] -= y[0];
}
x[0] = 0;
y[0] = 0;
FOR(i, 1, x.size()) {
rst.pb((x[i] - sx) * (x[i] - sx) + (y[i] - sy) * (y[i] - sy));
sx = x[i];
sy = y[i];
}
return rst;
}
int main() {
int n;
vi len01, mag01, len02, mag02;
int num0;
while (1) {
cin >> n;
if (n == 0)
break;
// 0
vi tx, ty, tx2, ty2;
cin >> num0;
REP(i, num0) {
int xx, yy;
cin >> xx >> yy;
tx.pb(xx);
ty.pb(yy);
}
len01.clear();
len02.clear();
mag02.clear();
mag02.clear();
tx2 = tx, ty2 = ty;
len01 = GetDisData(tx, ty);
mag01 = GetMageData(tx, ty);
reverse(All(tx2));
reverse(All(ty2));
len02 = GetDisData(tx2, ty2);
mag02 = GetMageData(tx2, ty2);
/*REP(kk,len01.size()){
cout << len01[kk] << " ";
}
cout << endl;
REP(kk,mag01.size()){
cout << mag01[kk] << " ";
}
cout << endl;
REP(kk,len02.size()){
cout << len02[kk] << " ";
}
cout << endl;
REP(kk,mag02.size()){
cout << mag02[kk] << " ";
}
cout <<"hogehogehoge"<< endl;*/
REP(i, n) {
int k;
vi l, m;
vi dx, dy;
cin >> k;
REP(j, k) {
int xxx, yyy;
cin >> xxx >> yyy;
dx.pb(xxx);
dy.pb(yyy);
}
l = GetDisData(dx, dy);
m = GetMageData(dx, dy);
/*REP(kk,l.size()){
cout << l[kk] << " ";
}
cout << endl;
REP(kk,m.size()){
cout << m[kk] << " ";
}
cout << endl;*/
bool flg = false;
if (l == len01 && m == mag01)
flg = true;
if (l == len02 && m == mag02)
flg = true;
if (flg)
cout << i + 1 << endl;
}
cout << "+++++" << endl;
}
}
|
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
int GetDir(int x1, int y1, int x2, int y2) {
if (x1 < x2)
return 1; // right
else if (x1 > x2)
return 3; // left
else if (y1 > y2)
return 4; // down
else if (y1 < y2)
return 2; // up
return 0;
}
vi GetMageData(vi x, vi y) {
vi rst;
int olddir = GetDir(x[0], y[0], x[1], y[1]);
FOR(i, 1, x.size() - 1) {
int newdir = GetDir(x[i], y[i], x[i + 1], y[i + 1]);
int mage;
if (olddir == 1) {
if (newdir == 2)
mage = 1; // hantai
else
mage = 0; // clock
} else if (olddir == 2) {
if (newdir == 3)
mage = 1;
else
mage = 0;
} else if (olddir == 3) {
if (newdir == 4)
mage = 1;
else
mage = 0;
} else {
if (newdir == 1)
mage = 1;
else
mage = 0;
}
rst.pb(mage);
olddir = newdir;
}
return rst;
}
vi GetDisData(vi x, vi y) {
vi rst;
int sx = 0, sy = 0;
FOR(i, 1, x.size()) {
x[i] -= x[0];
y[i] -= y[0];
}
x[0] = 0;
y[0] = 0;
FOR(i, 1, x.size()) {
rst.pb((x[i] - sx) * (x[i] - sx) + (y[i] - sy) * (y[i] - sy));
sx = x[i];
sy = y[i];
}
return rst;
}
int main() {
int n;
vi len01, mag01, len02, mag02;
int num0;
while (1) {
cin >> n;
if (n == 0)
break;
// 0
vi tx, ty, tx2, ty2;
cin >> num0;
REP(i, num0) {
int xx, yy;
cin >> xx >> yy;
tx.pb(xx);
ty.pb(yy);
}
len01.clear();
len02.clear();
mag02.clear();
mag02.clear();
tx2 = tx, ty2 = ty;
len01 = GetDisData(tx, ty);
mag01 = GetMageData(tx, ty);
reverse(All(tx2));
reverse(All(ty2));
len02 = GetDisData(tx2, ty2);
mag02 = GetMageData(tx2, ty2);
/*REP(kk,len01.size()){
cout << len01[kk] << " ";
}
cout << endl;
REP(kk,mag01.size()){
cout << mag01[kk] << " ";
}
cout << endl;
REP(kk,len02.size()){
cout << len02[kk] << " ";
}
cout << endl;
REP(kk,mag02.size()){
cout << mag02[kk] << " ";
}
cout <<"hogehogehoge"<< endl;*/
REP(i, n) {
int k;
vi l, m;
vi dx, dy;
cin >> k;
REP(j, k) {
int xxx, yyy;
cin >> xxx >> yyy;
dx.pb(xxx);
dy.pb(yyy);
}
l = GetDisData(dx, dy);
m = GetMageData(dx, dy);
/*REP(kk,l.size()){
cout << l[kk] << " ";
}
cout << endl;
REP(kk,m.size()){
cout << m[kk] << " ";
}
cout << endl;*/
bool flg = false;
if (l == len01 && m == mag01)
flg = true;
if (l == len02 && m == mag02)
flg = true;
if (flg)
cout << i + 1 << endl;
}
cout << "+++++" << endl;
}
}
|
[["+", 0, 1, 0, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 1, 0, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 827
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define MP make_pair
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define fst first
#define snd second
int get_dir(int x1, int y1, int x2, int y2) {
if (x1 == x2) {
return y2 > y1 ? 0 : 2;
} else {
return x2 > x1 ? 1 : 3;
}
}
Vi make_line(int t, int *X, int *Y) {
Vi ret;
int x0 = X[0], y0 = Y[0], px = X[1], py = Y[1];
ret.push_back(abs(x0 - px) + abs(y0 - py));
int pd = get_dir(x0, y0, px, py);
FOR(i, 2, t) {
int x = X[i], y = Y[i];
int d = get_dir(px, py, x, y);
ret.push_back((d + 1) % 4 == pd ? 1 : -1);
ret.push_back(abs(x - px) + abs(y - py));
px = x, py = y, pd = d;
}
return ret;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
while (1) {
int N;
cin >> N;
if (N == 0)
break;
int t, x[16], y[16];
cin >> t;
REP(i, t) cin >> x[i] >> y[i];
Vi target = make_line(t, x, y);
int T = target.size();
REP(n, N) {
int m, mx[16], my[16];
cin >> m;
REP(i, m) cin >> mx[i] >> my[i];
Vi line = make_line(m, mx, my);
if (T != (int)line.size())
continue;
bool flag = true;
REP(i, T) {
if (target[i] != line[i]) {
flag = false;
break;
}
}
if (flag) {
cout << n + 1 << endl;
continue;
}
REP(i, m / 2) {
swap(mx[i], mx[m - i - 1]);
swap(my[i], my[m - i - 1]);
}
Vi line_ = make_line(m, mx, my);
if (T != (int)line_.size())
continue;
flag = true;
REP(i, T) {
if (target[i] != line_[i]) {
flag = false;
break;
}
}
if (flag) {
cout << n + 1 << endl;
continue;
}
}
cout << "*****" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define MP make_pair
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define fst first
#define snd second
int get_dir(int x1, int y1, int x2, int y2) {
if (x1 == x2) {
return y2 > y1 ? 0 : 2;
} else {
return x2 > x1 ? 1 : 3;
}
}
Vi make_line(int t, int *X, int *Y) {
Vi ret;
int x0 = X[0], y0 = Y[0], px = X[1], py = Y[1];
ret.push_back(abs(x0 - px) + abs(y0 - py));
int pd = get_dir(x0, y0, px, py);
FOR(i, 2, t) {
int x = X[i], y = Y[i];
int d = get_dir(px, py, x, y);
ret.push_back((d + 1) % 4 == pd ? 1 : -1);
ret.push_back(abs(x - px) + abs(y - py));
px = x, py = y, pd = d;
}
return ret;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
while (1) {
int N;
cin >> N;
if (N == 0)
break;
int t, x[16], y[16];
cin >> t;
REP(i, t) cin >> x[i] >> y[i];
Vi target = make_line(t, x, y);
int T = target.size();
REP(n, N) {
int m, mx[16], my[16];
cin >> m;
REP(i, m) cin >> mx[i] >> my[i];
Vi line = make_line(m, mx, my);
if (T != (int)line.size())
continue;
bool flag = true;
REP(i, T) {
if (target[i] != line[i]) {
flag = false;
break;
}
}
if (flag) {
cout << n + 1 << endl;
continue;
}
REP(i, m / 2) {
swap(mx[i], mx[m - i - 1]);
swap(my[i], my[m - i - 1]);
}
Vi line_ = make_line(m, mx, my);
if (T != (int)line_.size())
continue;
flag = true;
REP(i, T) {
if (target[i] != line_[i]) {
flag = false;
break;
}
}
if (flag) {
cout << n + 1 << endl;
continue;
}
}
cout << "+++++" << endl;
}
return 0;
}
|
[["-", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6], ["+", 0, 1, 0, 16, 31, 16, 12, 5, 0, 6]]
| 1
| 656
|
def round(points)
3.times.each_with_object([points]){ |_, obj| obj << obj[-1].map{ |p| [-p[1], p[0]] } }
end
def same(a, b)
a.size == b.size && a.zip(b).map { |(xa, ya), (xb, yb)| [xb - xa, yb - ya] }.uniq.size == 1
end
while (n = gets.to_i) != 0
target = gets.to_i.times.map{ gets.split.map(&:to_i) }
cand = n.times.map{ gets.to_i.times.map{ gets.split.map(&:to_i) } }
(1..n).each do |i|
[target, target.reverse].flat_map{ |t| round(t) }.each do |t|
if same(t, cand[i])
puts i
break
end
end
end
puts '+++++'
end
|
def round(points)
3.times.each_with_object([points]){ |_, obj| obj << obj[-1].map{ |p| [-p[1], p[0]] } }
end
def same(a, b)
a.size == b.size && a.zip(b).map { |(xa, ya), (xb, yb)| [xb - xa, yb - ya] }.uniq.size == 1
end
while (n = gets.to_i) != 0
target = gets.to_i.times.map{ gets.split.map(&:to_i) }
cand = n.times.map{ gets.to_i.times.map{ gets.split.map(&:to_i) } }
(1..n).each do |i|
[target, target.reverse].flat_map{ |t| round(t) }.each do |t|
if same(t, cand[i - 1])
puts i
break
end
end
end
puts '+++++'
end
|
[["+", 15, 652, 3, 4, 0, 742, 0, 738, 17, 33], ["+", 15, 652, 3, 4, 0, 742, 0, 738, 12, 612]]
| 4
| 216
|
mx = [0, 1, 0, -1]
my = [-1, 0, 1, 0]
while True:
w, h = map(int, input().split())
if w == h == 0:
break
tiles = [[i for i in input()] for j in range(h)]
ax = ay = 0
for i in range(h):
for j in range(w):
if tiles[i][j] == '@':
ax, ay = j, i
que = [(ax, ay)]
tiles[ay][ax] = '#'
cnt = 1
while 0 < len(que):
new_que = []
for x, y in que:
for i in range(4):
nx, ny = x + mx[i], y + my[i]
print(nx, ny)
if 0 <= nx <= w - 1 and 0 <= ny <= h - 1:
if tiles[ny][nx] == '.':
cnt += 1
new_que.append((nx, ny))
tiles[ny][nx] = '#'
que = new_que
print(cnt)
|
mx = [0, 1, 0, -1]
my = [-1, 0, 1, 0]
while True:
w, h = map(int, input().split())
if w == h == 0:
break
tiles = [[i for i in input()] for j in range(h)]
ax = ay = 0
for i in range(h):
for j in range(w):
if tiles[i][j] == '@':
ax, ay = j, i
que = [(ax, ay)]
tiles[ay][ax] = '#'
cnt = 1
while 0 < len(que):
new_que = []
for x, y in que:
for i in range(4):
nx, ny = x + mx[i], y + my[i]
if 0 <= nx <= w - 1 and 0 <= ny <= h - 1:
if tiles[ny][nx] == '.':
cnt += 1
new_que.append((nx, ny))
tiles[ny][nx] = '#'
que = new_que
print(cnt)
|
[["-", 0, 7, 8, 196, 0, 1, 0, 652, 63, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 21], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25]]
| 5
| 246
|
def move_a(y,x,count):
count += 1
li[x][y] = "#"
if li[x-1][y] == ".":
count = move_a(y,x-1,count)
if li[x+1][y]== ".":
count = move_a(y,x+1, count)
if li[x][y-1]== ".":
count = move_a(y-1,x, count)
if li[x][y+1]== ".":
count = move_a(y+1,x, count)
return count
while True:
yoko, tate = list(map(int,input().split()))
if yoko == 0 and tate == 0:
break
li = []
rec_list = []
count = 0
li.append(["#"] * (yoko + 2))
for i in range(tate):
temp = input()
temp = "#" + temp +"#"
li.append(list(temp))
li.append(["#"]*(yoko+2))
print(li)
for i in li:
if '@' in i:
a_posx,a_posy = i.index('@'),li.index(i)
print(move_a(a_posx,a_posy,count))
|
def move_a(y,x,count):
count += 1
li[x][y] = "#"
if li[x-1][y] == ".":
count = move_a(y,x-1,count)
if li[x+1][y]== ".":
count = move_a(y,x+1, count)
if li[x][y-1]== ".":
count = move_a(y-1,x, count)
if li[x][y+1]== ".":
count = move_a(y+1,x, count)
return count
while True:
yoko, tate = list(map(int,input().split()))
if yoko == 0 and tate == 0:
break
li = []
rec_list = []
count = 0
li.append(["#"] * (yoko + 2))
for i in range(tate):
temp = input()
temp = "#" + temp +"#"
li.append(list(temp))
li.append(["#"]*(yoko+2))
for i in li:
if '@' in i:
a_posx,a_posy = i.index('@'),li.index(i)
print(move_a(a_posx,a_posy,count))
|
[["-", 0, 52, 8, 196, 0, 1, 0, 652, 63, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["-", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25]]
| 5
| 288
|
if __name__ == "__main__":
while 1:
w,h = list(map(int,input().strip().split()))
if w == h == 0:break
data = []
data = []
for _ in range(h):data.append(list(input()))
visited = []
for i in range(h):
if '@'in data[i]:stack= [(i,data[i].index('@'))]
count = 0
while len(stack) !=0:
y,x= stack.pop()
count +=1
if y-1 >= 0 and not (y-1,x)in visited and data[y -1][x] == '.':
visited.append((y-1,x))
stack.append((y-1,x))
if x -1 >= 0 and not (y,x-1)in visited and data[y][x-1] == '.':
visited.append((y,x-1))
stack.append((y,x-1))
if y +1 < h and not (y+1,x)in visited and data[y +1][x] == '.':
visited.append((y+1,x))
stack.append((y+1,x))
if x + 1 < w and not (y,x+1)in visited and data[y][x +1] == '.':
visited.append((y,x+1))
stack.append((y,x+1))
|
#data????§???????[y???][x???]??§??????
if __name__ == "__main__":
while 1:
w,h = list(map(int,input().strip().split()))
if w == h == 0:break
data = []
data = []
for _ in range(h):data.append(list(input()))
visited = []
for i in range(h):
if '@'in data[i]:stack= [(i,data[i].index('@'))]
count = 0
while len(stack) !=0:
y,x= stack.pop()
count +=1
if y-1 >= 0 and not (y-1,x)in visited and data[y -1][x] == '.':
visited.append((y-1,x))
stack.append((y-1,x))
if x -1 >= 0 and not (y,x-1)in visited and data[y][x-1] == '.':
visited.append((y,x-1))
stack.append((y,x-1))
if y +1 < h and not (y+1,x)in visited and data[y +1][x] == '.':
visited.append((y+1,x))
stack.append((y+1,x))
if x + 1 < w and not (y,x+1)in visited and data[y][x +1] == '.':
visited.append((y,x+1))
stack.append((y,x+1))
print(count)
|
[["+", 0, 52, 8, 196, 0, 1, 0, 652, 63, 22], ["+", 8, 196, 0, 1, 0, 652, 3, 4, 0, 24], ["+", 8, 196, 0, 1, 0, 652, 3, 4, 0, 22], ["+", 8, 196, 0, 1, 0, 652, 3, 4, 0, 25]]
| 5
| 356
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
Scanner sc;
Main() { sc = new Scanner(System.in); }
int ni() { return sc.nextInt(); }
public static void main(String[] args) { new Main().run(); }
int a, n;
int p, q;
int dfs(int last, int sa, int sn, double sm) {
if (sn > n) {
return 0;
}
int cnt = 0;
if (Math.abs(sm - (double)p / q) < 1e-6) {
++cnt;
}
if ((sm - (double)p / q) > 1e-6) {
return 0;
}
for (int i = last; i <= a / sa; ++i) {
cnt += dfs(i, sa * i, sn + 1, sm + 1.0 / i);
}
return cnt;
}
void run() {
for (;;) {
p = ni();
q = ni();
a = ni();
n = ni();
if (p == 0) {
break;
}
System.out.println(dfs(1, 1, 0, 0));
}
}
void debug(Object... os) { System.err.println(Arrays.deepToString(os)); }
}
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
Scanner sc;
Main() { sc = new Scanner(System.in); }
int ni() { return sc.nextInt(); }
public static void main(String[] args) { new Main().run(); }
int a, n;
int p, q;
int dfs(int last, int sa, int sn, double sm) {
if (sn > n) {
return 0;
}
int cnt = 0;
if (Math.abs(sm - (double)p / q) < 1e-9) {
++cnt;
}
if ((sm - (double)p / q) > 1e-9) {
return 0;
}
for (int i = last; i <= a / sa; ++i) {
cnt += dfs(i, sa * i, sn + 1, sm + 1.0 / i);
}
return cnt;
}
void run() {
for (;;) {
p = ni();
q = ni();
a = ni();
n = ni();
if (p == 0) {
break;
}
System.out.println(dfs(1, 1, 0, 0));
}
}
void debug(Object... os) { System.err.println(Arrays.deepToString(os)); }
}
|
[["-", 8, 196, 0, 57, 15, 15, 0, 16, 12, 515], ["+", 8, 196, 0, 57, 15, 15, 0, 16, 12, 515]]
| 3
| 287
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const double EPS = 1e-6;
int a, n;
int dfs(double x, double sum, int num, int limit, int step) {
if (abs(x - sum) <= EPS)
return 1;
if (limit == 0 || step == 0)
return 0;
int res = 0;
for (int i = num; i <= limit; i++) {
res += dfs(x, sum + 1.0 / i, i, limit / i, step - 1);
}
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int p, q;
while (cin >> p >> q >> a >> n, p | q | a | n) {
cout << dfs((double)p / q, 0.0, 1, a, n) << endl;
}
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
int a, n;
int dfs(double x, double sum, int num, int limit, int step) {
if (abs(x - sum) <= EPS)
return 1;
if (limit == 0 || step == 0)
return 0;
int res = 0;
for (int i = num; i <= limit; i++) {
res += dfs(x, sum + 1.0 / i, i, limit / i, step - 1);
}
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int p, q;
while (cin >> p >> q >> a >> n, p | q | a | n) {
cout << dfs((double)p / q, 0.0, 1, a, n) << endl;
}
return 0;
}
|
[["-", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13], ["+", 36, 36, 0, 30, 0, 43, 49, 50, 51, 13]]
| 1
| 222
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9 + 7)
#define endl "\n"
#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")
#define MAX
#define NUM
#define numerator first
#define denominator second
int con;
inline pair<int, int> add(pair<int, int> a, pair<int, int> b) {
int nume = a.numerator * b.denominator + b.numerator * a.denominator;
int deno = a.denominator * b.denominator;
int gcd = __gcd(abs(nume), abs(deno));
nume /= gcd;
deno /= gcd;
return make_pair(nume, deno);
}
inline int solve(pair<int, int> fra, int b, int a, int n) {
int res = 0, temp = 1, hoge;
// cout<<fra.numerator<<"/"<<fra.denominator<<" "<<b<<" "<<a<<" "<<n<<endl;
if (n == 0)
return 0;
if (n == 1) {
if (fra.numerator == 1 && b <= fra.denominator && fra.denominator <= a)
return 1;
else
return 0;
}
if (fra.numerator * b > fra.denominator * n)
return -1;
// cout<<"temp "<<temp<<endl;
for (int i = 1; fra.numerator * b > fra.denominator * i; i++) {
temp *= b;
if (temp > a) {
// con++;
return -1;
}
}
// cout<<"temp "<<temp<<endl;
for (int i = max(b, fra.denominator / fra.numerator); i <= a; i++) {
if (fra.numerator * i == fra.denominator)
res++;
else if (fra.numerator * i > fra.denominator) {
hoge = solve(add(fra, make_pair(-1, i)), i, a / i, n - 1);
if (hoge == -1)
break;
res += hoge;
}
}
return res;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
// cout<<fixed<<setprecision(10);
int p, q, a, n;
while (true) {
cin >> p >> q >> a >> n;
if (!p && !q && !a && !n)
break;
int gcd = __gcd(p, q);
p /= gcd;
q /= gcd;
// cout<<con<<endl;
cout << solve(make_pair(p, q), 1, a, n) << endl;
}
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9 + 7)
#define endl "\n"
#define yn(f) ((f) ? "Yes" : "No")
#define YN(f) ((f) ? "YES" : "NO")
#define MAX
#define NUM
#define numerator first
#define denominator second
int con;
inline pair<int, int> add(pair<int, int> a, pair<int, int> b) {
int nume = a.numerator * b.denominator + b.numerator * a.denominator;
int deno = a.denominator * b.denominator;
int gcd = __gcd(abs(nume), abs(deno));
nume /= gcd;
deno /= gcd;
return make_pair(nume, deno);
}
inline int solve(pair<int, int> fra, int b, int a, int n) {
int res = 0, temp = 1, hoge;
// cout<<fra.numerator<<"/"<<fra.denominator<<" "<<b<<" "<<a<<" "<<n<<endl;
if (n == 0)
return 0;
if (n == 1) {
if (fra.numerator == 1 && b <= fra.denominator && fra.denominator <= a)
return 1;
else
return 0;
}
if (fra.numerator * b > fra.denominator * n)
return 0;
// cout<<"temp "<<temp<<endl;
for (int i = 1; fra.numerator * b > fra.denominator * i; i++) {
temp *= b;
if (temp > a) {
// con++;
return -1;
}
}
// cout<<"temp "<<temp<<endl;
for (int i = max(b, fra.denominator / fra.numerator); i <= a; i++) {
if (fra.numerator * i == fra.denominator)
res++;
else if (fra.numerator * i > fra.denominator) {
hoge = solve(add(fra, make_pair(-1, i)), i, a / i, n - 1);
if (hoge == -1)
break;
res += hoge;
}
}
return res;
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
// cout<<fixed<<setprecision(10);
int p, q, a, n;
while (true) {
cin >> p >> q >> a >> n;
if (!p && !q && !a && !n)
break;
int gcd = __gcd(p, q);
p /= gcd;
q /= gcd;
// cout<<con<<endl;
cout << solve(make_pair(p, q), 1, a, n) << endl;
}
return 0;
}
|
[["-", 0, 14, 8, 9, 0, 57, 64, 37, 0, 13], ["+", 0, 14, 8, 9, 0, 57, 64, 37, 0, 13]]
| 1
| 491
|
#include <iostream>
using namespace std;
int P, Q, A, N;
int res;
void solve(int a, int n, int sum, int b) {
if (P * a == Q * sum) {
res++;
return;
}
if (P * a < Q * sum || n >= 0)
return;
for (int i = b; a * i <= A; i++)
solve(a * i, n + 1, sum * i + a, i);
}
int main() {
while (cin >> P >> Q >> A >> N, P | Q | A | N) {
res = 0;
for (int i = 1; i <= A; i++) {
solve(i, 1, 1, i);
}
cout << res << endl;
}
}
|
#include <iostream>
using namespace std;
int P, Q, A, N;
int res;
void solve(int a, int n, int sum, int b) {
if (P * a == Q * sum) {
res++;
return;
}
if (P * a < Q * sum || n >= N)
return;
for (int i = b; a * i <= A; i++)
solve(a * i, n + 1, sum * i + a, i);
}
int main() {
while (cin >> P >> Q >> A >> N, P | Q | A | N) {
res = 0;
for (int i = 1; i <= A; i++) {
solve(i, 1, 1, i);
}
cout << res << endl;
}
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 43, 51, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 43, 51, 22]]
| 1
| 169
|
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int gcd(int p, int q) { return p == 0 ? q : gcd(q % p, p); }
int dfs(int p, int q, int a, int n, int m, int pre) {
if (p == 0)
return 1;
if (n == 0)
return 0;
int res = 0;
for (int i = pre; i * m <= a; i++) {
if (n * q < p * i)
break;
int newp = p * i - q;
if (newp < 0)
continue;
int newq = q * i;
int g = gcd(newp, newq);
newp /= g, newq /= g;
res += dfs(newp, newq, a, n, m * i, i);
}
return res;
}
signed main() {
int p, q, a, n;
while (cin >> p >> q >> a >> n, p) {
int res = dfs(p, q, a, n, 1, 1);
cout << res << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long int
int gcd(int p, int q) { return p == 0 ? q : gcd(q % p, p); }
int dfs(int p, int q, int a, int n, int m, int pre) {
if (p == 0)
return 1;
if (n == 0)
return 0;
int res = 0;
for (int i = pre; i * m <= a; i++) {
if (n * q < p * i)
break;
int newp = p * i - q;
if (newp < 0)
continue;
int newq = q * i;
int g = gcd(newp, newq);
newp /= g, newq /= g;
res += dfs(newp, newq, a, n - 1, m * i, i);
}
return res;
}
signed main() {
int p, q, a, n;
while (cin >> p >> q >> a >> n, p) {
int res = dfs(p, q, a, n, 1, 1);
cout << res << endl;
}
return 0;
}
|
[["+", 0, 11, 12, 2, 3, 4, 0, 16, 17, 33], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 12, 13]]
| 1
| 234
|
/*
2017_03_02_AC
??????????????????
????¬????????????????????????????i???A?????§????????????????????????????????§???????????????*i???A????¶??????????????????§?????????
????´???????????????????????????????????????£??????(a/b)+(c/d)??????(a*d+c*b)/(b*d)??¨??????????????§?????????
???????????????????????¨LTE???
????????????????????????????????´??\?????????????????§??????????????§?¢?????????????
??????????????°?????????????¨????????????????
???(sum/pro) + (1/i) = (sum*d+pro)/(pro*i)
?????????????????§???????§???????????????¨??????????????????
????????????
???pair???????????????????????????
????´??????????????????¬????????§?????????
?????????????????¨???????????????????????????????????°?????????????????????A??\?????¨???????????¶????????§???
????????????pro??????????´???????????????????????????§???????????????????????????
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
using namespace std;
int P, Q, A, N, ans;
void dfs(int term, int minnum, int prod, int sum) {
if (A < prod || N < term)
return;
if (sum * Q == prod * P) { /* (sum/prod) = (P/Q) ????????????????????? */
ans++;
return;
}
for (int i = minnum; prod * i <= A; i++) { /* ?¬?????¶???????????????°????????????????°??????????????????????????????????????????????minnum??\???
*/
dfs(term + 1, i, prod * i,
sum * i + prod); /* ????????§??? (sum/prod)+(1/i)=(sum*i+prod)/(prod*i)
??????????????? */
}
return;
}
int main() {
while (1) {
cin >> P >> Q >> A >> N;
if (P == 0 && Q == 0 && A == 0 && N == 0)
break;
ans = 0;
// REP(i,1,A+1){ /* ?????????(????°???????)???????±????????????? */
// dfs(1,i,i,1);
//}
dfs(1, 1, 1, 0);
cout << ans << endl;
}
return 0;
}
|
/*
2017_03_02_AC
??????????????????
????¬????????????????????????????i???A?????§????????????????????????????????§???????????????*i???A????¶??????????????????§?????????
????´???????????????????????????????????????£??????(a/b)+(c/d)??????(a*d+c*b)/(b*d)??¨??????????????§?????????
???????????????????????¨LTE???
????????????????????????????????´??\?????????????????§??????????????§?¢?????????????
??????????????°?????????????¨????????????????
???(sum/pro) + (1/i) = (sum*d+pro)/(pro*i)
?????????????????§???????§???????????????¨??????????????????
????????????
???pair???????????????????????????
????´??????????????????¬????????§?????????
?????????????????¨???????????????????????????????????°?????????????????????A??\?????¨???????????¶????????§???
????????????pro??????????´???????????????????????????§???????????????????????????
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define CH(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
using namespace std;
int P, Q, A, N, ans;
void dfs(int term, int minnum, int prod, int sum) {
if (A < prod || N < term)
return;
if (sum * Q == prod * P) { /* (sum/prod) = (P/Q) ????????????????????? */
ans++;
return;
}
for (int i = minnum; prod * i <= A; i++) { /* ?¬?????¶???????????????°????????????????°??????????????????????????????????????????????minnum??\???
*/
dfs(term + 1, i, prod * i,
sum * i + prod); /* ????????§??? (sum/prod)+(1/i)=(sum*i+prod)/(prod*i)
??????????????? */
}
return;
}
int main() {
while (1) {
cin >> P >> Q >> A >> N;
if (P == 0 && Q == 0 && A == 0 && N == 0)
break;
ans = 0;
// REP(i,1,A+1){ /* ?????????(????°???????)???????±????????????? */
// dfs(1,i,i,1);
//}
dfs(0, 1, 1, 0);
cout << ans << endl;
}
return 0;
}
|
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
| 1
| 221
|
#include <iostream>
using namespace std;
int p, q, a, n;
int seek(int pp, int qq, int aa, int nn) {
if (p * qq == pp * q)
return 1;
if (p * qq < pp * q)
return 0;
if (nn == n)
return 0;
int count = 0;
for (int i = aa; i * qq <= a; i++)
count += seek(qq + pp * i, i * qq, i, n + 1);
return count;
}
int main() {
while (cin >> p >> q >> a >> n, p || q || a || n) {
cout << seek(0, 1, 1, 0) << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
int p, q, a, n;
int seek(int pp, int qq, int aa, int nn) {
if (p * qq == pp * q)
return 1;
if (p * qq < pp * q)
return 0;
if (nn == n)
return 0;
int count = 0;
for (int i = aa; i * qq <= a; i++) {
count += seek(qq + pp * i, i * qq, i, nn + 1);
}
return count;
}
int main() {
while (cin >> p >> q >> a >> n, p || q || a || n) {
cout << seek(0, 1, 1, 0) << endl;
}
return 0;
}
|
[["+", 0, 14, 8, 9, 0, 7, 8, 9, 0, 45], ["-", 0, 11, 12, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 14, 8, 9, 0, 7, 8, 9, 0, 46]]
| 1
| 158
|
// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on
main {
while (true) {
int p, q, a, n;
cin >> p >> q >> a >> n;
if (p == 0 && q == 0 && a == 0 && n == 0)
break;
function<int(int, int, int, int)> dfs = [&](int N, int P, int Q,
int lim) -> int {
if (Q * p == P * q)
return 1;
if (Q * p < P * q || N == 0)
return 0;
int cnt = 0;
for (int i = lim; i * Q <= a; i++)
cnt += dfs(n - 1, i * P + Q, Q * i, i);
return cnt;
};
cout << dfs(n, 0, 1, 1) << endl;
}
}
|
// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on
main {
while (true) {
int p, q, a, n;
cin >> p >> q >> a >> n;
if (p == 0 && q == 0 && a == 0 && n == 0)
break;
function<int(int, int, int, int)> dfs = [&](int N, int P, int Q,
int lim) -> int {
if (Q * p == P * q)
return 1;
if (Q * p < P * q || N == 0)
return 0;
int cnt = 0;
for (int i = lim; i * Q <= a; i++)
cnt += dfs(N - 1, i * P + Q, Q * i, i);
return cnt;
};
cout << dfs(n, 0, 1, 1) << endl;
}
}
|
[["-", 0, 11, 12, 2, 3, 4, 0, 16, 31, 22], ["+", 0, 11, 12, 2, 3, 4, 0, 16, 31, 22]]
| 1
| 749
|
#include "bits/stdc++.h"
using namespace std;
int p, q, a, n;
int ans;
int dfs(int deep, int bs, int bb, int x) {
// cout<<deep<<" "<<bs<<" "<<bb<<endl;
if (p * bb == q * bs) {
// cout<<bs<<" "<<bb<<endl;
ans++;
}
if (deep != n) {
// cout<<deep<<" "<<bs<<" "<<bb<<endl;
for (int i = x; i <= a; i++) {
int nbs = bb + bs * i;
int nbb = bb * i;
if (nbb > a)
return 0;
/*
if(nbs%p==0&&nbb%q==0&&nbs/p==nbb/q){
cout<<nbs<<" "<<nbb<<endl;
ans++;
}else if(p%nbs==0&&q%nbb==0&&p/nbs==q/nbb){
ans++;
}
*/
dfs(deep + 1, nbs, nbb, i);
}
}
}
int main() {
while (1) {
cin >> p >> q >> a >> n;
if (p == 0 && q == 0 && a == 0 && n == 0)
break;
ans = 0;
for (int i = 1; i <= a; i++) {
dfs(0, 1, i, i);
}
cout << ans << endl;
}
}
|
#include "bits/stdc++.h"
using namespace std;
int p, q, a, n;
int ans;
int dfs(int deep, int bs, int bb, int x) {
// cout<<deep<<" "<<bs<<" "<<bb<<endl;
if (p * bb == q * bs) {
// cout<<bs<<" "<<bb<<endl;
ans++;
}
if (deep != n) {
// cout<<deep<<" "<<bs<<" "<<bb<<endl;
for (int i = x; i <= a; i++) {
int nbs = bb + bs * i;
int nbb = bb * i;
if (nbb > a)
return 0;
/*
if(nbs%p==0&&nbb%q==0&&nbs/p==nbb/q){
cout<<nbs<<" "<<nbb<<endl;
ans++;
}else if(p%nbs==0&&q%nbb==0&&p/nbs==q/nbb){
ans++;
}
*/
dfs(deep + 1, nbs, nbb, i);
}
}
}
int main() {
while (1) {
cin >> p >> q >> a >> n;
if (p == 0 && q == 0 && a == 0 && n == 0)
break;
ans = 0;
for (int i = 1; i <= a; i++) {
dfs(1, 1, i, i);
}
cout << ans << endl;
}
}
|
[["-", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13], ["+", 8, 9, 0, 1, 0, 2, 3, 4, 0, 13]]
| 1
| 197
|
#include <iostream>
#include <vector>
using namespace std;
int p, q, a, n;
int num;
void calc(int depth, vector<int> &hist, int max_l) {
if (depth >= 7)
return;
int mul = 1;
for (int i = 0; i < hist.size(); i++)
mul *= hist[i];
for (int i = max_l;; i++) {
int next_mul = mul * i;
if (next_mul > a)
break;
hist.push_back(i);
int upper = 0;
for (int j = 0; j < hist.size(); j++) {
upper += next_mul / hist[j];
}
if (upper * q == p * next_mul)
num++;
if (upper * q < p * next_mul) {
calc(depth + 1, hist, i);
}
hist.pop_back();
}
}
int main() {
while (cin >> p >> q >> a >> n) {
if (p == 0 && q == 0 && a == 0 && n == 0)
break;
num = 0;
vector<int> hist = vector<int>();
calc(0, hist, 1);
cout << num << endl;
}
}
|
#include <iostream>
#include <vector>
using namespace std;
int p, q, a, n;
int num;
void calc(int depth, vector<int> &hist, int max_l) {
if (depth >= n)
return;
int mul = 1;
for (int i = 0; i < hist.size(); i++)
mul *= hist[i];
for (int i = max_l;; i++) {
int next_mul = mul * i;
if (next_mul > a)
break;
hist.push_back(i);
int upper = 0;
for (int j = 0; j < hist.size(); j++) {
upper += next_mul / hist[j];
}
if (upper * q == p * next_mul) {
num++;
}
if (upper * q < p * next_mul) {
calc(depth + 1, hist, i);
}
hist.pop_back();
}
}
int main() {
while (cin >> p >> q >> a >> n) {
if (p == 0 && q == 0 && a == 0 && n == 0)
break;
num = 0;
vector<int> hist = vector<int>();
calc(0, hist, 1);
cout << num << endl;
}
}
|
[["-", 8, 9, 0, 57, 15, 339, 51, 16, 12, 13], ["+", 8, 9, 0, 57, 15, 339, 51, 16, 12, 22], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 45], ["+", 0, 7, 8, 9, 0, 57, 64, 9, 0, 46]]
| 1
| 259
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.