Atcoder Grand Contest 037B(DP,组合数学,思维)
#include<bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
string s;
int a[300007];
long long x[7],y[7];
int main(){
int n;
cin>>n;
cin>>s;
int len=s.size();
for(int i=0;i<len;++i){
if(s[i]=='B')
a[i]=1;
else if(s[i]=='G')
a[i]=2;
}
long long ans=1;
for(int i=2;i<=n;++i)
ans=1ll*ans*i%mod;//每三个球可以分配给n~1个人。
for(int i=0;i<len;++i){//遇到第一种颜色的球,记录它的个数,当遇到另一种颜色的时候从第一种颜色的球中任选一个,同时记录第二种颜色的球的个数,当遇到第三种颜色的时候,从第二种颜色的球中任选一个。
//这里第一种颜色的球的个数是用x[a[i]]存的,第二种颜色的球的个数是存在y[(a[i]+1or2)%3]中的,当遇到第三种颜色即(a[i]+1or2)%3时从第二种颜色的球中任选一个
if(y[a[i]])//i前面有y[a[i]]个(a[i]+2)%3颜色的球,遇到i时前面可以从y[a[i]]个中选取一个
ans=ans*y[a[i]]--%mod;
else if(x[(a[i]+1)%3])//和下一个else if等价,另两种颜色地位相同,遇到哪一种都可以组合,三种颜色都是等价的
ans=ans*x[(a[i]+1)%3]--%mod,y[(a[i]+2)%3]++;//前面有x[(a[i]+1)%3]个(a[i]+1)%3颜色的球,遇到i时可以从x[(a[i]+1)%3]个中选取一个
else if(x[(a[i]+2)%3])//和上一个else if等价,另两种颜色地位相同,遇到哪一种都可以组合,三种颜色都是等价的
ans=ans*x[(a[i]+2)%3]--%mod,y[(a[i]+1)%3]++;//前面有x[(a[i]+2)%3]个(a[i]+2)%3颜色的球,遇到i时可以从x[(a[i]+2)%3]个中选取一个
else
x[a[i]]++;//记录当前a[i]颜色的球的数量
}
cout<<ans;
return 0;
}
Atcoder Grand Contest 037B(DP,组合数学,思维)的更多相关文章
- Atcoder Grand Contest 031C(构造,思维,异或,DFS)
#include<bits/stdc++.h>using namespace std;int n,a,b,sum;void dfs(int x,int y,int ban){ if( ...
- Atcoder Grand Contest 024 E - Sequence Growing Hard(dp+思维)
题目传送门 典型的 Atcoder 风格的计数 dp. 题目可以转化为每次在序列中插入一个 \([1,k]\) 的数,共操作 \(n\) 次,满足后一个序列的字典序严格大于前一个序列,问有多少种操作序 ...
- AtCoder Grand Contest 019 B - Reverse and Compare【思维】
AtCoder Grand Contest 019 B - Reverse and Compare 题意:给定字符串,可以选定任意i.j且i<=j(当然i==j时没啥卵用),然后翻转i到j的字符 ...
- AtCoder Grand Contest 009
AtCoder Grand Contest 009 A - Multiple Array 翻译 见洛谷 题解 从后往前考虑. #include<iostream> #include< ...
- AtCoder Grand Contest 008
AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...
- AtCoder Grand Contest 007
AtCoder Grand Contest 007 A - Shik and Stone 翻译 见洛谷 题解 傻逼玩意 #include<cstdio> int n,m,tot;char ...
- AtCoder Grand Contest 005
AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...
- Atcoder Grand Contest 036 D - Negative Cycle
Atcoder Grand Contest 036 D - Negative Cycle 解题思路 在某些情况下,给一张图加或删一些边要使图合法的题目要考虑到最短路的差分约束系统.这一题看似和最短路没 ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
随机推荐
- springboot+mybatis报错Invalid bound statement (not found)
今天做项目时报了一个错提示说Invalid bound statement (not found),也就是说mapper接口绑定.xml文件出错了,找不到指定的sql:原因是程序没有把.xml文件编译 ...
- pycharm中的搜索快捷键
- mysql事务管理及spring声明式事务中主动异常抛出使数据库回滚
mysql的引擎常用的有两个,一个MyISAM,另一个是InnoDB,mysql默认的为MyISAM,而InnoDB才是支持事务的.所以一般需要修改下,如何修改就不说了. 事务需要依赖数据库,好久没使 ...
- 【Hibernate 多表查询】
HibernateManyTable public class HibernateManyTable { //演示hql左连接查询 @Test public void testSelect12() { ...
- SpringBoot2.x过后static下的静态资源无法访问
@Configuration public class CorsConfig extends WebMvcConfigurationSupport {/** * 添加静态资源文件,外部可以直接访问地址 ...
- Shiro入门基础
Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能. Authentication:身份认证/登录,验证用户是不是拥有相应的身份: Authorization:授权,即 ...
- PAT 1003 Emergency (25分)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- iOS 增强程序健壮性 - - 使用 NullSafe 对 <null> 处理
在项目开发中,和服务端交互数据时,若服务端数据为空时,会出现 <null>,客户端解析时会 Crash,为了增强程序的健壮性,减少 Crash 的发生,可以使用 NullSafe 这个类别 ...
- javaweb项目中web.xml配置文件的/和/*的区别
1.拦截"/",可以实现现在很流行的REST风格.很多互联网类型的应用很喜欢这种风格的URL.为了实现REST风格,拦截了所有的请求.同时对*.js,*.jpg等静态文件的访问也就 ...
- 解决 Anaconda 3.7更新出现CondaHTTPError与SSLError
1.问题描述: An HTTP error occurred when trying to retrieve this URL. HTTP errors are often intermittent, ...