题解:CF1575C Cyclic Sum

· · 题解

题目传送门:洛谷 || CF

水题解

提供一种非常简单的无脑做法。

题目分析

考虑一个合法的子串,一定是三种:

对于本身,是好算的。先考虑不跨越循环节的,对于原序列 a,记前缀和为 s,则需满足 s_r-s_{l-1} \equiv 0 \pmod k。这个是好算的,算出一个循环节的答案后乘 m 即可。

考虑跨越循环节的部分,此时可以化为一段后缀(不能为空)加几个(可以为空)原序列加一段前缀(可以为空)。但是我们只关心和模 k 的的值。所以我们预处理每一段非空后缀的和与任意多原序列和在每个余数时的方案数,将两个序列循环卷积。然后只要加入最后一段前缀就可以了。注意前缀和后缀重叠的情况应减去。

时间复杂度 O(k \log k)。

AC Code

// by wangyizhi
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//bool Mst;
using namespace std;
namespace wyzfastio
{
//#define usefio
#ifdef usefio
    namespace __getchar{const int bufsize=1<<20;char buf[bufsize<<1],*p1=buf,*p2=buf;inline char getchar(){return (p1==p2&&(p2=(p1=buf)+fread(buf,1,bufsize,stdin),p1==p2))?EOF:(*p1++);}}using __getchar::getchar;
    namespace __putchar{const int bufsize=1<<20;char buf[bufsize<<1],*p=buf;inline void putchar(const char ch){if(p-buf==bufsize) fwrite(buf,1,bufsize,stdout),p=buf;*p++=ch;}inline void flush(){fwrite(buf,1,p-buf,stdout);}}using __putchar::putchar;using __putchar::flush;
    namespace __flusher{struct Flusher{Flusher(){}~Flusher(){flush();}}flusher;}
#endif
    /*---input---*/
    inline void read(unsigned long long &x){char ch=getchar();unsigned long long res=0;while(ch<'0'||ch>'9')ch=getchar();while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res;}
    inline void read(unsigned int &x){char ch=getchar();unsigned int res=0;while(ch<'0'||ch>'9')ch=getchar();while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res;}
    inline void read(long long &x){char ch=getchar();long long f=1;unsigned long long res=0;while(ch<'0'||ch>'9'){if(ch=='-')f=-f;ch=getchar();}while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res*f;}
    inline void read(int &x){char ch=getchar();int f=1;unsigned res=0;while(ch<'0'||ch>'9'){if(ch=='-')f=-f;;ch=getchar();}while(ch>='0'&&ch<='9')res=(res<<3)+(res<<1)+ch-'0',ch=getchar();x=res*f;}
    inline void read(char &s){s=getchar();while(s==' '||s=='\n'||s=='\r') s=getchar();}
    inline int read(char *s){int i=0;char ch=getchar();while(ch==' '||ch=='\n'||ch=='\r') ch=getchar();while(!(ch==' '||ch=='\n'||ch=='\r'||ch==EOF)) s[++i]=ch,ch=getchar();s[i+1]='\0';return i;}
    inline void read(std::string &s){char ch=getchar();s.clear();while(ch==' '||ch=='\n'||ch=='\r') ch=getchar();while(!(ch==' '||ch=='\n'||ch=='\r'||ch==EOF)) s.push_back(ch),ch=getchar();}
    template<typename _Tp,typename ...Args>inline void read(_Tp &x,Args &...args){read(x),read(args...);}
    /*---output---*/
    inline void write(const unsigned long long x){if(x<10) putchar(x+'0');else write(x/10),putchar(x%10+'0');}
    inline void write(const long long x){unsigned long long t=x;if(x<0)putchar('-'),t=-x;write(t);}
    inline void write(const unsigned int x){if(x<10) putchar(x+'0');else write(x/10),putchar(x%10+'0');}
    inline void write(const int x){unsigned int t=x;if(x<0)putchar('-'),t=-x;write(t);}
    inline void write(const char x){putchar(x);}
    inline void write(const char *s){while(*s!='\0'&&*s!=EOF)putchar(*s),s++;}
    template<typename _Tp,typename ...Args>inline void write(_Tp x,Args ...args){write(x),write(args...);}
}
using namespace wyzfastio;
using ll=long long;
using ld=long double;
#define int ll
using pii=pair<int,int>;
const int N=(1<<20)+5,mod=1e9+7;
inline ll pr(ll x,const int M=mod){return x+(x<0)*M;}
inline ll rd(ll x,const int M=mod){return x-(x>=M)*M;}
inline ll qpow(ll a,ll b=mod-2,const int M=mod){ll r=1;for(;b;b>>=1,a=a*a%M)if(b&1)r=r*a%M;return r;}
const vector<int> mods={998244353,1004535809,469762049};
const int B=3;
int rev[N];
struct integer
{
    int a,b,c;
    integer(ll _a,ll _b,ll _c):a(_a%mods[0]),b(_b%mods[1]),c(_c%mods[2]){}
    integer(ll __num=0):a(__num%mods[0]),b(__num%mods[1]),c(__num%mods[2]){}
    int get(int mod)
    {
        ll x=1ll*((b-a)%mods[1]+mods[1])%mods[1]*qpow(mods[0],mods[1]-2,mods[1])%mods[1]*mods[0]+a;
        return (1ll*(c-x%mods[2]+mods[2])%mods[2]*qpow(1ll*mods[0]*mods[1]%mods[2],mods[2]-2,mods[2])%mods[2]*
            (1ll*mods[0]*mods[1]%mod)%mod+x)%mod;
    }
    integer& operator=(int x){return *this=integer(x);}
};
integer operator+(integer x,integer y)
{return integer((x.a+y.a)%mods[0],(x.b+y.b)%mods[1],(x.c+y.c)%mods[2]);}
integer operator-(integer x,integer y)
{return integer((x.a+mods[0]-y.a)%mods[0],(x.b+mods[1]-y.b)%mods[1],(x.c+mods[2]-y.c)%mods[2]);}
integer operator*(integer x,integer y)
{return integer(1ll*x.a*y.a%mods[0],1ll*x.b*y.b%mods[1],1ll*x.c*y.c%mods[2]);}
inline integer qpow(integer a,int b)
{
    if(!b) return 1;
    else if(b==1) return a;
    integer res=qpow(a,b>>1);
    if(b&1) return res*res*a;
    else return res*res;
}
vector<integer> pwB;
void init(int sz)
{
    assert(__builtin_popcount(sz)==1);
    int l=__lg(sz);
    for(int i=1;i<sz;i++) rev[i]=(rev[i>>1]>>1)|((i&1)<<(l-1));
    const integer t(qpow(B,(mods[0]-1)/sz,mods[0]),qpow(B,(mods[1]-1)/sz,mods[1]),qpow(B,(mods[2]-1)/sz,mods[2]));
    pwB.push_back(1);
    for(int i=1;i<=sz;i++) pwB.push_back(pwB.back()*t);
}
struct poly
{
    vector<integer> a;
    poly(int sz=1){assert(__builtin_popcount(sz)==1),a.resize(sz);}
    void resize(int sz=1){assert(__builtin_popcount(sz)==1),a.resize(sz);}
    int size(){return a.size();}
    integer &operator[](int pos){return a[pos];}
    void ntt(int op)
    {
        int n=size();
        for(int i=1;i<n;i++) if(i<rev[i]) swap(a[i],a[rev[i]]);
        for(int mid=1,len=2;mid<n;mid<<=1,len<<=1)
        {
            int t=n/len;
            for(int i=0;i<n;i+=len)
                for(int j=0;j<mid;j++)
                {
                    integer o=op>0?pwB[t*j]:pwB[n-t*j],x=a[i+j],y=o*a[i+j+mid];
                    a[i+j]=x+y,a[i+j+mid]=x-y;
                }
        }
        if(op<0)
        {
            integer t(qpow(n,mods[0]-2,mods[0]),qpow(n,mods[1]-2,mods[1]),qpow(n,mods[2]-2,mods[2]));
            for(int i=0;i<n;i++) a[i]=a[i]*t;
        }
    }
};
int a[N],c[N];
//bool Med;
signed main()
{
//  cerr<<"Memory Size: "<<abs((&Med)-(&Mst))/1024.0/1024<<" MB\n";
//  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//  freopen("in.in","r",stdin);
//  freopen("out.out","w",stdout);
    int n,m,k,sz=1,ans=0;
    read(n,m,k);
    while(sz<(k<<1)) sz<<=1;
    init(sz);
    poly f(sz),g(sz);
    for(int i=1;i<=n;i++) read(a[i]),f[a[i-1]]=f[a[i-1]]+1,a[i]=(a[i]+a[i-1])%k;
    for(int i=0;i<m;i++) g[1ll*a[n]*i%k]=g[1ll*a[n]*i%k]+1;
    f.ntt(1),g.ntt(1);
    for(int i=0;i<sz;i++) f[i]=f[i]*g[i];
    f.ntt(-1);
    for(int i=0;i<k;i++) c[i]=(f[i].get(mod)+f[i+k].get(mod))%mod;
    int sum=1ll*a[n]*m%k;
    for(int i=0;i<n;i++) c[a[i]]--,ans=(ans+c[a[i]])%mod,c[(sum+a[i])%k]++;
    write((1ll*ans*m+!sum)%mod,'\n');
    return 0;
}