Codeforces 710D Two Arithmetic Progressions(同余方程组)

本文介绍了解同余方程组的方法,包括使用扩展欧几里得算法(exgcd)来寻找满足特定条件的解,并提供了两种不同的实现代码。通过这些方法可以有效地找到满足多个同余条件的解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

x=a1k+b1=a2l+b2
x=b1(moda1),x=b2(moda2)
b1,b2L=max(max(b1,b2),L)
k,l0L
xlcm(a1,a2)
11


2exgcd
a1x+a2y=b2b1
xx
a1x+b1Lb1,b2,L
lcm(a1,a2)


代码

#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           10000005
#define   MAXN          1000005
#define   maxnode       205
#define   sigma_size    26
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-4;
const LL    mod    = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/ 

vector<int> a,b;

LL mul(LL a,LL b,LL mod){
    LL n = 0;
    while(b){
        if(b&1)
          n = (n+a)%mod;
        a = (a*2)%mod;
        b /= 2;
    }
    return n;
}

void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
    if(!b){x=1;y=0;d=a;return;}
    else {exgcd(b,a%b,d,y,x);y-=a/b*x;}
}

LL labs(LL a){
    if(a<0) return -a;
    return a;
}
LL solve(){ //x=b[i](mod a[i])
    LL ta=a[0],tb=b[0];
    int flag=1;
    for(int i=1;i<a.size();i++){
        LL xa=ta,xb=a[i],c=b[i]-tb,d,x,y;
        exgcd(xa,xb,d,x,y);
        if(c%d){
            flag=0;
            break;
        }
        LL tmp=xb/d;
        LL k=1;
        if(x<0) k*=-1;
        if(c/d<0) k*=-1;
        x=(mul(labs(x),labs(c/d),tmp)*k+tmp)%tmp;//加入了二分快速乘,需要把负数先变为正数
        tb=ta*x+tb; 
        ta=ta/d*a[i];
    }
    if(!flag) return -INF;
    return tb;
}

LL gcd(LL x,LL y){
    if(!y) return x;
    return gcd(y,x%y);
}

LL lcm(LL x,LL y){
    return x*y/gcd(x,y);
}
int main(){
    LL a1,b1,a2,b2,L,R;
    while(cin>>a1>>b1>>a2>>b2>>L>>R){
        a.clear();b.clear();
        a.push_back(a1);
        a.push_back(a2);
        b.push_back(b1);
        b.push_back(b2);
        LL cnt=solve();
        L=max(max(b1,b2),L);
        //cout<<cnt<<endl;
        if(L>R){
            cout<<0<<endl;
            continue;
        }
        if(cnt==-INF){
            cout<<0<<endl;
            continue;
        }
        LL tmp=lcm(a1,a2);
        LL ans=0;
        if(cnt<=R) ans+=(R-cnt)/tmp+1;
        if(cnt<L) ans-=(L-1-cnt)/tmp+1;
        cout<<ans<<endl;
    }
    return 0;
}

代码2:

#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <bitset>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#pragma comment(linker,"/STACK:102400000,102400000")

using namespace std;
#define   MAX           100005
#define   MAXN          1000005
#define   maxnode       205
#define   sigma_size    26
#define   lson          l,m,rt<<1
#define   rson          m+1,r,rt<<1|1
#define   lrt           rt<<1
#define   rrt           rt<<1|1
#define   middle        int m=(r+l)>>1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   pii           pair<int,int>
#define   bits(a)       __builtin_popcount(a)
#define   mk            make_pair
#define   limit         10000

//const int    prime = 999983;
const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 0x3f3f;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-4;
const LL    mod    = 1e9+7;
const ull    mx    = 133333331;

/*****************************************************/
inline void RI(int &x) {
      char c;
      while((c=getchar())<'0' || c>'9');
      x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
 }
/*****************************************************/ 

void exgcd(LL a,LL b,LL &d,LL &x,LL &y){  
    if(!b){x=1;y=0;d=a;return;}  
    else {exgcd(b,a%b,d,y,x);y-=a/b*x;}  
}  

LL labs(LL a){
    if(a<0) return -a;
     return a;
}
int main(){
    LL a1,b1,a2,b2,L,R;
    while(cin>>a1>>b1>>a2>>b2>>L>>R){
        LL x,y,d;
        exgcd(a1,a2,d,x,y);
        if((b2-b1)%d!=0){
            cout<<0<<endl;
            continue;
        }
        x*=(b2-b1)/d;
        x=(x%labs(a2/d)+labs(a2/d))%labs(a2/d);
        LL cnt=x*a1+b1;
        LL tmp=labs(a1*a2/d);
        //cout<<cnt<<" "<<tmp<<endl;
        LL ans=0;
        L=max(L,max(b1,b2));
        if(L>R){
            cout<<0<<endl;
            continue;
        }
        if(cnt<=R) ans+=(R-cnt)/tmp+1;
        if(cnt<L) ans-=(L-1-cnt)/tmp+1;
        cout<<ans<<endl;
    }
    return 0;
}
### Codeforces Problem 710F 解法分析 Codeforces Problem 710F 是一道涉及字符串处理的经典问题,其核心在于高效实现动态字符串集合的操作以及查询功能。以下是对此问题的详细解析。 #### 题目概述 该题目要求维护一个字符串集合 \( D \),并支持三种操作: 1. **添加字符串**到集合 \( D \) 中。 2. **删除字符串**从集合 \( D \) 中。 3. 给定一个字符串 \( str \),计算集合 \( D \) 中所有字符串在 \( str \) 中的总出现次数。 由于数据规模较大 (\( m, \sum |str| \leq 3 \times 10^5 \)),因此需要设计高效的算法来完成这些操作。 --- #### 核心思路 此问题可以通过 **AC 自动机 (Aho-Corasick Automaton)** 和 **二进制分组技术** 来解决[^5]。 ##### 1. AC 自动机构建 - 使用 AC 自动机对字符串集合 \( D \) 进行动态管理。 - 当执行 “添加字符串” 或 “删除字符串” 操作时,在 AC 自动机上更新对应的节点权重(表示某个模式串是否存在于集合中)。 - 对于每次查询操作,通过遍历目标字符串 \( str \) 并匹配 AC 自动机上的状态转移路径,统计所有匹配成功的模式串数量及其频率。 ##### 2. 动态更新优化 - 考虑到频繁的插入和删除操作可能带来较高的时间复杂度,采用 **二进制分组策略** 将字符串按批次进行管理和重建。 - 具体而言,将新加入的字符串暂时存入缓冲区,当缓冲区内积累了一定量的数据后再统一重新构建一次完整的 AC 自动机。 - 此方法能够有效平衡单次操作的时间开销与整体性能需求。 --- #### 实现细节 下面是一个基于上述思想的具体实现框架: ```cpp #include <bits/stdc++.h> using namespace std; // 定义 AC 自动机结构 struct ACAutomaton { struct Node { int next[26]; int fail; long long weight; // 记录当前节点对应的有效模式串总数 bool isEnd; // 是否为某模式串终点 Node() : fail(-1), weight(0), isEnd(false) { memset(next, -1, sizeof(next)); } }; vector<Node> nodes; ACAutomaton() { nodes.push_back(Node()); } void insert(const string &s) { int current = 0; for(char c : s){ if(nodes[current].next[c-'a'] == -1){ nodes[current].next[c-'a'] = nodes.size(); nodes.push_back(Node()); } current = nodes[current].next[c-'a']; } nodes[current].isEnd = true; ++nodes[current].weight; } void buildFail(){ queue<int> q; for(int i=0;i<26;++i){ if(nodes[0].next[i]!=-1){ nodes[nodes[0].next[i]].fail = 0; q.push(nodes[0].next[i]); } } while(!q.empty()){ int u = q.front(); q.pop(); for(int i=0;i<26;++i){ if(nodes[u].next[i]==-1) continue; int v = nodes[u].next[i]; int f = nodes[u].fail; while(f != -1 && nodes[f].next[i]==-1) f = nodes[f].fail; if(f == -1) nodes[v].fail = 0; else{ nodes[v].fail = nodes[f].next[i]; nodes[v].weight += nodes[nodes[f].next[i]].weight; } q.push(v); } } } long long query(const string &text){ long long res = 0; int state = 0; for(auto ch:text){ int idx = ch - 'a'; while(state!=-1 && nodes[state].next[idx]==-1) state = nodes[state].fail; if(state==-1) state = 0; else state = nodes[state].next[idx]; res += nodes[state].weight; } return res; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int Q; cin >> Q; ACAutomaton ac; map<string,int> freqMap; // 辅助记录每条字符串的实际存在情况 while(Q--){ char op; string s; cin >> op >> s; if(op=='1'){ // 添加字符串 if(freqMap[s]==0){ ac.insert(s); } ++freqMap[s]; } else if(op=='2'){ // 删除字符串 if(freqMap[s]>0){ --freqMap[s]; if(freqMap[s]==0){ // 更新 AC 自动机逻辑以移除影响 // 可能需额外标记机制或重建部分树状结构 } } } else{ // 查询操作 cout << ac.query(s) << "\n"; } } } ``` --- #### 时间复杂度分析 - 插入/删除操作:平均情况下接近线性时间 \( O(|S_i|) \),但由于采用了批量重构方式,实际运行效率更高。 - 查询操作:对于长度为 \( L \) 的输入字符串,耗时约为 \( O(L) \)。 --- #### 注意事项 - 输入强制在线意味着无法预先读取全部测试样例内容,必须逐条实时响应请求。 - 数据范围较广,建议选用快速 IO 方法提升程序吞吐能力。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值