Codeforces Round #630 (Div. 2)【ABCDE】(题解)

table of Contents

Covering knowledge: thinking, structure.

Game links: Portal

A - Exercising Walk

The meaning of problems: a given area of a coordinate range and the initial point, are required \ (a, b, c, d \) times move vertically and horizontally, can meet the requirements asked.
Solution: in order to offset sentenced borders, the status of the death sentence special folder.
Accept Code:

#include <bits/stdc++.h>
using namespace std;

int main(){
    int t;
    cin>>t;
    while(t--){
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        int x,y,x1,y1,x2,y2;
        cin>>x>>y>>x1>>y1>>x2>>y2;
        if(a-b>x-x1||b-a>x2-x||c-d>y-y1||d-c>y2-y||((a+b)&&(x1==x2))||((c+d)&&(y1==y2)))
            puts("No");
        else puts("Yes");
    }
    return 0;
}

B - Composite Coloring

Meaning of the questions: with no more than 11 colors to fit the number of array dyed the same color to ensure that the number is not relatively prime.
Solution: within the range of the array 1000, and the number of total mass less than 11 100. Factorization assign colors based on the minimum prime factor.
Accept Code:

#include <bits/stdc++.h>
using namespace std;
int res[1010];
map<int,int> mp;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int cnt=0;
        mp.clear();
        for(int i=0,a;i<n;i++){
            cin>>a;
            for(int j=2;j<=sqrt(a);j++){
                if(a%j==0){
                    if(mp[j])res[i]=mp[j];
                    else res[i]=mp[j]=++cnt;
                    break;
                }
            }
        }
        cout<<cnt<<"\n";
        for(int i=0;i<n;i++)
            cout<<res[i]<<" ";
        cout<<"\n";
    }
    return 0;
}

C - K-Complete Word

Meaning of the questions: ask to change a few letters of the string is a palindrome with a period \ (k \) .
Solution: Only \ (n \ div k \) length of \ (K \) a palindromic sequence satisfy a mosaic effect. So us one pair, adding the largest number of contributions to the final answer for each select a position to meet the conditions. See the specific implementation code.
Accept Code:

#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int cnt[maxn][26];
int calc(int u,int v){
    int res=0,mx=0;
    for(int i=0;i<26;i++){
        res+=cnt[u][i]+cnt[v][i];
        mx=max(mx,cnt[u][i]+cnt[v][i]);
    }
    return res-mx;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        //memset(cnt,0,sizeof cnt);
        int n,k;
        string s;
        cin>>n>>k>>s;
        for(int i=0;i<k;i++){
            for(int j=0;j<26;j++)cnt[i][j]=0;
        }
        for(int i=0;i<s.length();i++)cnt[i%k][s[i]-'a']++;
        int ans=0;
        for(int i=0;i<k;i++)ans+=calc(i,k-i-1);
        cout<<ans/2<<"\n";
    }
    return 0;
}

D - Walk on Matrix

Meaning of the questions: construct a matrix, according to the subject requirements given algorithm calculated the maximum difference between the real and final answer to \ (k \) .
answer:

\[\left( \begin{array}{ccc} 2^{17}+k & 2^{17} & 0\\ k & 2^{17}+k & k\\ \end{array} \right) \]

Accept Code:

#include <bits/stdc++.h>
using namespace std;
int main(){
    int k;
    cin>>k;
    cout<<"2 3\n";
    int x=(1<<17);
    cout<<(x^k)<<" "<<x<<" 0\n";
    cout<<k<<" "<<(x^k)<<" "<<k<<"\n";
    return 0;
}

E - Height All the Same

The meaning of problems: a \ (N × M \) each grid on a grid placed FIG initial height \ ([L, R] \ ) cube block range. Every time you can select a trellis diagram in this position plus two cubes of wood, or two adjacent positions to select each of the block plus one, ask several initial state can pass through a limited number of operations are all wood location highly consistent.
Solution: Because no matter how operations are adding to an existing stack the box 2 box, so the parity unchanged. We assume that an initial maximum height of a pile of wood \ (H \) , if the final height position can be made consistent with all of \ (K \) , it is necessary to satisfy: \ ((n-m * + K * (n-m * H * - \ {I SUM cnt_, J})) \% 2 = 0 \) , where \ (cnt_ {i, j} \) refers to the trellis diagram position \ ((i, j) \ ) number of squares .
If not difficult to find \ (n * m \) is odd, then the above equation must be able to set up, the program number \ ((L-R & lt +. 1) ^ {nm} \) ; if \ (n * m \) is an even number, depending on whether the same height becomes \ (n * m * h- \ sum pos_ {i, j} \) parity, and because the \ (n * m \) is an even number, so the initial the total number of squares will surely odd if not, we subtract the initial block number is an odd number is the answer.
Accept Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
ll qpow(ll a, ll b, ll p){
    ll ans = 1;
    while (b){
        if (b & 1)
            ans = ans * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return ans;
}
int main(){
    ll n, m, l, r;
    cin>>n>>m>>l>>r;
    if ((n & 1) && (m & 1))
        printf("%lld\n", qpow((r - l + 1), n * m, mod));
    else{
        ll even = r / 2ll - (l - 1ll) / 2ll, odd = r - l + 1ll - even;
        ll ans = (qpow(r - l + 1, m * n, mod) - qpow(odd - even, m * n, mod) + mod) % mod;
        ans = (ans * qpow(2ll, mod - 2ll, mod)) % mod;
        ans = (qpow(r - l + 1, n * m, mod) - ans + mod) % mod;
        printf("%lld\n", ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/charles1999/p/12612071.html