Week14 - 程序设计思维与实践 - 矩阵快速幂(+优化DP)

题目连接

A - Q老师与石头剪刀布(必做)

题意

每一个大人曾经都是一个小孩,Q老师 也一样。
为了回忆童年,Q老师 和 Monika 玩起了石头剪刀布的游戏,游戏一共 n 轮。无所不知的 Q老师 知道每一轮 Monika 的出招,然而作为限制, Q老师 在这 n 轮游戏中必须恰好出 a 次石头,b 次布和 c 次剪刀。
如果 Q老师 赢了 Monika n/2(上取整) 次,那么 Q老师就赢得了这场游戏,否则 Q老师 就输啦!
Q老师非常想赢,他想知道能否可以赢得这场游戏,如果可以的话,Q老师希望你能告诉他一种可以赢的出招顺序,任意一种都可以。

Input

第一行一个整数 t(1 ≤ t ≤ 100)表示测试数据组数。然后接下来的 t 组数据,每一组都有三个整数:
第一行一个整数 n(1 ≤ n ≤ 100)
第二行包含三个整数 a, b, c(0 ≤ a, b, c ≤ n)。保证 a+b+c=n
第三行包含一个长度为 n 的字符串 s,字符串 s 由且仅由 ‘R’, ‘P’, ‘S’ 这三个字母组成。第 i 个字母 s[i] 表示 Monika 在第 i 轮的出招。字母 ‘R’ 表示石头,字母 ‘P’ 表示布,字母 ‘S’ 表示剪刀

Output

对于每组数据:
如果 Q老师 不能赢,则在第一行输出 “NO”(不含引号)
否则在第一行输出 “YES”(不含引号),在第二行输出 Q老师 的出招序列 t。要求 t 的长度为 n 且仅由 ‘R’, ‘P’, ‘S’ 这三个字母构成。t 中需要正好包含 a 个 ‘R’,b 个 ‘P’ 和 c 个 ‘S’
“YES”/"NO"是大小写不敏感的,但是 ‘R’, ‘P’, ‘S’ 是大小写敏感的。

Sample Input

2
3
1 1 1
RPS
3
3 0 0
RPS

Sample Output

YES
PSR
NO

思路

由于题目要求在这 n 轮游戏中必须恰好出 a 次石头,b 次布和 c 次剪刀,同时还要保证尽可能最多胜场,
所以我们优先处理能 Q 老师能赢的情况 ,最后再把剩下的 a、b、c 分配给平局/输局即可。

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;

int main()
{
	int t,n,a,b,c;
	char m[110],ans[110];
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			ans[i]='0';
		scanf("%d%d%d",&a,&b,&c);
		scanf("%s",m+1);
		int cnt=0;
		for(int i=1;i<=n;i++){
			if(m[i]=='R'&&b>0){
				b--;cnt++;ans[i]='P';
			}
			else if(m[i]=='P'&&c>0){
				c--;cnt++;ans[i]='S';	
			}
			else if(m[i]=='S'&&a>0){
				a--;cnt++;ans[i]='R';
			}
		}
		for(int i=1;i<=n;i++){
			if(ans[i]=='0'){
				if(a>0){
					ans[i]='R';a--;
				}
				else if(b>0){
					ans[i]='P';b--;
				}
				else if(c>0){
					ans[i]='S';c--;
				}
			}
		}
		if(cnt>=ceil((float)n/2.0)){
			printf("YES\n");
			for(int i=1;i<=n;i++)
				printf("%c",ans[i]);
			printf("\n");
		}
		else{
			printf("NO\n");
		}
	}
	return 0;
}

B - Q老师与十字叉(必做)

题意

Q老师 得到一张 n 行 m 列的网格图,上面每一个格子要么是白色的要么是黑色的。
Q老师认为失去了 十字叉 的网格图莫得灵魂。一个十字叉可以用一个数对 x 和 y 来表示, 其中 1 ≤ x ≤ n 并且 1 ≤ y ≤ m, 满足在第 x 行中的所有格子以及在第 y 列的 所有格子都是黑色的。
Q老师 得到了一桶黑颜料,他想为这个网格图注入灵魂。 Q老师 每分钟可以选择一个白色的格子并且把它涂黑。现在他想知道要完成这个工作,最少需要几分钟?

Input

第一行包含一个整数 q (1 ≤ q ≤ 5 * 10^4) — 表示测试组数
对于每组数据:
第一行有两个整数 n 和 m (1 ≤ n, m ≤ 5 * 10^4, n * m ≤ 4 * 10^5) — 表示网格图的行数和列数
接下来的 n 行中每一行包含 m 个字符 — ‘.’ 表示这个格子是白色的, '’ 表示这个格子是黑色的
保证 q 组数据中 n 的总和不超过 5 * 10^4, n
m 的总和不超过 4 * 10^5

Output

答案输出 q 行, 第 i 行包含一个整数 — 表示第 i 组数据的答案

Sample Input

9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**

Sample Output

0
0
0
0
0
4
1
1
2

思路

WA了三发了。。。冷静冷静再做。。。
回来补坑!!!
首先开二维数组存每一个元素是肯定会爆内存的
一开始把问题想简单了,只是记录横向与纵向黑色块的最大值然后计算,虽然大多数情况都符合,但有一种情况比如酱紫:
在这里插入图片描述
横向/纵向不是最大值,但两个交叉在一起之后结果就不一样了gg…
所以改为踏踏实实地枚举每一行每一列…

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
using namespace std;
const int maxn=50010;
int q,n,m;
char c[maxn];

struct node{
	int x,y;
	node(int xx,int yy):x(xx),y(yy){}
	bool operator<(const node &s)const{
		if(x!=s.x) return x<s.x;
		else	   return y<s.y;
	}
};

map<node,bool> black;
int a[maxn],b[maxn];

int main()
{
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);
	scanf("%d",&q);
	while(q--){
		scanf("%d%d",&n,&m);
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		for(int i=1;i<=n;i++){
			scanf("%s",c+1);
			for(int j=1;j<=m;j++){
				if(c[j]=='*'){
					black[node(i,j)]=1;
					a[i]++;b[j]++;
				}
			}
		}
		int ans=1e9;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				int tmp=black[node(i,j)]?a[i]+b[j]-1:a[i]+b[j];
				ans=min(ans,n+m-1-tmp);
			}
		}
		black.clear();
		printf("%d\n",ans);
	}
	return 0;	
}

C - Q老师的考验(必做)

题意

Q老师 对数列有一种非同一般的热爱,尤其是优美的斐波那契数列。
这一天,Q老师 为了增强大家对于斐波那契数列的理解,决定在斐波那契的基础上创建一个新的数列 f(x) 来考一考大家。数列 f(x) 定义如下:
当 x < 10 时,f(x) = x;
当 x ≥ 10 时,f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10),ai 只能为 0 或 1。
Q老师 将给定 a0~a9,以及两个正整数 k m,询问 f(k) % m 的数值大小。
聪明的你能通过 Q老师 的考验吗?

Input

输出文件包含多组测试用例,每组测试用例格式如下:
第一行给定两个正整数 k m。(k < 2e9, m < 1e5)
第二行给定十个整数,分别表示 a0~a9。

Output

对于每一组测试用例输出一行,表示 f(k) % m 的数值大小。

Sample Input

10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0

Sample Output

45
104

思路

在这里插入图片描述

代码实现

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll k,mod,a[20];
const int N=10;

struct matrix{
	matrix(){memset(a,0,sizeof(a));}
	matrix(const matrix &s){memcpy(a,s.a,sizeof(a));}
	matrix operator*(const matrix &s){
		matrix t;
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				for(int k=0;k<N;k++){
					t.a[i][j]+=a[i][k]*s.a[k][j]%mod;
					t.a[i][j]%=mod;
				}				
			}	
		}					
		return t;
	}
	ll a[N][N];
};

void solve(matrix x,int n){
	matrix ans;
	for(int i=0;i<10;i++)
		ans.a[i][i]=1;
	while(n){
		if(n&1) ans=ans*x;
		x=x*x;
		n>>=1;
	}
	ll res=0;
	for(int i=0;i<10;i++){
		res+=ans.a[0][i]*(10-i-1)%mod;
	}
	printf("%lld\n",res%mod);
}

int main()
{
	matrix x;
	while(~scanf("%lld%lld",&k,&mod)){
		for(int i=0;i<10;i++)
			scanf("%lld",&a[i]);
		if(k<10){
			printf("%lld\n",k%mod);
			continue;
		}
		memset(x.a,0,sizeof(x.a));
		for(int j=0;j<10;j++)
			x.a[0][j]=a[j];
		for(int i=1;i<10;i++)
			x.a[i][i-1]=1;
		solve(x,k-9);
	}
	return 0;
}

D - Q老师染砖(选做)

题意

衣食无忧的 Q老师 有一天突发奇想,想要去感受一下劳动人民的艰苦生活。
具体工作是这样的,有 N 块砖排成一排染色,每一块砖需要涂上红、蓝、绿、黄这 4 种颜色中的其中 1 种。且当这 N 块砖中红色和绿色的块数均为偶数时,染色效果最佳。
为了使工作效率更高,Q老师 想要知道一共有多少种方案可以使染色效果最佳,你能帮帮他吗?

Input

第一行为 T,代表数据组数。(1 ≤ T ≤ 100)
接下来 T 行每行包括一个数字 N,代表有 N 块砖。(1 ≤ N ≤ 1e9)

Output

输出满足条件的方案数,答案模 10007。

Sample Input

2
1
2

Sample Output

2
6

思路

在这里插入图片描述

代码实现

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mod=10007;
int t,n;
const int N=3;

struct matrix{
	matrix(){memset(a,0,sizeof(a));}
	matrix(const matrix &s){memcpy(a,s.a,sizeof(a));}
	matrix operator*(const matrix &s){
		matrix t;
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				for(int k=0;k<N;k++){
					t.a[i][j]+=a[i][k]*s.a[k][j]%mod;
					t.a[i][j]%=mod;
				}				
			}	
		}					
		return t;
	}
	ll a[N][N];
};

void solve(matrix x,int n){
	matrix ans;
	for(int i=0;i<N;i++)
		ans.a[i][i]=1;
	while(n){
		if(n&1) ans=ans*x;
		x=x*x;
		n>>=1;
	}
	ll res=0;
	res=ans.a[0][0]%mod;
	printf("%lld\n",res);
}

int main()
{
	scanf("%d",&t);
	while(t--){
		scanf("%d",&n);
		matrix x;
		x.a[0][0]=x.a[1][1]=2;
		x.a[0][2]=x.a[1][2]=1;
		for(int i=0;i<N;i++)
			x.a[2][i]=2;
		solve(x,n);
	}
	return 0;
}

E - Q老师度假(选做)

题意

忙碌了一个学期的 Q老师 决定奖励自己 N 天假期。
假期中不同的穿衣方式会有不同的快乐值。
已知 Q老师 一共有 M 件衬衫,且如果昨天穿的是衬衫 A,今天穿的是衬衫 B,则 Q老师 今天可以获得 f[A][B] 快乐值。
在 N 天假期结束后,Q老师 最多可以获得多少快乐值?

Input

输入文件包含多组测试样例,每组测试样例格式描述如下:
第一行给出两个整数 N M,分别代表假期长度与 Q老师 的衬衫总数。(2 ≤ N ≤ 100000, 1 ≤ M ≤ 100)
接下来 M 行,每行给出 M 个整数,其中第 i 行的第 j 个整数,表示 f[i][j]。(1 ≤ f[i][j] ≤ 1000000)
测试样例组数不会超过 10。

Output

每组测试样例输出一行,表示 Q老师 可以获得的最大快乐值。

Sample Input

3 2
0 1
1 0
4 3
1 2 3
1 2 3
1 2 3

Sample Output

2
9

思路

在这里插入图片描述

代码实现

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> 
using namespace std;
typedef long long ll;
ll n,m,f[110][110];
int N;

struct matrix{
	matrix(){memset(a,0,sizeof(a));}
	matrix(const matrix &s){memcpy(a,s.a,sizeof(a));}
	matrix operator*(const matrix &s){
		matrix t;
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				for(int k=0;k<N;k++){
					t.a[i][j]=max(t.a[i][j],a[i][k]+s.a[k][j]);
				}				
			}	
		}					
		return t;
	}
	ll a[110][110];
};

void solve(matrix x,int n){
	matrix ans;	
	while(n){
		if(n&1)	ans=ans*x;	
		x=x*x;
		n>>=1;
	}	
	ll res=0;
	for(int i=0;i<m;i++){
		for(int j=0;j<m;j++){
			res=max(res,ans.a[i][j]);
		}
	}
	printf("%lld\n",res);
}

int main()
{
	while(~scanf("%lld%lld",&n,&m)){
		N=m;
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				scanf("%lld",&f[i][j]);
			}
		}
		matrix x;
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				x.a[i][j]=f[j][i];
			}
		}
		solve(x,n-1); 
	}
	return 0;
}

在这里插入图片描述
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
const int N=3;

struct matrix{
	matrix(){memset(a,0,sizeof(a));}
	matrix(const matrix &s){
		for(int i=0;i<N;i++)
			for(int j=0;j<N;j++)
				a[i][j]=s.a[i][j];
	}
	matrix operator*(const matrix &s){
		matrix t;
		for(int i=0;i<N;i++){
			for(int j=0;j<N;j++){
				for(int k=0;k<N;k++){
					t.a[i][j]+=a[i][k]*s.a[k][j];
				}				
			}	
		}					
		return t;
	}
	long double a[N][N];
};

void solve(matrix x,int n){
	matrix ans;
	for(int i=0;i<N;i++)
		ans.a[i][i]=1.0;
	while(n){
		if(n&1) ans=ans*x;
		x=x*x;
		n>>=1;
	}
	long double res=ans.a[0][0]*(long double)0.75+ans.a[0][1]*(long double)0.5+ans.a[0][2];
	printf("%.10Lf\n",res);
}

int main()
{
	int n;
	cin>>n;
	matrix x;
	x.a[0][0]=x.a[0][2]=0.5;
	x.a[0][1]=0.25;
	x.a[1][0]=x.a[2][2]=1.0;
	if(n==1){
		cout<<0.5<<endl;
		return 0;
	}
	else if(n==2){
		cout<<0.75<<endl;
		return 0;
	}
	solve(x,n-2);
	return 0;
}

舍友直接一个 for 写完了…err…高数知识都还给了 wzm 老师,现在已经忘了收敛是啥了23333

#include <bits/stdc++.h>
using namespace std;
 
int n;
long double ans,f1,f2,f3,lt;
 
int main(){
    cin>>n;
    f1=1.0;f2=1.0;lt=8;ans=0.75;
    if (n==1) 
        cout<<f1/2;
    else if (n==2)
        cout<<0.75;
    else{
	    for (int i=3;i<=n;i++){
	        f3=f1+f2;
	        long double tmp=(long double)1.0*f3/lt;
	        lt*=2;
	        ans+=tmp;
	        f1=f2;
	        f2=f3;
	        if (tmp-0<=1e-12) break;
	    }
	    printf("%.10Lf",ans);   	
	}
 	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值