清北学堂 模拟题1

算 sum

#include
#include
#include
#include
#include
#define LL long long
using namespace std;
const int p=1e9+7;
LL n,m,ans;
LL read(){
	LL x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while('0'<=ch&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
LL quick_pow(LL a,LL b){
	LL ret=1;
	while(b!=0){
		if(b%2) ret=ret*a%p;
		a=a*a%p;
		b/=2;
	}
	return ret%p;
}
LL phi(LL a){
	return quick_pow(a,p-2)%p;
}
int main(){
	freopen("sum.in","r",stdin);
	freopen("sum.out","w",stdout);
	n=read();m=read();
	ans=m%p;
	for(int i=2;i<=n;i++){
		ans=(ans%p+((((i%p)*((quick_pow(i,m)-1)%p))%p)*phi(i-1))%p)%p;
	}
	printf("%lld",ans);
	fclose(stdin);fclose(stdout);
	return 0;
} 

游tour

#include
#include
#include
#include
#include
#define LL long long
using namespace std;
const int N=50020;
int n,sum,hed[N],net[N*2],to[N*2],v[N*2],mx,summ;
int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while('0'<=ch&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
void add(int x,int y,int z){
	sum++;
	to[sum]=y;
	v[sum]=z;
	net[sum]=hed[x];
	hed[x]=sum;
}
void dfs(int x,int fa,int wy){
	mx=max(mx,wy);
	for(int i=hed[x];i;i=net[i]){
		int to1=to[i],w=v[i];
		if(to1!=fa){
			dfs(to1,x,wy+v[i]);
		}
	}
	return;
}
int main(){
	freopen("tour.in","r",stdin);
	freopen("tour.out","w",stdout);
	n=read();
	for(int i=1;i<=n-1;i++){
		int s,t,w;
		s=read();t=read();w=read();
		summ+=w;
		add(s,t,w); add(t,s,w);
	}
	dfs(1,0,0);
	printf("%d",summ*2-mx);
	fclose(stdin);fclose(stdout);
	return 0;
}

运lucky

#include
#include
#include
#include
#include
#define LL long long
using namespace std;
const int p=1e9+7;
const int N=300010;
LL read(){
	LL x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while('0'<=ch&&ch<='9'){
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
LL n,k,a[N],sum1[N],sum2[N],num=0,fl[N],ans,flag,dp[3500][3500],ss;
int judge(LL x){
	int flag47=0;
	while(x!=0){
		if(x%10!=4&&x%10!=7) flag47++;
		x/=10;
	}
	if(flag47!=0) return 1;
	return 0;
}
LL quick_pow(LL x,LL y){
	LL ret=1;
	while(y!=0){
		if(y%2) ret=ret*x%p;
		x=x*x%p;
		y/=2;
	}
	return ret%p;
}
LL phi(LL x){
	return quick_pow(x,p-2)%p;
}
LL C(LL x,LL y){
	return ((sum1[x-y+1]%p*phi(sum1[x+1])%p)%p*phi(sum2[y])%p)%p;
}
int main(){
	freopen("lucky.in","r",stdin);
	freopen("lucky.out","w",stdout);
	n=read();k=read();
	sum1[n+1]=1;
	for(int i=n;i>=1;i--) sum1[i]=sum1[i+1]*i%p;
	sum2[0]=1;
	for(int i=1;i<=n;i++) sum2[i]=sum2[i-1]*i%p;
	for(int i=1;i<=n;i++) a[i]=read();
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++){
    	if(!judge(a[i])){
    		ss++;
    		if(a[i]!=a[i-1]){
    			num++;
   		        fl[num]=1;
    		}
   		    else  fl[num]++;
    	}
    } 
   for(int i=0;i<=num;i++) dp[i][0]=1;
    for(int i=1;i<=num;i++)
	 for(int j=1;j<=i;j++){
	 	  dp[i][j]=(dp[i][j]+dp[i-1][j]+dp[i-1][j-1]%p*fl[i]%p)%p;
	 }	   
	for(int i=0;i<=k;i++){
		if(num>=i&&n-ss>=k-i)
		ans=(ans+dp[num][i]%p*C(n-ss,k-i)%p)%p;
	} 
	printf("%lld",ans);
	fclose(stdin);fclose(stdout);  	
	return 0;
}

 

你可能感兴趣的:(编程)