哈尔滨理工大学软件学院ACM程序设计全国邀请赛(网络同步赛)

本文探讨了使用动态规划解决硬币组合问题的方法,并介绍了几种字符串处理算法,包括计算特定字符序列出现次数及矩阵运算等。这些算法适用于解决计算机科学中的实际问题。

A

B

C

//题目给出n,表示n个硬币,能够凑成所有0~m的数,并且这n个硬币的和是m
//那么定义撞塌dp[i][j][k],i代表硬币数,j代表能表示1~j的数,k表示硬币中最大的数
#include<iostream>
using namespace std;
typedef long long LL;
const LL mod=2*1e9+7;
const int maxn=200+5;
LL f[maxn][maxn][maxn];
int main()
{
	f[1][1][1]=1;
	for(int i=1;i<=200;i++){
		for(int j=1;j<=200;j++){
			for(int k=1;k<=200;k++){
				if(!f[i][j][k]) continue;
				for(int add=k;add+j<=200&&add<j+2;add++){
					f[i+1][j+add][add]+=f[i][j][k];
					f[i+1][j+add][add]%=mod;
				}
			}
		}
	}
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		LL result=0;
		for(int i=1;i<=m;i++)
			result=(result+f[n][m][i])%mod;
		cout<<result<<endl;
	}
	return 0;
}	


D

E

#include<iostream>
using namespace std;
const int maxn=200000+5;
char s[maxn];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		scanf("%s",s);
		int len=0;
		long long count=0;
		for(int i=0;i<n;i++){
			if(s[i]=='6') len++;
			else{
				count+=(long long)(len+1)*len/2;
				len=0;
			}
		}
		if(len) count+=(long long)(len+1)*len/2;
		printf("%lld\n",count);
	}
	return 0;
}

F

G

H

#include<iostream>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
struct Matrix{
	int m[2][2];
}base,ans;
Matrix multi(Matrix a,Matrix b)
{
	Matrix tmp;
	for(int i=0;i<2;i++){
		for(int j=0;j<2;j++){
			tmp.m[i][j]=0;
			for(int k=0;k<2;k++){
				tmp.m[i][j]=(tmp.m[i][j]+1LL*a.m[i][k]*b.m[k][j])%mod;
			}
		}
	}
	return tmp;
}
LL Fibonacci(LL n)
{
	ans.m[0][0]=ans.m[1][1]=1;ans.m[0][1]=ans.m[1][0]=0;
	base.m[0][0]=base.m[0][1]=base.m[1][0]=1;base.m[1][1]=0;
	while(n)
	{
		if(n&1)
			ans=multi(ans,base);
		base=multi(base,base);
		n>>=1;
	}
	return ans.m[0][0];
}
int main()
{
	LL n;
	while(scanf("%lld",&n)&&n){
		printf("%lld\n",Fibonacci(n));
	}
	return 0;
}


I

#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
LL num[maxn];
int main()
{
	int n;
	LL k;
	while(~scanf("%d%lld",&n,&k))
	{
		for(int i=0;i<n;i++) scanf("%lld",&num[i]);
		sort(num,num+n);
		int pos=0,count=0;;
		for(int i=0;i<n;i++)
		{
			LL tmp=num[i]+k;
			while(num[pos]<tmp&&pos<n) pos++;
			if(num[pos]==tmp){
				int pos1=pos;
				while(num[pos1]==tmp&&pos<n) {pos1++;count++;}
			}
		}
		printf("%d\n",count);
	}
	return 0;
}


J

#include<iostream>
using namespace std;
int main()
{
	int n;
	while(~scanf("%d",&n))
	{
		int x,count=0;
		for(int i=0;i<n;i++) {scanf("%d",&x);if(x%2==1) count++;}
		printf("%d\n",count);
	}
	return 0;
}

K

#include<iostream>
using namespace std;
const int mod=1e9+7;
int pow_mod(int a,int b)
{
	int result=1;
	int base=a;
	while(b)
	{
		if(b&1) result=(1LL*result%mod*base%mod)%mod;
		base=(1LL*base*base%mod%mod)%mod;
		b>>=1;
	}
	return result;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n,m;
		cin>>n>>m;
		cout<<pow_mod(m,n)<<endl;
	}
	return 0;
}


L


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值