跳到正文

题解

题解:CF1729F Kirei and the Linear Function

yr409892525约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年4月16日
收录
2025年12月12日
更新
2026年10月27日
篇幅
约 3 分钟
获赞
2
题目
CF1729F

版本

  1. v1当前

题解:CF1729F Kirei and the Linear Function

我们知道一个数模 99 的余数和这个数的数字和模 99 的余数一样。
先处理出每个长度为 ww,且 v(i,i+w−1) mod 9=xv(i,i+w-1) \bmod 9=x 的所有坐标。
对于每次询问,我们都可以枚举 v(L1,L1+w−1) mod 9v(L1,L1+w-1) \bmod 9 和 v(L2,L2+w−1) mod 9v(L2,L2+w-1) \bmod 9。
然后再用预处理出的坐标去更新答案。

C++
#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;
}
原文
存档评论2 条抓取于