Codeforces Edu Round 48 A-D

A. Death Note

Simple simulation, available \ (\% \) and \ (/ \) to reduce the amount of codes

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 200010;
int n, m, a[N], cnt = 0, tot = 0;
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++){
        scanf("%d", a + i);
        cnt = (tot + a[i]) / m;
        tot = (tot + a[i]) % m;
        printf("%d ", cnt);
    } 
}

B - Segment Occurrences

Pretreatment \ (A, B \) of \ (the Hash \) table, the time complexity can be reduced to \ (O (qn) \)

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef unsigned long long ULL;
const int N = 1010, B = 221;
int n, m, q, len;
char s[N], t[N]; 
ULL P[N], S[2][N];
ULL inline get(int l, int r, int c){
    return S[c][r] - S[c][l - 1] * P[r - l + 1];
}
int main(){
    scanf("%d%d%d%s%s", &n, &m, &q, s + 1, t + 1);
    P[0] = 1;
    for(int i = 1; i <= n; i++){
        P[i] = P[i - 1] * B;
        S[0][i] = S[0][i - 1] * B + s[i];
        S[1][i] = S[1][i - 1] * B + t[i];
    }
    while(q--){
        int l, r, res = 0; scanf("%d%d", &l, &r);
        for(int i = l; i <= r - m + 1; i++)
            if(get(i, i + m - 1, 0) == get(1, m, 1))res++;
        printf("%d\n", res);
    }
    return 0;
}

C - Vasya And The Mushrooms

+ Prefix and hard core simulation process. Otherwise repeated around the end \ (2 * N \) lattice, starting from the upper left corner, or \ (S \) shape around and around the circle several times, or around the entire lap.

Can enumerate about \ (S \) number of lattice, \ (S \) is the prefix portion may be calculated and the coefficient is the same, the rear part of the calculation is more complicated.

Example 1

The first case, a front portion around several complete \ (the U-\) . Starting from the top.

Find ( \ (now \) on behalf \ (U \) the number of columns the word end (including) a):

Upper half: $ \ sum_ {i = now + 1} ^ na [0] [i] * (i * 2) $

We found that the number of columns and the suffix and suffix are:

\(sufx[j] = \sum_{i = j} ^ n a[i] * (i - j + 1) = a[j] * 1 + a[j + 1] * 2 + … + a[n] * (n - j + 1)\)

Therefore, the \ (sufx [now + 1] \) plus \ (\ sum_ {J} ^ = I Na [I] * (I * 2 -. 1) \) , that is the answer, this part of the rear extension available and \ (O (1) \) is calculated.

Lower half: $ \ sum_ {i = now + 1} ^ na [1] [i] * (n + (n - i + 1)) $

We want something like:

\(a[i] * 3 + a[i + 1] * 2 + a[i + 2] * 1\)

This looks like \ (Hash \) the principle of the table, but \ (b = 1 \) , you can use the idea hash table \ (O (1) \) to obtain this, and then add the suffix factor either.

case2

For the second case, but will be turned upside down, we can be interchanged approach.

Pretreatment prefixes and suffixes, and enumerated and \ (S \) time are \ (O (n-) \) , each solved simply \ (O (. 1) \) , so that a total of $ complexity is O ( n) $ of

#include <cstdio>
#include <iostream>
#include <cmath>
 
using namespace std;
typedef long long LL;
const int N = 300010;
//s形的预处理
int n, a[2][N];
LL pre[2][N], s[2][N], suf[2][N], sum[N], prex[2][N], sufx[2][N], ans = -1;
 
 
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[0][i]);
    for(int i = 1; i <= n; i++) scanf("%d", &a[1][i]);
    int S = n >> 1, tot = 0;
    for(int i = 1; i <= S * 2; i+=2){
        s[0][i] = (LL)a[0][i] * (tot++); s[1][i] = (LL)a[1][i] * (tot++);
        s[1][i + 1] = (LL)a[1][i + 1] * (tot++); s[0][i + 1] = (LL)a[0][i + 1] * (tot++); 
    }

   
    for(int i = 0; i < 2; i++)
        for(int j = 1; j <= n; j++){
            pre[i][j] = pre[i][j - 1] + a[i][j];
            prex[i][j] = prex[i][j - 1] + pre[i][j];     
        }
    
    for(int j = 0; j < 2; j++)
        for(int i = n; i >= 1; i--){
            suf[j][i] = suf[j][i + 1] + a[j][i]; 
            sufx[j][i] = sufx[j][i + 1] + suf[j][i]; 
        }
 
    for(int i = 0; i <= n; i++){
        LL tot = sum[i] = sum[i - 1] + s[0][i] + s[1][i];
        //如果是奇数,则在下面出发
        if(i % 2){
            tot += (n + i - 1) * suf[0][i + 1] + (prex[0][n] - prex[0][i] - (n - i) * pre[0][i]);
            tot += (i * 2 - 1) * suf[1][i + 1]  + (sufx[1][i + 1]);
        }else{
            //从上面出发
            tot += (i * 2 - 1) * suf[0][i + 1] + (sufx[0][i + 1]);
            tot += (n + i - 1) * suf[1][i + 1] + (prex[1][n] - prex[1][i] - (n - i) * pre[1][i]);
        }
        ans = max(ans, tot);
    }

    printf("%lld", ans);
    return 0;
}

D - Vasya And The Matrix

Reference problem solution. Existence of the nature of XOR reference, \ (X \) \ (XOR \) $ $ X = 0.

Consider constructing a rational sequence, in addition to just the last line, the last one of all the number of add \ (0 \) .

In addition to the lower right corner of the other data copy number, the lower right corner with a different or can attempt.

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 110;
int n, m, a[N], b[N], ans = 0;
int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%d", a + i), ans ^= a[i];
    for(int i = 1; i <= m; i++) scanf("%d", b + i), ans ^= b[i];

    if(ans != 0)puts("NO");
    else{
        puts("YES");
        ans = b[m];
        for(int i = 1; i < n; i++){
            for(int j = 1; j < m; j++)
                printf("0 ");
            printf("%d\n", a[i]);
            ans ^= a[i];
        }
        b[m] = ans;
        for(int i = 1; i <= m; i++) printf("%d ", b[i]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/dmoransky/p/11247607.html