zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 83 (Rated for Div. 2)

    A. Two Regular Polygons

    思路: 在一个正n边形的顶点连线,构造出一个正m边形是否可能。

    #include<iostream>
    #include<string.h>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<string>
    #include<set>
    #include<map>
    using namespace std;
    typedef pair<int,int> PII;
    typedef long long LL;
    using namespace std;
    int main(){
        int T;
        cin>>T;
        while(T--){
            int n,m;
            cin>>n>>m;
            if(n%m==0) cout<<"YES
    ";
            else cout<<"NO
    ";
        }
        return 0;
    }
    

    B. Bogosort

    You are given an array a1,a2,…,an. Array is good if for each pair of indexes i<j the condition j−aj≠i−ai holds. Can you shuffle this array so that it becomes good? To shuffle an array means to reorder its elements arbitrarily (leaving the initial order is also an option).
    For example, if a=[1,1,3,5], then shuffled arrays [1,3,5,1], [3,5,1,1] and [5,3,1,1] are good, but shuffled arrays [3,1,5,1], [1,1,3,5] and [1,1,5,3] aren't.
    It's guaranteed that it's always possible to shuffle an array to meet this condition.
    Input
    The first line contains one integer t (1≤t≤100) — the number of test cases.
    The first line of each test case contains one integer n (1≤n≤100) — the length of array a.
    The second line of each test case contains n integers a1,a2,…,an (1≤ai≤100).
    Output
    For each test case print the shuffled version of the array a which is good.
    Example
    input
    3
    1
    7
    4
    1 1 3 5
    6
    3 2 1 5 6 4
    output
    7
    1 5 1 3
    2 4 6 1 3 5

    思路: 降序排序输出就可以了。考虑将问题转化为(a_1,a_2,...,a_n)加上(n,n-1,...,1)后不相等,那么(a_1,a_2,...,a_n)是降序的话一定不会有相等的两个数了。

    #include<iostream>
    #include<string.h>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<string>
    #include<set>
    #include<map>
    using namespace std;
    int a[110];
    int main(){
        int T;
        cin>>T;
        while(T--){
            int n;
            cin>>n;
            for(int i=1;i<=n;++i)
                cin>>a[i];
            sort(a+1,a+1+n);
           for(int i=n;i>=1;--i)
                cout<<a[i]<<" ";
            cout<<endl;
        }
        return 0;
    }
    

    [C. Adding Powers]*(https://codeforces.com/contest/1312/problem/C)

    Suppose you are performing the following algorithm. There is an array v1,v2,…,vn filled with zeroes at start. The following operation is applied to the array several times — at i-th step (0-indexed) you can:
    either choose position pos (1≤pos≤n) and increase vpos by ki;
    or not choose any position and skip this step.
    You can choose how the algorithm would behave on each step and when to stop it. The question is: can you make array v equal to the given array a (vj=aj for each j) after some step?
    Input
    The first line contains one integer T (1≤T≤1000) — the number of test cases. Next 2T lines contain test cases — two lines per test case.
    The first line of each test case contains two integers n and k (1≤n≤30, 2≤k≤100) — the size of arrays v and a and value k used in the algorithm.
    The second line contains n integers a1,a2,…,an (0≤ai≤1016) — the array you'd like to achieve.
    Output
    For each test case print YES (case insensitive) if you can achieve the array a after some step or NO (case insensitive) otherwise.
    Example
    input
    5
    4 100
    0 0 0 0
    1 2
    1
    3 4
    1 4 1
    3 2
    0 1 3
    3 9
    0 59049 810
    output
    YES
    YES
    NO
    NO
    YES

    思路: 将每个数变成v进制,那么(a=t_1*v^i+t_1*v^j...)就可以唯一确定了,任意一个数都可以被转化为唯一的某一进制数,如果有超过两个数同时包含某个(v^i)则无解。
    其实直接进制转换就好了,不知道为啥一开始想了这个奇葩的方法还过了hhh。

    #include<iostream>
    #include<string.h>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<string>
    #include<set>
    #include<map>
    using namespace std;
    typedef pair<int,int> PII;
    typedef long long LL;
    const int N=31;
    LL a[N];
    LL b[N*3],vis[N*3];
    int main(){
        int T;
        cin>>T;
        while(T--){
            int n;LL k;
            cin>>n>>k;
            for(int i=1;i<=n;++i){
                cin>>a[i];
            }
            int cnt=0;
            LL tk=1;
            while(tk<=1e16){
                b[++cnt]=tk;
                vis[cnt]=0;
                tk*=k;
            }
            sort(a+1,a+1+n);
            for(int i=1;i<=n;++i){
                for(int j=cnt;j>=1;--j){
                    if(vis[j]) continue;
                    if((a[i]-b[j])>=0&&(a[i]-b[j])%k==0){
                        a[i]-=b[j];
                        vis[j]=1;
                    }
                    if(a[i]==0) break;
                }
            }
            for(int i=1;i<=n;++i){
                for(int j=cnt;j>=1;--j){
                    if(vis[j]) continue;
                    if((a[i]-b[j])>=0&&(a[i]-b[j])%k==0){
                        a[i]-=b[j];
                        vis[j]=1;
                    }
                    if(a[i]==0) break;
                }
            }
            bool f=1;
            for(int i=1;i<=n;++i)
                if(a[i]) f=0;
            if(f) cout<<"YES
    ";
            else cout<<"NO
    ";
        }
        return 0;
    }
    

    枚举进制:

    #include<iostream>
    #include<string.h>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    typedef pair<int,int> PII;
    typedef long long LL;
    const int N=31;
    int vis[N*3];
    int main(){
        int T;
        cin>>T;
        while(T--){
            int n;LL k,a;
            cin>>n>>k;
            bool f=1;
            memset(vis,0,sizeof vis);
            for(int i=1;i<=n;++i){
                cin>>a;
                int flag=0;
                while(a&&f==1){
                    int t=a%k;
                    a/=k;
                    vis[flag]+=t;
                    if(vis[flag]>1) {
                        f=0;
                    }
                    flag++;
                }
            }
            if(f) cout<<"YES
    ";
            else cout<<"NO
    ";
        }
        return 0;
    }
    

    D. Count the Arrays

    Suppose you are performing the following algorithm. There is an array v1,v2,…,vn filled with zeroes at start. The following operation is applied to the array several times — at i-th step (0-indexed) you can:
    either choose position pos (1≤pos≤n) and increase vpos by ki;
    or not choose any position and skip this step.
    You can choose how the algorithm would behave on each step and when to stop it. The question is: can you make array v equal to the given array a (vj=aj for each j) after some step?
    Input
    The first line contains one integer T (1≤T≤1000) — the number of test cases. Next 2T lines contain test cases — two lines per test case.
    The first line of each test case contains two integers n and k (1≤n≤30, 2≤k≤100) — the size of arrays v and a and value k used in the algorithm.
    The second line contains n integers a1,a2,…,an (0≤ai≤1016) — the array you'd like to achieve.
    Output
    For each test case print YES (case insensitive) if you can achieve the array a after some step or NO (case insensitive) otherwise.
    Example
    input
    5
    4 100
    0 0 0 0
    1 2
    1
    3 4
    1 4 1
    3 2
    0 1 3
    3 9
    0 59049 810
    output
    YES
    YES
    NO
    NO
    YES
    Note
    In the first test case, you can stop the algorithm before the 0-th step, or don't choose any position several times and stop the algorithm.
    In the second test case, you can add k0 to v1 and stop the algorithm.
    In the third test case, you can't make two 1 in the array v.
    In the fifth test case, you can skip 90 and 91, then add 92 and 93 to v3, skip 94 and finally, add 95 to v2.

    思路: 构造一个数列满足几个要求:
    1.长度为n,只包含1~m中的数
    2.有且仅有两个数相等
    3.存在一个数(a_i)大于左右两边左边严格降序,右边严格降序
    求可以构造出多少满足条件的数列
    首先要在m个数中选择n-1个数构造数列C(n-1,m),最大的数就是i,再在n-2个数中的选择一个作为相等数C(1,n-2),除了相等的两个数和最大数,其他数n-3个数可以枚举在左边和右边(2)(n-3),因为有确定的排序方式所以每种枚举可能都是1。

    #include<bits/stdc++.h>
    using namespace std;
    
    typedef long long LL;
    const int mod=998244353;
    const int maxn=2e5+10;
    LL fac[maxn];
    LL inv[maxn];
    inline LL C(LL m,LL n){
        if(m>n)
            return -1;
        return fac[n]*inv[m]%mod*inv[n-m]%mod;
    }
    inline LL quick_mod(LL a,LL m){
        LL tmp=a%mod;
        LL ans=1;
        while(m){
            if(m&1)
                ans=ans*tmp%mod;
            tmp=tmp*tmp%mod;
            m/=2;
        }
        return ans;
    }
    
    void init(){
        fac[0]=1;
        for(int i=1; i<maxn; i++)
            fac[i]=(fac[i-1]*i)%mod;
        inv[maxn-1]=quick_mod(fac[maxn-1],mod-2);
        for(int i=maxn-2; i>=0; i--)
            inv[i]=(inv[i+1]*(i+1))%mod;
    }
    int main(){
        LL n,m;
        init();
        cin>>n>>m;
        cout<<C(n-1,m)*(n-2)%mod*quick_mod(2,n-3)%mod;
        return 0;
    }
    
  • 相关阅读:
    node入门(一)——安装
    移动web开发基础(二)——viewport
    移动web开发基础(一)——像素
    关于min-height:100%的解决办法
    用类与原型写一个组件(三)——学习笔记
    用类与原型写一个组件(二)——学习笔记
    用类与原型写一个组件(一)——学习笔记
    js类、原型——学习笔记
    Android 常用RGB值及名称
    AES加密示例
  • 原文地址:https://www.cnblogs.com/jjl0229/p/12549109.html
Copyright © 2011-2022 走看看