牛客周赛 Round 60 A-F题解

A

偶数个相同的数异或为0,奇数个相同的数异或为这个数本身

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
#define int long long
typedef tuple<int,int,int> tp;
#define x first
#define y second
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
constexpr int N=1000010,mod=998244353,inf=1e18;
constexpr double pi=3.1415926535897932384626,eps=1e-5;
const ll  P=rnd()%mod;
#define all(a) a.begin(),a.end()
#define get_count(x) __builtin_popcount(x)
#define fors(i,a,b) for(int i=a;i<=b;i++)
#define forr(i,a,b) for(int i=b;i>=a;i--)
#define pb(x) push_back(x)
int dx[]={0,1,0,-1,1,1,-1,-1,0};
int dy[]={1,0,-1,1,1,-1,-1,1,0};
int random(int l,int r){
    return rand()%r+l;
}
signed main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int __=1;
//    cin >> __;
    while(__--){
        int x;
        cin >> x;
        cout << 0 << '\n';
    }
    system("color 04");
    return 0;
}

B

正数P和负数Q交叉来填,如果\(P==Q\)则为\(2*P\),否则为\(min(P,Q)*2+1\)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
#define int long long
typedef tuple<int,int,int> tp;
#define x first
#define y second
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
constexpr int N=1000010,mod=998244353,inf=1e18;
constexpr double pi=3.1415926535897932384626,eps=1e-5;
const ll  P=rnd()%mod;
#define all(a) a.begin(),a.end()
#define get_count(x) __builtin_popcount(x)
#define fors(i,a,b) for(int i=a;i<=b;i++)
#define forr(i,a,b) for(int i=b;i>=a;i--)
#define pb(x) push_back(x)
int dx[]={0,1,0,-1,1,1,-1,-1,0};
int dy[]={1,0,-1,1,1,-1,-1,1,0};
int random(int l,int r){
    return rand()%r+l;
}
signed main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int __=1;
//    cin >> __;
    while(__--){
        int n,m;
        cin >> n >> m;
        if(n==m) cout << 2*n << '\n';
        else  cout << 2*min(n,m)+1 << '\n';
    }
    system("color 04");
    return 0;
}

C

把相同行的列数存在一起,把相同列的行数存在一起,最后查询

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
#define int long long
typedef tuple<int,int,int> tp;
#define x first
#define y second
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
constexpr int N=1000010,mod=998244353,inf=1e18;
constexpr double pi=3.1415926535897932384626,eps=1e-5;
const ll  P=rnd()%mod;
#define all(a) a.begin(),a.end()
#define get_count(x) __builtin_popcount(x)
#define fors(i,a,b) for(int i=a;i<=b;i++)
#define forr(i,a,b) for(int i=b;i>=a;i--)
#define pb(x) push_back(x)
int dx[]={0,1,0,-1,1,1,-1,-1,0};
int dy[]={1,0,-1,1,1,-1,-1,1,0};
int random(int l,int r){
    return rand()%r+l;
}
vector<int> e[N],b[N];
signed main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int __=1;
//    cin >> __;
    while(__--){
        int n,m;
        cin >> n >> m;
        for(int i=1;i<=m;i++){
            int x,y;
            cin >> x >> y;
            e[x].push_back(y);
            b[y].push_back(x);
        }
        int ans=0;
        for(int i=1;i<=n;i++){
            sort(all(e[i])),sort(all(b[i]));
            if(e[i].size()){
                ans=max(ans,e[i].back()-e[i][0]);
            }
            if(b[i].size()){
                ans=max(ans,b[i].back()-b[i][0]);
            }
        }
        cout << ans << '\n';
    }
    system("color 04");
    return 0;
}

D

将数组从小到大排序,sum记为前缀和,如果\(sum>=a_i-1\)说明可以凑出1-ai的所有数,否则不可以,最后判断sum是否大于等于n即可

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
#define int long long
typedef tuple<int,int,int> tp;
#define x first
#define y second
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
constexpr int N=1000010,mod=998244353,inf=1e18;
constexpr double pi=3.1415926535897932384626,eps=1e-5;
const ll  P=rnd()%mod;
#define all(a) a.begin(),a.end()
#define get_count(x) __builtin_popcount(x)
#define fors(i,a,b) for(int i=a;i<=b;i++)
#define forr(i,a,b) for(int i=b;i>=a;i--)
#define pb(x) push_back(x)
int dx[]={0,1,0,-1,1,1,-1,-1,0};
int dy[]={1,0,-1,1,1,-1,-1,1,0};
int random(int l,int r){
    return rand()%r+l;
}
signed main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int __=1;
    cin >> __;
    while(__--){
        int n;
        cin >> n;
        vector<int> a(n+1);
        for(int i=1;i<=n;i++) cin >> a[i];
        sort(all(a));
        int sum=0;
        for(int i=1;i<=n;i++){
            if(sum>=a[i]-1){
                sum+=a[i];
            }
        }
        if(sum>=n) cout << "Cool!" << '\n';
        else cout << sum+1 << '\n';
    }
    system("color 04");
    return 0;
}

E

盒子模型 n-2小球,放m-1个盒子里有多少种方案

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
#define int long long
typedef tuple<int,int,int> tp;
#define x first
#define y second
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
constexpr int N=1000010,mod=1e9+7,inf=1e18;
constexpr double pi=3.1415926535897932384626,eps=1e-5;
const ll  P=rnd()%mod;
#define all(a) a.begin(),a.end()
#define get_count(x) __builtin_popcount(x)
#define fors(i,a,b) for(int i=a;i<=b;i++)
#define forr(i,a,b) for(int i=b;i>=a;i--)
#define pb(x) push_back(x)
int dx[]={0,1,0,-1,1,1,-1,-1,0};
int dy[]={1,0,-1,1,1,-1,-1,1,0};
int random(int l,int r){
    return rand()%r+l;
}
class Com{
public:
    vector<int> f,uf;
    void init(int n){
        f.resize(n+1),uf.resize(n+1);
    }
    int qpow(int a,int b,int mod){
        int res=1;
        while(b){
            if(b&1) res=res*a%mod;
            a=a*a%mod;
            b>>=1;
        }
        return res;
    }
    void get_f(int n,int mod){
        f[0]=uf[0]=1;
        for(int i=1;i<=n;i++){
            f[i]=f[i-1]*i%mod;
            uf[i]=uf[i-1]*qpow(i,mod-2,mod)%mod;
        }
    }
    int get_C(int n,int m,int mod){
        if(n<m) return 0;
        return f[n]*uf[n-m]%mod*uf[m]%mod;
    }
};
signed main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int __=1;
    cin >> __;
    Com C;
    C.init(N);
    C.get_f(N,mod);
    while(__--){
        int n,m;
        cin >> n >> m;
        cout << C.get_C(n-2,m-1,mod) << '\n';
    }
    system("color 04");
    return 0;
}

F

期望DP,推式子,首先,令f(i):从第i个字开始讲完这句话的期望。\(f_1=a_1/(a_1+b_1)*f_2+b_1/(a_1+b_1)*f_1+1\),移项求得\(f_1=1*f_2+(a_1+b_1)/a_1\),令\(P_1=1,Q_1=(a_1+b_1)/(a_1)\),\(f_2=a_2^2/(a_2+b_2)^2*f_3+2*a_2*b_2/(a_2+b_2)^2*f_2+b_2^2/(a_2+b_2)^2*f1\),同理整理得\(f2=a_2^2/(a_2^2+b_2^2-b_2^2*P_1)*f_3+Q_1*b_2^2+(a_2+b_2)^2/(a_2^2+b_2^2-b_2^2*P_1)\),化成一般式,\(f_i=P_i*f_(i+1)+Q_i\)\(P_i=a^2/(a^2+b^2-b^2*P_(i-1))\),\(Q_i=(Q_(i-1)*b^2+(a+b)^2)/(a^2+b^2-b^2*P_(i-1))\)。从而可以预处理掉P和Q,那么\(f_i=p_i*f_(i+1)+q_i\),初始化\(f_n=1\),则倒序枚举最后输出\(f_1\)即为答案。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
mt19937 rnd(time(0));
#define int long long
typedef tuple<int,int,int> tp;
#define x first
#define y second
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
constexpr int N=1000010,mod=1e9+7,inf=1e18;
constexpr double pi=3.1415926535897932384626,eps=1e-5;
const ll  P=rnd()%mod;
#define all(a) a.begin(),a.end()
#define get_count(x) __builtin_popcount(x)
#define fors(i,a,b) for(int i=a;i<=b;i++)
#define forr(i,a,b) for(int i=b;i>=a;i--)
#define pb(x) push_back(x)
int dx[]={0,1,0,-1,1,1,-1,-1,0};
int dy[]={1,0,-1,1,1,-1,-1,1,0};
int random(int l,int r){
    return rand()%r+l;
}
int qpow(int a,int b,int mod){
    int res=1;
    while(b){
        if(b&1) res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}
signed main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    int __=1;
//     cin >> __;
    while(__--){
        int n;
        cin >> n;
        vector<int> a(n),b(n),f(n+1),p(n),q(n);
        for(int i=1;i<n;i++) cin >> a[i];
        for(int i=1;i<n;i++) cin >> b[i];
        p[1]=1,q[1]=(a[1]+b[1])%mod*qpow(a[1],mod-2,mod)%mod;
        for(int i=2;i<n;i++){
            int x=a[i],y=b[i];
            int A=x*x%mod,B=y*y%mod,C=(x+y)%mod*(x+y)%mod;
            int t=((A+B)%mod-B*p[i-1]%mod+mod)%mod;
            p[i]=A*qpow(t,mod-2,mod)%mod;
            q[i]=(q[i-1]*B%mod+C)%mod*qpow(t,mod-2,mod)%mod;
        }
        f[n]=1;
        for(int i=n-1;i>=1;i--){
            f[i]=(f[i+1]*p[i]%mod+q[i])%mod;
        }
        cout << f[1] << '\n';
    }
    system("color 04");
    return 0;
}
posted @ 2024-09-15 23:53  sty_stability  阅读(61)  评论(0)    收藏  举报