NCPC 2017 Preliminary Round

Problem

A

Just 3n + 1.

 1#include <bits/stdc++.h>
 2using namespace std;
 3
 4#define sz(x) (int(x.size()))
 5typedef long long ll;
 6typedef double db;
 7
 8int main() {
 9    ll x;
10    while (scanf(" %lld", &x)) {
11        if (x == 0) break;
12        printf("%lld ", x);
13
14        ll mx = -1;
15        int i;
16        for (i = 0; ; i++) {
17            if (x & 1) {
18                x = 3 * x + 1;
19            }
20            else {
21                x = x / 2;
22            }
23            mx = max(mx, x);
24            if (x == 1) {
25                break;
26            }
27        }
28
29        printf("%d %lld\n", i + 1, mx);
30    }
31
32
33    return 0;
34}

B

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5typedef long long ll;
 6
 7void solve()
 8{
 9    int n;
10    scanf("%d", &n);
11
12    int inp[n];
13    for(int i = 0; i < n; i++) {
14        scanf("%d", &inp[i]);
15    }
16
17    sort(inp, inp + n);
18
19    // implement
20    // -------++++++++
21    ll product = inp[0] * inp[1];
22    ll ans = LLONG_MIN;
23    for(int i = 2; i < n; i++) {
24        ans = max(ans, product * inp[i]);
25    }
26
27    product = inp[n - 1] * inp[n - 2];
28    for(int i = 0; i < n - 2; i++) {
29        ans = max(ans, product * inp[i]);
30    }
31
32    printf("%lld\n", ans);
33}
34
35int main()
36{
37    int ncase;
38    scanf("%d", &ncase);
39
40    while(ncase--)
41        solve();
42
43    return 0;
44}

C

Ahh…… We can’t do the math with mod inverse… so we resort to enumeration…

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5typedef long long ll;
 6const ll P = 65537; 
 7
 8ll x[3];
 9ll y[3];
10ll cal(ll a0, ll a1, ll a2, int which)
11{
12    ll x_2 = x[which] * x[which] % P;
13    return (((a0 + a1 * x[which]) % P + a2 * x_2) % P);
14}
15
16void solve()
17{
18    for(int i = 0; i < 3; i++)
19        scanf("%lld %lld", &x[i], &y[i]);
20    
21    ll x_2 = (x[0] * x[0]) % P;
22    for(ll a1 = 0; a1 < P; a1++) {
23        for(ll a2 = 0; a2 < P; a2++) {
24            ll a0 = 5 * P + y[0] - (a1 * x[0]) % P - (a2 * x_2) % P;
25            a0 %= P;
26            assert(cal(a0, a1, a2, 0) == y[0]);
27
28            if(cal(a0, a1, a2, 1) == y[1] && cal(a0, a1, a2, 2) == y[2]) {
29                printf("%lld %lld %lld\n", a0, a1, a2);
30                return;
31            }
32        }
33    }
34}
35
36int main()
37{
38    int ncase;
39    scanf("%d", &ncase);
40
41    while(ncase--)
42        solve();
43
44    return 0;
45}

D

Problem statement contains error…………..

  1#include <bits/stdc++.h>
  2using namespace std;
  3
  4const int S = 4;
  5const int INA = 2; // inactive
  6const int ADA = 0;
  7const int XY = 1;
  8const int NON = 3; // nonreachable
  9const int ACT = 5; // active
 10
 11int sx, sy;
 12int A[8][8];
 13int Z[8][8];
 14
 15bool xy_check(int tx, int ty) {
 16    for (int x = min(sx, tx); x <= max(sx, tx); x++) {
 17        if (A[x][sy] == INA) {
 18            return false;
 19        }
 20    }
 21    for (int y = min(sy, ty); y <= max(sy, ty); y++) {
 22        if (A[tx][y] == INA) {
 23            return false;
 24        }
 25    }
 26    return true;
 27}
 28
 29bool ada_check(int tx, int ty) {
 30    for (int x = min(sx, tx); x <= max(sx, tx); x++) {
 31        for (int y = min(sy, ty); y <= max(sy, ty); y++) {
 32            if (A[x][y] == INA) {
 33                return false;
 34            }
 35        }
 36    }
 37    return true;
 38}
 39
 40void print(int a[8][8]) {
 41    for (int y = 0; y < 8; y++) {
 42        for (int x = 0; x < 8; x++) {
 43            cout << a[x][y];
 44        }
 45        cout << endl;
 46    }
 47}
 48
 49int main() {
 50    ios::sync_with_stdio(false);
 51    cin.tie(0);
 52
 53    string s;
 54    while (cin >> s) {
 55        if (s == "0") break;
 56
 57        for (int x = 0; x < 8; x++) {
 58            for (int y = 0; y < 8; y++) {
 59                Z[x][y] = NON;
 60            }
 61        }
 62
 63        for (int i = 0; i < 64; i++) {
 64            int y = i / 8;
 65            int x = i % 8;
 66            if (s[i] == '2') {
 67                A[x][y] = S;
 68                Z[x][y] = S;
 69                sx = x;
 70                sy = y;
 71            } else if (s[i] == '1') {
 72                A[x][y] = INA;
 73                Z[x][y] = INA;
 74            } else {
 75                A[x][y] = ACT;
 76            }
 77        }
 78
 79        // print(A);
 80        // cout << endl;
 81
 82        for (int x = 0; x < 8; x++) {
 83            for (int y = 0; y < 8; y++) {
 84                if (x == sx && y == sy) continue;
 85                if (xy_check(x, y)) {
 86                    Z[x][y] = XY;
 87                }
 88            }
 89        }
 90
 91        for (int x = 0; x < 8; x++) {
 92            for (int y = 0; y < 8; y++) {
 93                if (x == sx && y == sy) continue;
 94                if (ada_check(x, y)) {
 95                    Z[x][y] = ADA;
 96                }
 97            }
 98        }
 99
100        for (int y = 0; y < 8; y++) {
101            for (int x = 0; x < 8; x++) {
102                cout << Z[x][y];
103            }
104        }
105        cout << endl;
106
107        // print(Z);
108    }
109    return 0;
110}

G

I know using graph for this one is overkill, but this is what makes us feel like 1 AC :)

 1#include <bits/stdc++.h>
 2
 3using namespace std;
 4
 5#define N 10010
 6
 7typedef pair<int, int> ii;
 8
 9void solve(int n)
10{
11    int inp[n + 1];
12    for(int i = 1; i <= n; i++)
13        scanf("%d", &inp[i]);
14    // for(int i = 1; i <= n; i++)
15        //printf("%d%c", inp[i], i == n ? '\n' : ' ');
16
17    vector<int> g[N];
18    int in[N] = {0};
19    int out[N] = {0};
20    for(int i = 1; i <= n; i++) {
21        g[i].push_back(inp[i]);
22        in[inp[i]]++;
23        out[i]++;
24    }
25    
26    vector<int> s;
27    for(int i = 1; i <= n; i++) {
28        if(in[i] == 0)
29            s.push_back(i);
30    }
31    
32    vector<ii> ans;
33    for(int i = 0; i < (int) s.size(); i++) {
34        int cnt = 0;
35        int who = s[i];
36        // printf("cnt %d who %d\n", cnt, who);
37        while((int)g[who].size() > 0) {
38            // printf("cnt %d who %d\n", cnt, who);
39            cnt++;
40            who = g[who][0];
41        }
42
43        ans.push_back(ii(s[i], cnt));
44    }   
45    sort(ans.begin(), ans.end());
46    
47    printf("%d", (int)ans.size());
48    for(int i = 0; i < (int)ans.size(); i++)
49        printf(" %d %d", ans[i].first, ans[i].second);
50    printf("\n");
51}
52
53int main()
54{
55    int n;
56    while(scanf("%d", &n) == 1 && n)
57        solve(n);
58
59    return 0;
60}