文章目录
A. Subtle Substring Subtraction(签到)
思路: n n n为奇数时,头大吃头,尾大吃尾,留下一个给 B o b Bob Bob,再做比较; n n n为偶数, A l i c e Alice Alice全吃必赢
C o d e : Code: Code:
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define y1 yyy
typedef long long ll;
typedef unsigned long long u64;
typedef pair<string,int> PII;
const int N=2e5+10,mod=998244353;
int t;
void solve(){
string s;
cin>>s;
int n=s.size();
if(n%2==1){
ll ans1=0,ans2=0;
if(s[0]>s[n-1]){
for(int i=0;i<n-1;i++) ans1+=(s[i]-'a'+1);
ans2=s[n-1]-'a'+1;
}else {
for(int i=1;i<n;i++) ans1+=(s[i]-'a'+1);
ans2=s[0]-'a'+1;
}
if(ans1>ans2) {
cout<<"Alice"<<" "<<ans1-ans2<<endl;
}else{
cout<<"Bob"<<" "<<ans2-ans1<<endl;
}
}else{
ll ans1=0;
for(int i=0;i<n;i++) ans1+=(s[i]-'a'+1);
cout<<"Alice"<<" "<<ans1<<endl;
}
}
int main(){
read(t);
while(t--) solve();
return 0;
}
B. A Perfectly Balanced String?(思维)
思路:如果不是类似于 a b c a b c a . . . abcabca... abcabca...的字符串都能找出一个子串使其不包括某个字符但这个子串中存在某个字符的个数大于等于2
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define y1 yyy
typedef long long ll;
typedef unsigned long long u64;
typedef pair<string,int> PII;
const int N=2e5+10,mod=998244353;
void solve(){
string s;cin>>s;
set<char>st;
for(auto i:s) st.insert(i);
int m=st.size(),n=s.size();
for(int i=m;i<n;i++){
if(s[i]!=s[i-m]){
cout<<"NO"<<endl;
return;
}
}
cout<<"YES"<<endl;
}
int main(){
int t; cin>>t;
while(t--){
solve();
}
return 0;
}
C. Palindrome Basis(完全背包)
思路:先预处理出所有的对称数,大约 500 500 500个,然后就是一个完全背包求方案数的裸题
C o d e : Code: Code:
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define y1 yyy
typedef long long ll;
typedef unsigned long long u64;
typedef pair<string,int> PII;
const int N=4e4+10,mod=1e9+7;
int t,k;
vector<int>a;
ll dp[N];
bool check(int x){
vector<int>q;
q.clear();
while(x){
q.push_back(x%10);
x/=10;
}
for(int i=0,j=q.size()-1;i<=j;i++,j--){
if(q[i]!=q[j]) return false;
}
return true;
}
void solve(){
int n;cin>>n;
cout<<dp[n]<<endl;
}
int main(){
cin>>t;
for(int i=1;i<=N;i++){
if(check(i)) a.push_back(i);
}
k=a.size();
dp[0]=1;
for(int i=0;i<k;i++){
for(int j=a[i];j<=N-10;j++){
dp[j]=(dp[j]+dp[j-a[i]])%mod;
}
}
while(t--){
solve();
}
return 0;
}
D. Lost Arithmetic Progression(数学)
思路:
C
C
C是
A
A
A和
B
B
B的子集,因此
d
c
=
l
c
m
(
d
a
,
d
b
)
dc=lcm(da,db)
dc=lcm(da,db)
接下来判断
C
C
C是否全部在
B
B
B中出现过,需要满足下面三个条件:
if(!(st1<=st2&&ed2<=ed1)||d2%d1!=0||(st2-st1)%d1!=0)
接下来判断 A A A是否是无穷多个,满足条件为:
if(st2-d2<st1||ed2+d2>ed1)
枚举 d a da da, d a da da是 d c dc dc的因子,对于每个满足条件的 d a da da,可以得到 A A A的种数为: d c d a ∗ d c d a \frac{dc}{da}*\frac{dc}{da} dadc∗dadc
C o d e : Code: Code:
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define y1 yyy
typedef long long ll;
typedef unsigned long long u64;
typedef pair<int,int> PII;
const int N=5e5+10,mod=1e9+7;
ll st1,d1,len1,st2,d2,len2;
ll gcd(ll a,ll b){
if(!b) return a;
return gcd(b,a%b);
}
ll lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
void solve(){
cin>>st1>>d1>>len1;
cin>>st2>>d2>>len2;
ll ed1=st1+d1*(len1-1);
ll ed2=st2+d2*(len2-1);
if(!(st1<=st2&&ed2<=ed1)||d2%d1!=0||(st2-st1)%d1!=0){
cout<<"0"<<endl;
return ;
}
if(st2-d2<st1||ed2+d2>ed1) {
cout<<"-1"<<endl;
return ;
}
ll ans=0;
for(ll i=1;i*i<=d2;i++){
if(d2%i==0){
if(lcm(d1,i)==d2) ans=(ans+(d2/i)*(d2/i))%mod;
if(d2/i!=i&&lcm(d1,d2/i)==d2) ans=(ans+i*i)%mod;
}
}
cout<<ans<<endl;
}
int main(){
int t;cin>>t;
while(t--) solve();
return 0;
}