【CQ18高一暑假前挑战赛3.5】标程

【A:快速幂相关】

#include<bits/stdc++.h>
using namespace std;
int qpow(int a,int x){
    a%=10;int res=1; while(x){
        if(x&1) res=res*a%10;
        a=a*a%10; x>>=1;
    } return res;
}
int main()
{
    int N;
    cin>>N;
    cout<<qpow(N,N);
    return 0;
}

【B:位数相关】

#include<bits/stdc++.h>
using namespace std;
#define ll long long
int qpow(int a,int x){
    int res=1; a%=1000; while(x){
        if(x&1) res=res*a%1000;
        a=a*a%1000; x>>=1;
    }return res;
}
int main()
{
    int T,Case=0; ll N,K;
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld",&N,&K);
        double x=K*log10(N)-(ll)(K*log10(N));
        x=pow(10.0,x);
        printf("Case %d: %d %03d\n",++Case,(int)(100.0*x),qpow(N,K));
    }
    return 0;
}

【C:Nim博弈相关】

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int N,x,ans=0;
    scanf("%d",&N);
    while(N--){
        scanf("%d",&x);
        ans^=x;
    }
    if(!ans) puts("B");
    else puts("A");
    return 0;
}

【D:分治相关】

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int Mod=1e9+7;
ll qpow2(ll a,ll x){
    ll res=1; while(x){
        if(x&1) res=res*a%Mod;
        a=a*a%Mod;x>>=1;
    }
    return res;
}
ll qpow(ll N)
{
    if(N==1) return 3;
    ll res=0;
    if(N&1){ res=qpow2(3,N); N--; }
    ll tmp=qpow(N/2); 
    return (tmp+tmp*qpow2(3,N/2)%Mod+res)%Mod;    
}
int main()
{
    ll N,ans;
    scanf("%lld",&N);
    ans=(1+qpow(N))%Mod;
    printf("%lld\n",ans);
    return 0;
}

【E:组合数相关】Lucas+中国剩余定理

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=100010;
LL fac[maxn],mod[maxn],odd[maxn],M,Mod;
void factorial()
{
    fac[0]=1;  for(int i=1;i<=Mod;i++) fac[i]=fac[i-1]*i%Mod;
}
LL f_pow(LL a,LL x)
{
    LL res=1; a%=Mod;
    while(x){  if(x&1) res=res*a%Mod;a=a*a%Mod; x>>=1; }return res;
}
LL C(LL n,LL m)
{
    if(m>n) return 0; return fac[n]*f_pow(fac[m]*fac[n-m]%Mod,Mod-2)%Mod;
}
LL Lucas(LL n,LL m)
{
    if(m==0) return 1;  return C(n%Mod,m%Mod)*Lucas(n/Mod,m/Mod)%Mod; 
}
LL mul(LL x,LL y,LL p)
{
    LL res=0;
    while(y){
        if(y&1) res=(res+x)%p;y>>=1;x=(x+x)%p;
    }return res%p;
}
void China(int k)
{
    LL ans=0;
    for(int i=1;i<=k;i++){
        Mod=mod[i];
        ans=ans+mul(mul(M/mod[i],f_pow(M/mod[i],mod[i]-2),M),odd[i],M);
        //ans=ans+M/mod[i]*f_pow(M/mod[i],mod[i]-2)*odd[i]%M;
    }printf("%lld\n",(ans+M)%M);
}
int main()
{    
    LL T,n,m,k;
    scanf("%lld",&T);
    while(T--){
        M=1;
        scanf("%lld%lld%lld",&n,&m,&k);
        for(int i=1;i<=k;i++){
            scanf("%d",&mod[i]);Mod=mod[i];M*=mod[i];
            factorial();
            odd[i]=Lucas(n,m)%Mod;
        }
        China(k);
    }return 0;
}

猜你喜欢

转载自www.cnblogs.com/hua-dong/p/9141870.html