题解:CF1729F Kirei and the Linear Function
yr409892525 · · 题解
题解:CF1729F Kirei and the Linear Function
我们知道一个数模
先处理出每个长度为
对于每次询问,我们都可以枚举
然后再用预处理出的坐标去更新答案。
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,m,q,t;
string s;
vector<int> p[9];
int a[N];
int main(){
for(cin>>q;q--;){
cin>>s>>m>>t;
n=s.size();
for(int i=0;i<9;i++){
p[i].clear();
}
for(int i=1;i<=n;i++){
a[i]=(a[i-1]+s[i-1]-'0')%9;
if(i>=m) p[(a[i]-a[i-m]+9)%9].push_back(i-m+1);
}
for(int l,r,x,l1=2e9,l2=2e9;t--;l1=2e9,l2=2e9){
cin>>l>>r>>x;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
if((i*(a[r]-a[l-1]+9)+j)%9==x){
if(i==j && p[i].size()>=2){
if(p[i][0]<l1 || p[i][0]==l1 && p[i][1]<l2){
l1=p[i][0],l2=p[i][1];
}
}
if(i!=j && p[i].size() && p[j].size()){
if(p[i][0]<l1 || p[i][0]==l1 && p[j][0]<l2){
l1=p[i][0],l2=p[j][0];
}
}
}
}
}
cout<<(l1==2e9?-1:l1)<<" "<<(l2==2e9?-1:l2)<<"\n";
}
}
return 0;
}