2023 QBXT Day4 赛总

· · 个人记录

T1

题面

众所周知,小葱同学擅长计算,尤其擅长计算组合数,但这个题和组合数没什么关系。 小葱同学喜欢吃糖,小葱买了很多糖。但是小葱买的糖太多了,小葱记不清具体数字了,小葱只记得自己的总糖数是自己记录在笔记本上的N个数a_1,a_2,\cdots,a_N的最小公倍数。请你帮帮小葱,算算小葱买了多少糖。 第一行一个正整数N。 第二行N个整数a_1,a_2,\cdots,a_N。 一行一个整数代表答案对10^9+7取模的结果。 对于40\%的数据,1\leq N,a_i\leq10。 对于60\%的数据,a_i\leq 1000。 对于80\%的数据,a_i\leq 10^6。 对于100\%的数据,1\leq N\leq 10^3,1\leq a_i\leq 10^9。

思路

考虑质因数分解,此时取\text{lcm}等同于指数取最大值,简单维护即可

代码

//吾日十一省吾身:
//输入多而不快读乎?
//题目标注而不freopen乎?
//乘除并列先乘后除乎?
//不手撕样例直接写代码乎?
//不仔细读题直接关页面乎?
//1e9而不开long long乎?
//Ctrl+V而不改名称乎?(papaw->papan IMPLIES tg1=->2=)
//相信评测神机乎?
//多测清空不彻底乎?
//写完题而不对拍乎?
//只考虑期望复杂度而不记忆化乎?
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<utility>
#include<deque>
#include<ctime>
#include<sstream>
#include<list>
#include<bitset>
#include<map>
using namespace std;
typedef long long ll;
namespace FastIO{
    template<class T> void R(T &x){
        x=0;ll f=1;char c='c';
        while(c>'9' or c<'0'){f=(c=='-'?-f:f);c=getchar();}
        while(c<='9' and c>='0'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
        x*=f;
        return;
    }
    template<class T,class... _T> void R(T& x,_T&... _x){R(x);R(_x...);return;}
    template<class T> void W(T x,char c=0){
        if(c){W(x);putchar(c);}
        else if(x<0){putchar('-');W(-x);}
        else if(x<10){putchar(x+'0');}
        else{W(x/10);putchar(x%10+'0');}
        return;
    }
}
using namespace FastIO;
void open_file(){
    freopen("buy.in","r",stdin);
    freopen("buy.out","w",stdout);
    return;
}
void close_file(){
    fclose(stdin);
    fclose(stdout);
    return;
}
const ll MO(1e9+7);
ll n,a[114514];
map<ll,ll> tot;
void Insert(ll num){
    ll ren=num;
    for(ll i=2;i*i<=ren;++i){
        ll rap=0;
        while(num%i==0){
            rap++;
            num/=i;
        }
        if(rap) tot[i]=max(tot[i],rap);
    }
    if(num>1)
        tot[num]=max(tot[num],1ll);
    return;
}
ll ans=1;
ll fpow(ll x,ll y){
    ll res=1;
    while(y){
        if(y&1) res=(res*x)%MO;
        x=(x*x)%MO;
        y>>=1;
    }
    return res;
}
int main(){
    open_file(); 
    R(n);
    for(ll i=1;i<=n;++i){
        R(a[i]);
        Insert(a[i]);
    }
    for(auto i:tot){
        ans=(ans*fpow(i.first,i.second))%MO;
    }
    W(ans,'\n');
    close_file();
    return 0;
} 

T2

题面

思路

代码

T3

题面

众所周知,小葱同学擅长计算,尤其擅长计算组合数,但这个题和组合数没什么关系。 小葱想要吃糖,小葱将拿出来的N颗糖排成一排,所有糖的美味值构成了一个N位数,每个糖的美味值都在0\sim9之间。小葱可以吃掉一段区间的糖,前提是这段区间糖果美味值组成的数恰好是p的倍数(可以有前导零)。现在小葱想知道,对于第l颗糖到第r颗糖的所有子区间,有几个区间是可以被吃掉的呢? 第一行一个字符串a_1 a_2\cdots a_N,代表所有糖的美味值。 第二行两个整数p,M,其中M代表操作次数。 接下来M行每行第一个数opt。如果opt=1,接下来会给出两个整数x,y,代表将第x颗糖的美味值修改为y;如果opt=2,接下来会给出两个整数l,r代表一次询问操作。 对于每个询问操作,一行一个整数代表答案。 对于20\%的数据,N,M\leq100。 对于另外15\%的数据,opt=2。 对于另外5\%的数据,p=2。 对于另外5\%的数据,p=3。 对于另外5\%的数据,p=4。 对于另外5\%的数据,p=6。 对于另外5\%的数据,p=11。 对于100\%的数据,1\leq N,M\leq 10^5,2\leq p\leq 15,0\leq a_i,y\leq 9,1\leq x\leq N,1\leq l\leq r\leq N。

思路

区间内区间计数问题容易想到线段树,考虑子区间如何合并 首先区间内答案可以直接继承,对于跨越中点的区间,不难想到维护区间前缀和后缀生成的十进制数\mod p=k的前缀、后缀个数,但是区间拼接有进位效果,所以假设右半区间前缀长度为l,生成的十进制数\mod p为y,左半区间为x,那么有

x*10^l+y\equiv 0 \mod p

考虑到10^l不好处理,但是因为模数很小,所以考虑将10^l\mod p也纳入计数分类标准中,这样进行合并就容易了 需要注意的是,由于逆元未必存在,所以要合理选择枚举的变量,否则某些东西可能会很难处理

代码

//吾日十一省吾身:
//输入多而不快读乎?
//题目标注而不freopen乎?
//乘除并列先乘后除乎?
//不手撕样例直接写代码乎?
//不仔细读题直接关页面乎?
//1e9而不开long long乎?
//Ctrl+V而不改名称乎?(papaw->papan IMPLIES tg1=->2=)
//相信评测神机乎?
//多测清空不彻底乎?
//写完题而不对拍乎?
//只考虑期望复杂度而不记忆化乎?
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<utility>
#include<deque>
#include<ctime>
#include<sstream>
#include<list>
#include<bitset>
using namespace std;
typedef long long ll;
namespace FastIO{
    template<class T> void R(T &x){
        x=0;ll f=1;char c='c';
        while(c>'9' or c<'0'){f=(c=='-'?-f:f);c=getchar();}
        while(c<='9' and c>='0'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
        x*=f;
        return;
    }
    template<class T,class... _T> void R(T& x,_T&... _x){R(x);R(_x...);return;}
    template<class T> void W(T x,char c=0){
        if(c){W(x);putchar(c);}
        else if(x<0){putchar('-');W(-x);}
        else if(x<10){putchar(x+'0');}
        else{W(x/10);putchar(x%10+'0');}
        return;
    }
}
using namespace FastIO;
void open_file(){
    freopen("eat.in","r",stdin);
    freopen("eat.out","w",stdout);
    return;
}
void close_file(){
    fclose(stdin);
    fclose(stdout);
    return;
}
const int MAXN(100005);
const int MAXP(16);
string s;
int p,m,n,val[MAXN];
int tep[MAXN];//10的次幂预处理 
void Insert(string s){
    n=s.length();tep[0]=1;
    for(int i=1;i<=n;++i) val[i]=s[i-1]-'0',tep[i]=(tep[i-1]*10)%p;
    return;
}
struct node{
    int l,r,lson,rson,sum,len;
    ll ans;
    int pre[MAXP][MAXP],suf[MAXP];//pre[i][j]:满足(10^len)%p=i,sum%p=j的前缀个数,suf[i]:满足sum%p=i的后缀个数 
    bool is_trash;//是否为无用数据
    void Clear(){
        l=r=0;lson=rson=0;ans=sum=len=0;is_trash=false;
        for(int i=0;i<p;++i){
            suf[i]=0;
            for(int j=0;j<p;++j) pre[i][j]=0;
        }
        return;
    }
}res; 
node operator+(const node& a,const node& b){
    if(a.is_trash) return b;if(b.is_trash) return a;
    node c;c.Clear();
    c.is_trash=false;c.ans=a.ans+b.ans;c.l=a.l;c.r=b.r;c.len=a.len+b.len;c.sum=(a.sum*tep[b.len]%p+b.sum)%p;
    for(int i=0;i<p;++i){
        c.suf[i]+=b.suf[i];//i是bsufi 
        c.suf[((i*tep[b.len])%p+b.sum)%p]+=a.suf[i];//aprei*tep+bsum=i,选择枚举aprei 
        for(int j=0;j<p;++j){
            c.pre[i][j]+=a.pre[i][j];//i是tepi,j是aprei 
            c.pre[tep[a.len]*i%p][(a.sum*i+j)%p]+=b.pre[i][j];//asum*tepi+bprei=i,选择枚举tepi和bprei 
            c.ans+=a.suf[i]*b.pre[j][(p-(i*j)%p)%p];//i*j+x=0(mod p)->x=(-i*j%p+p)%p,i枚举左边后缀和%p,j枚举右边前缀10^len%p  
        }
    }//值得注意的是,上述每一个语句里i和j的意义都不相同 
    return c; 
}//对pre、suf、ans、len、l、r、sum进行合并
class Segment_Tree{
    node Tree[MAXN<<2];
    int cnt;
    #define LFT Tree[pos].l
    #define RGT Tree[pos].r
    #define LS Tree[pos].lson
    #define RS Tree[pos].rson
    #define ANS Tree[pos].ans
    #define SUM Tree[pos].sum
    #define LEN Tree[pos].len
    #define SUF Tree[pos].suf
    #define PRE Tree[pos].pre
    void Push_up(int pos){
        res=Tree[LS]+Tree[RS];
        res.is_trash=false;res.lson=LS;res.rson=RS;
        swap(Tree[pos],res);
        return;
    }
    void EDIT(int pos,int whc,int va){
        if(!(LFT<=whc&&whc<=RGT)) return;
        if(LFT==RGT){
            SUF[SUM%p]=0;PRE[10%p][SUM%p]=0;
            SUM=va;ANS=((SUM%p==0)?1:0);
            SUF[SUM%p]=1;PRE[10%p][SUM%p]=1;
            return;
        }
        EDIT(LS,whc,va);
        EDIT(RS,whc,va);
        Push_up(pos);
        return;
    }
    node QUERY(int pos,int l,int r){
        node qwq;qwq.is_trash=true;//暂无结果 
        if(l>RGT||r<LFT) return qwq;
        if(l<=LFT&&RGT<=r) return Tree[pos];
        return QUERY(LS,l,r)+QUERY(RS,l,r); 
    }
    int root;
    void BUILD(int &pos,int l,int r){
        pos=++cnt;
        LFT=l;RGT=r;
        if(l==r){
            SUM=val[l];LEN=1;ANS=((SUM%p==0)?1:0);
            Tree[pos].is_trash=false;
            SUF[SUM%p]=1;PRE[10%p][SUM%p]=1;
            return;
        }
        ll mid=(l+r)>>1;
        BUILD(LS,l,mid);
        BUILD(RS,mid+1,r);
        Push_up(pos);
        return;
    }
    public:
        void Build(){BUILD(root,1,n);return;}
        int Query(int l,int r){return QUERY(root,l,r).ans;}
        void Edit(int pos,int va){EDIT(root,pos,va);return;}
}T;
int opt,l,r;
int main(){
    open_file();
    cin>>s;
    R(p,m);
    Insert(s);
    T.Build();
    while(m--){
        R(opt,l,r);
        switch(opt){
            case 1:
                T.Edit(l,r);
                break;
            case 2:
                W(T.Query(l,r),'\n');
                break;
        }
    }
    close_file();
    return 0;
}

T4

题面

思路

代码