题目链接

A.牛妹的游戏

题意:



题解:








AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int g[10][10];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        if(n>5){
            cout<<"yes"<<endl;
            for(int i=0;i<m;i++){int u,v;cin>>u>>v;}
            continue;
        }
        memset(g,0,sizeof g);
        for(int i=0;i<m;i++){
            int u,v;
            cin>>u>>v;
            g[u][v]=g[v][u]=1;
        }
        bool f=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                for(int k=1;k<=n;k++){
                    if(i==j||j==k||i==k)continue;
                    if(g[i][j]==g[i][k]&&g[i][k]==g[j][k]&&g[j][k]==g[i][j])f=1;
                }
        if(f)cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }


    return 0;
}


B.病毒扩散

题意:



题解:







AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

ll fact[maxn],inv1[maxn];
ll Pow(ll a, ll b){
    ll ans = 1;
    while(b > 0){
        if(b & 1){
            ans = ans * a % mod;
        }
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
//逆元
ll inv(ll b){
    return Pow(b,mod-2)%mod;
}



ll C(ll n,ll m){
    if(m==0||m==n) return 1;
    ll res=(fact[n]*inv1[m]%mod*inv1[n-m])%mod;
    return res;
}

void init() {
    fact[0] = 1;
    for (int i = 1; i < maxn; i++) {
        fact[i] = fact[i - 1] * i %mod;
    }
    inv1[maxn - 1] = Pow(fact[maxn - 1], mod - 2);
    for (int i = maxn - 2; i >= 0; i--) {
        inv1[i] = inv1[i + 1] * (i + 1) %mod;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;
    cin>>t;
    init();
    while(t--){
        ll x,y,t;
        cin>>x>>y>>t;
        if(x+y>t)cout<<0<<endl;
        else cout<<(C(t,x)*C(t-x,y))%mod<<endl;
    }


    return 0;
}


C.牛牛染颜色

题意:




题解:








AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

struct edge{
    int v,nx;
}e[maxn<<1];
int head[maxn];
int cnt;
void add(int u,int v){
    e[++cnt].v=v;
    e[cnt].nx=head[u];
    head[u]=cnt;
}
ll dp[maxn][2];
void dfs(int u,int fa){
    dp[u][0]=dp[u][1]=1;
    for(int i=head[u];i;i=e[i].nx){
        int v=e[i].v;
        if(v==fa)continue;
        dfs(v,u);
        dp[u][0]=(dp[u][0]+dp[v][1]+dp[v][0]-1)%mod;
        dp[u][1]=dp[u][1]*(dp[v][1]+dp[v][0])%mod;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<n;i++){
        int u,v;
        cin>>u>>v;
        add(u,v);add(v,u);
    }
    dfs(1,0);
    cout<<(dp[1][0]+dp[1][1])%mod<<endl;
    return 0;
}


D. 牛牛的呱数

题意:




题解:






AC代码

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int a[110];
int dis[210][210];
int len[110];
int b[maxn];
struct node{
    int a,b,len;
};

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,p;
    cin>>n>>p;
    b[0]=1;
    for(int i=1;i<maxn;i++)b[i]=b[i-1]*10%p;
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        len[i]=s.length();
        for(int j=0;j<len[i];j++)
            a[i]=(a[i]*10+s[j]-'0')%p;
    }
    int ans=inf;
    memset(dis,inf,sizeof dis);
    queue<node> q;
    for(int i=1;i<=n;i++){
        int da=a[i];
        int db=b[len[i]];
        int dlen=len[i];
        if(dis[da][db]>dlen){
            dis[da][db]=dlen;
            q.push((node){da,db,dlen});
        }
    }
    while(!q.empty()){
        node x=q.front();
        q.pop();
        if(x.len>dis[x.a][x.b])continue;
        if(!x.a)ans=min(ans,x.len);
        for(int i=1;i<=n;i++){
            int da=(x.a+a[i]*x.b)%p;
            int db=x.b*b[len[i]]%p;
            int dlen=x.len+len[i];
            if(dlen<dis[da][db]){
                dis[da][db]=dlen;
                q.push((node){da,db,dlen});
            }
        }
    }
    if(ans>=inf)cout<<-1;
    else cout<<ans;
    return 0;
}