文章目录
C - SSPPSPSP
题目来源:C - SSPPSPSP
!](https://i-blog.csdnimg.cn/direct/26fc1492b1724446be61cf39b718cf9b.png)
解题思路
题目很长但读懂题意就很简单,是一个根据字符顺序不断累加或累乘的过程。可以先算出一开始的加和以及乘积之和,然后从后往前依次累算。如果是s就累加加和,如果是p就累乘乘积之和。记得每次都要取模。
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const int mod=998244353;
int n,k,a[20];
string s;
void solve()
{
cin>>n>>k;
int s1=0,s2=1,sum,ans;
for(int i=1;i<=n;i++)
{
cin>>a[i];
s1=(s1+a[i])%mod;//算出初始的和
s2=(s2*a[i])%mod;// 积
}
cin>>s;
if(s[s.size()-1]=='s')//从后往前算,先将第一个值赋值在sum上
sum=s1%mod;
else
sum=s2%mod;
for(int i=s.size()-2;i>=0;i--)//依次遍历
{
if(s[i]=='s')//如果是加和可以直接乘n
sum=(sum*n)%mod;
else//乘积之积
{
ans=sum%mod;
for(int i=0;i<n-1;i++)
sum=(sum*ans)%mod;
}
}
cout<<sum%mod;
}
signed main()
{
IOS;
int _=1;
// cin>>_;
while(_--)solve();
return 0;
}
ProblemD.互互互质质质
题目来源:ProblemD.互互互质质质
解题思路
虽然x,y的范围很大,但依然可以遍历x,y之间的数来找符合条件的z,因为质数出现的次数还是很频繁的,质数一定分别和他们两个互质,因此它们要么就是相差很小没有互质的数,要么就一定在没超限的条件下能找到和他们都互质的数。
代码实现
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
void solve()
{
int x,y;
cin>>x>>y;
for(int i=x+1;i<y;i++)
{
if(__gcd(x,i)==1&&__gcd(i,y)==1)
{
cout<<i<<endl;
return ;
}
}
cout<<-1<<endl;
}
signed main()
{
IOS;
int _=1;
cin>>_;
while(_--)solve();
return 0;
}
ProblemF. Ever Forever
题目来源:ProblemF. Ever Forever
解题思路
签到题,题目范围很小,不用想太复杂,直接遍历,每遇到一个e就遍历从当前位置到最后的每个f的位置,将其每个距离加起来即可。
代码实现
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const int N=1e5+5;
void solve()
{
int n,ans=0;
cin>>n;
string s;
cin>>s;
for(int i=0;i<s.size();i++)
{
if(s[i]=='e')
{
for(int j=i+1;j<n;j++)
{
if(s[j]=='f')
ans+=(j-i);
}
}
}
cout<<ans;
}
signed main()
{
IOS;
int _=1;
// cin>>_;
while(_--)solve();
return 0;
}
ProblemG.石石石头头头剪剪剪刀刀刀布布布
题目来源:ProblemG.石头剪刀布
解题思路
又臭又烂的模拟题,我没有优化的方法,就是纯模拟,先模拟最大值,让石头1找剪刀2,如果有剩余再找石头2,如果还有剩余就只能找布2,最小值相反。
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
const int N = 1e5+1;
int a[N];
string s,s1;
vector<int> v;
int ans;
int n;
int x,y,z;
void solve()
{
int n,a1,b1,c1,a2,b2,c2,ans=0,res=0;
cin>>n>>a1>>b1>>c1>>a2>>b2>>c2;
int x1=a1,y1=b1,z1=c1,x2=a2,y2=b2,z2=c2;
if(a1<b2)
{
b2-=a1;
ans+=a1;
a1=0;
}
else
{
a1-=b2;
ans+=b2;
b2=0;
if(a1>=a2)
{
a1-=a2;
a2=0;
}
else
{
a2-=a1;
a1=0;
}
c2-=a1;
ans-=a1;
a1=0;
}
if(b1<c2)
{
c2-=b1;
ans+=b1;
b1=0;
}
else
{
b1-=c2;
ans+=c2;
c2=0;
if(b1>=b2)
{
b1-=b2;
b2=0;
}
else
{
b2-=b1;
b1=0;
}
a2-=b1;
ans-=b1;
b1=0;
}
if(c1<a2)
{
a2-=c1;
ans+=c1;
c1=0;
}
else
{
c1-=a2;
ans+=a2;
a2=0;
if(c1>=c2)
{
c1-=c2;
c2=0;
}
else
{
c2-=c1;
c1=0;
}
b2-=c1;
ans-=c1;
c1=0;
}
cout<<ans<<" ";
if(x1<z2)
{
z2-=x1;
res+=x1;
x1=0;
}
else
{
x1-=z2;
res+=z2;
z2=0;
if(x1>=x2)
{
x1-=x2;
x2=0;
}
else
{
x2-=x1;
x1=0;
}
y2-=x1;
res-=x1;
x1=0;
}
if(y1<x2)
{
x2-=y1;
res+=y1;
y1=0;
}
else
{
y1-=x2;
res+=x2;
x2=0;
if(y1>=y2)
{
y1-=y2;
y2=0;
}
else
{
y2-=y1;
y1=0;
}
z2-=y1;
res-=y1;
y1=0;
}
if(z1<y2)
{
y2-=z1;
res+=z1;
z1=0;
}
else
{
z1-=y2;
res+=y2;
y2=0;
if(z1>=z2)
{
z1-=z2;
z2=0;
}
else
{
z2-=z1;
z1=0;
}
x2-=z1;
res-=z1;
z1=0;
}
cout<<-1*res<<endl;
}
signed main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
cin>>t;
while(t--) solve();
return 0;
}
Problem J.奇偶游戏
题目来源:ProblemJ.奇奇奇偶偶偶游游游戏戏戏
解题思路
超级无敌签到题,先找出二者最大值,再判断奇偶,如果是奇数输出1,否则输出0;
代码实现
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const int N=1e5+5;
void solve()
{
int a,b;
cin>>a>>b;
int mx=max(a,b);
if(mx%2==1)
cout<<1;
else
cout<<2;
}
signed main()
{
IOS;
int _=1;
// cin>>_;
while(_--)solve();
return 0;
}
Problem L.好矩阵
题目来源:ProblemL.好好好矩矩矩阵阵阵
解题思路
找规律的一道题,题目意思是,矩阵的每行每列的异或和等于他本身,这样的01矩阵有多少个。通过给的样例就不难看出,如果两个数一奇一偶,输出2的奇数减1次幂,如果两个都是奇数则输出 2 的 二者之和减2 次幂,记得用快速幂取模!.
代码实现
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
const int mod= 998244353;
int Qmi(int a,int b)
{
int ans=1;
while(b)
{
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
void solve()
{
int x,y;
cin>>x>>y;
int ans=0;
if(x%2==0||y%2==0)
{
if(x&1)
ans=y-1;
if(y&1)
ans=x-1;
}
else if(x&1&&y&1)
ans=x+y-2;
cout<<Qmi(2,ans)<<endl;
}
signed main()
{
IOS;
int _=1;
cin>>_;
while(_--)solve();
return 0;
}
完结撒花~