Submission #134503

# Submission time Handle Problem Language Result Execution time Memory
134503 2019-07-22T22:56:49 Z RezwanArefin01 Remittance (JOI19_remittance) C++17
100 / 100
975 ms 23912 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii; 

const int N = 1e6 + 10; 
const int mod = 1e9 + 7;
ll a[N], b[N], f[N];  
int n;

inline int Pow(int a, int p) {
    int ret = 1; while(p) {
        if(p & 1) ret = (ll) ret * a % mod;
        a = (ll) a * a % mod;
        p >>= 1;
    } return ret;
}

int main(int argc, char const *argv[]) {
#ifdef LOCAL
    freopen("in", "r", stdin);
#endif
    scanf("%d", &n);

    ll pw = 1, I = 0; 
    for(int i = 0; i < n; ++i) {
        scanf("%d %d", &a[i], &b[i]); 
        I += pw * (a[i] - b[i] + mod) % mod;
        if(I >= mod) I -= mod;
        pw = pw * 2 % mod;
    }

    I *= Pow(pw - 1, mod - 2); 
    I %= mod;

    f[n] = I;
    
    auto no = []() { puts("No"); exit(0); };

    for(int k = 70; k--; ) {
        int idx = 0; 
        for(int i = n - 1; i > 0; --i) {
            f[i] = 2 * f[i + 1] - a[i];
            if(f[i] <= 0) { idx = i; break; }
        }   

        for(int i = idx; i < n - 1; ++i) {
            ll x = min(a[i] / 2, f[i + 1]);
            a[i] -= 2 * x; 
            a[(i + 1) % n] += x;
        }

        ll x = min(a[n - 1] / 2, f[n]); 
        a[n - 1] -= 2 * x;
        f[n] -= x; 
        a[0] += x;

        if(idx) break; 
    }

    for(int i = 0; i < n; ++i) {
        int x = a[i] - b[i];
        if(x < 0 || (x & 1)) no();
        a[i] -= x; 
        a[(i + 1) % n] += x / 2;
    }

    for(int i = 0; i < n; ++i) {
        if(a[i] != b[i]) no(); 
    }

    puts("Yes"); 
}

Compilation message

remittance.cpp: In function 'int main(int, const char**)':
remittance.cpp:28:36: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'll* {aka long long int*}' [-Wformat=]
         scanf("%d %d", &a[i], &b[i]); 
                        ~~~~~       ^
remittance.cpp:28:36: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'll* {aka long long int*}' [-Wformat=]
remittance.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ~~~~~^~~~~~~~~~
remittance.cpp:28:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &a[i], &b[i]); 
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 380 KB Output is correct
36 Correct 2 ms 276 KB Output is correct
37 Correct 2 ms 376 KB Output is correct
38 Correct 2 ms 380 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 380 KB Output is correct
41 Correct 2 ms 376 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 376 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 2 ms 376 KB Output is correct
48 Correct 1 ms 376 KB Output is correct
49 Correct 2 ms 372 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 376 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 380 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 376 KB Output is correct
58 Correct 2 ms 376 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 376 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 380 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 376 KB Output is correct
22 Correct 2 ms 376 KB Output is correct
23 Correct 2 ms 376 KB Output is correct
24 Correct 2 ms 376 KB Output is correct
25 Correct 2 ms 376 KB Output is correct
26 Correct 2 ms 376 KB Output is correct
27 Correct 2 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 376 KB Output is correct
30 Correct 2 ms 376 KB Output is correct
31 Correct 2 ms 376 KB Output is correct
32 Correct 2 ms 376 KB Output is correct
33 Correct 2 ms 376 KB Output is correct
34 Correct 2 ms 376 KB Output is correct
35 Correct 2 ms 380 KB Output is correct
36 Correct 2 ms 276 KB Output is correct
37 Correct 2 ms 376 KB Output is correct
38 Correct 2 ms 380 KB Output is correct
39 Correct 2 ms 376 KB Output is correct
40 Correct 2 ms 380 KB Output is correct
41 Correct 2 ms 376 KB Output is correct
42 Correct 2 ms 376 KB Output is correct
43 Correct 2 ms 376 KB Output is correct
44 Correct 2 ms 376 KB Output is correct
45 Correct 2 ms 376 KB Output is correct
46 Correct 2 ms 376 KB Output is correct
47 Correct 2 ms 376 KB Output is correct
48 Correct 1 ms 376 KB Output is correct
49 Correct 2 ms 372 KB Output is correct
50 Correct 2 ms 376 KB Output is correct
51 Correct 2 ms 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 376 KB Output is correct
54 Correct 2 ms 376 KB Output is correct
55 Correct 2 ms 380 KB Output is correct
56 Correct 2 ms 376 KB Output is correct
57 Correct 2 ms 376 KB Output is correct
58 Correct 2 ms 376 KB Output is correct
59 Correct 2 ms 376 KB Output is correct
60 Correct 2 ms 376 KB Output is correct
61 Correct 2 ms 376 KB Output is correct
62 Correct 2 ms 376 KB Output is correct
63 Correct 385 ms 15920 KB Output is correct
64 Correct 184 ms 16004 KB Output is correct
65 Correct 372 ms 16108 KB Output is correct
66 Correct 182 ms 15996 KB Output is correct
67 Correct 201 ms 16024 KB Output is correct
68 Correct 227 ms 15992 KB Output is correct
69 Correct 377 ms 16248 KB Output is correct
70 Correct 199 ms 16072 KB Output is correct
71 Correct 190 ms 15992 KB Output is correct
72 Correct 224 ms 15964 KB Output is correct
73 Correct 371 ms 16124 KB Output is correct
74 Correct 171 ms 16140 KB Output is correct
75 Correct 190 ms 15992 KB Output is correct
76 Correct 220 ms 15996 KB Output is correct
77 Correct 372 ms 16016 KB Output is correct
78 Correct 975 ms 23836 KB Output is correct
79 Correct 262 ms 15996 KB Output is correct
80 Correct 292 ms 16124 KB Output is correct
81 Correct 267 ms 15992 KB Output is correct
82 Correct 872 ms 23912 KB Output is correct
83 Correct 162 ms 16244 KB Output is correct
84 Correct 171 ms 16056 KB Output is correct
85 Correct 331 ms 15904 KB Output is correct
86 Correct 318 ms 15956 KB Output is correct
87 Correct 928 ms 23836 KB Output is correct
88 Correct 237 ms 15964 KB Output is correct