A. Array Divisibility
需要让满足$ k\mid j $ 的所有\(a_j\)的和整除k,只需要让每个\(a_j\)整除k就可以了,可以让\(a_j=j\)
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long long ll;
const int N = 2e5+5,M=20,mod=998244353,P=131;
void solve(){
int n;
cin>>n;
for(int i=1;i<=n;i++)cout<<i<<" ";
cout<<endl;
}
void fast(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
}
signed main(){
fast();
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
B. Corner Twist
可以知道,大矩形的操作可以由小矩形合成,两次副对角+1的操作可以合成为主对角+1的操作
于是使用最小的,副对角为1的矩形操作,从上到下,从左到右,最后判断相等就可以了
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define endl '\n'
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long long ll;
const int N = 505,M=20,mod=998244353,P=131;
int mp1[N][N];
int mp2[N][N];
void solve(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%1d",&mp1[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%1d",&mp2[i][j]);
for(int i=1;i<n;i++){
for(int j=1;j<m;j++){
while(mp1[i][j]!=mp2[i][j]){
mp1[i][j]=(mp1[i][j]+1)%3;
mp1[i+1][j]=(mp1[i+1][j]+2)%3;
mp1[i+1][j+1]=(mp1[i+1][j+1]+1)%3;
mp1[i][j+1]=(mp1[i][j+1]+2)%3;
}
}
}
int flag=1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(mp1[i][j]!=mp2[i][j])flag=0;
}
}
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
void fast(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
}
signed main(){
// fast();
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
C. Have Your Cake and Eat It Too
双指针枚举中间的一段,然后前缀和判断左右两边能不能用就行了
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long long ll;
const int N = 505,M=20,mod=998244353,P=131;
void solve(){
int n;
cin>>n;
vector<int> a(n+2),b(n+2),c(n+2);
vector<int> qza(n+2),qzb(n+2),qzc(n+2);
vector<int> hza(n+2),hzb(n+2),hzc(n+2);
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
for(int i=1;i<=n;i++)cin>>c[i];
int sum=0;
for(int i=1;i<=n;i++)sum+=a[i];
int tp=(sum+2)/3;
for(int i=1;i<=n;i++){
qza[i]=qza[i-1]+a[i];
qzb[i]=qzb[i-1]+b[i];
qzc[i]=qzc[i-1]+c[i];
}
for(int i=n;i>=1;i--){
hza[i]=hza[i+1]+a[i];
hzb[i]=hzb[i+1]+b[i];
hzc[i]=hzc[i+1]+c[i];
}
int l=1,s=0;
for(int r=1;r<=n;r++){
s+=a[r];
while(s-a[l]>=tp)s-=a[l++];
if(qzb[l-1]>=tp&&hzc[r+1]>=tp){
cout<<l<<" "<<r<<" "<<1<<" "<<l-1<<" "<<r+1<<" "<<n<<endl;
return ;
}
if(qzc[l-1]>=tp&&hzb[r+1]>=tp){
cout<<l<<" "<<r<<" "<<r+1<<" "<<n<<" "<<1<<" "<<l-1<<endl;
return ;
}
}
l=1,s=0;
for(int r=1;r<=n;r++){
s+=b[r];
while(s-b[l]>=tp)s-=b[l++];
if(qza[l-1]>=tp&&hzc[r+1]>=tp){
cout<<1<<" "<<l-1<<" "<<l<<" "<<r<<" "<<r+1<<" "<<n<<endl;
return ;
}
if(qzc[l-1]>=tp&&hza[r+1]>=tp){
cout<<r+1<<" "<<n<<" "<<l<<" "<<r<<" "<<1<<" "<<l-1<<endl;
return ;
}
}
l=1,s=0;
for(int r=1;r<=n;r++){
s+=c[r];
while(s-c[l]>=tp)s-=c[l++];
if(qza[l-1]>=tp&&hzb[r+1]>=tp){
cout<<1<<" "<<l-1<<" "<<r+1<<" "<<n<<" "<<l<<" "<<r<<endl;
return ;
}
if(qzb[l-1]>=tp&&hza[r+1]>=tp){
cout<<r+1<<" "<<n<<" "<<1<<" "<<l-1<<" "<<l<<" "<<r<<endl;
return ;
}
}
cout<<-1<<endl;
return ;
}
void fast(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
}
signed main(){
fast();
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
D. Swap Dilemma
可以知道,更大的操作可以由更小的操作合成,所以只需要交换两个相邻数就可以了,那么我们不妨把两个数组都交换成升序,如果这样做,因为其中一个交换时另外一个必须同时交换,并且同一对位置交换两次就抵消,所以就是看两者的交换次数是不是同奇偶,交换次数计算逆序对就行了。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long long ll;
const int N = 1e5+5,M=20,mod=998244353,P=131;
int tmp[N];
int merge_sort(vector<int> &a,int l,int r){
if(l>=r)return 0;
int mid=l+r>>1,res=0;
res+=merge_sort(a,l,mid);
res+=merge_sort(a,mid+1,r);
int i=l,j=mid+1,cnt=l;
while(i<=mid&&j<=r){
if(a[i]<=a[j])tmp[cnt++]=a[i++];
else {
res+=mid-i+1;
tmp[cnt++]=a[j++];
}
}
while(i<=mid)tmp[cnt++]=a[i++];
while(j<=r)tmp[cnt++]=a[j++];
for(i=l;i<=r;i++)a[i]=tmp[i];
return res;
}
void solve(){
int n;
cin>>n;
vector<int> a(n+1),b(n+1);
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
int ra=merge_sort(a,1,n);
int rb=merge_sort(b,1,n);
int flag=1;
for(int i=1;i<=n;i++)
if(a[i]!=b[i])flag=0;
if(abs(ra-rb)%2==0&&flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
void fast(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
}
signed main(){
fast();
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
E. I Love Balls
因为挑选到特殊球可以继续往下挑选,一直到挑选到普通球为止,那么我们不妨先计算普通球的贡献,再以插空的方式计算特殊球的贡献。可以知道每个普通球对先手者的贡献是\(\frac{\lceil\frac{n-k}{2}\rceil}{n-k}\cdot a[i]\), 每个普通球对后手者的贡献是\(\frac{\lfloor\frac{n-k}{2}\rfloor}{n-k}\cdot a[i]\),然后再插空计算贡献,有\(n-k+1\)个空,所以每个特殊球对先手者的贡献是\(\frac{\lceil\frac{n-k+1}{2}\rceil}{n-k+1}\cdot a[i]\),每个特殊球对后手者的贡献是\(\frac{\lceil\frac{n-k+1}{2}\rceil}{n-k+1}\cdot a[i]\) 。
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long long ll;
const int N = 2e5+5,M=20,mod=1e9+7,P=131;
int qmi(int a,int k){
int res=1;
while(k){
if(k&1)res=res*a%mod;
a=a*a%mod;
k>>=1;
}
return res;
}
void solve(){
int n,k;
cin>>n>>k;
vector<int> a(n+1);
for(int i=1;i<=n;i++)cin>>a[i];
int ans1=0,ans2=0;
for(int i=k+1;i<=n;i++){
ans1=(ans1+(n-k+1)/2*a[i]%mod*qmi(n-k,mod-2)%mod)%mod;
ans2=(ans2+(n-k)/2*a[i]%mod*qmi(n-k,mod-2)%mod)%mod;
}
for(int i=1;i<=k;i++){
ans1=(ans1+(n-k+2)/2*a[i]%mod*qmi(n-k+1,mod-2)%mod)%mod;
ans2=(ans2+(n-k+1)/2*a[i]%mod*qmi(n-k+1,mod-2)%mod)%mod;
}
cout<<ans1<<" "<<ans2<<endl;
}
void fast(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
}
signed main(){
fast();
int t=1;
cin>>t;
while(t--)solve();
return 0;
}
标签:typedef,int,题解,long,956,solve,tp,Div,define
From: https://www.cnblogs.com/lxllxs/p/18295926