题解 AT2005 【Sequential operations on Sequence】
这题的思维要求好高啊qwq(可能是因为我太菜
如果后面的一个a比前面小,那前面那些比它大的显然是没有意义的,因此我们可以在读入时维护一个单调栈,使得最后的操作序列单调递增
考虑一个计算第i次操作过程。前面的一大堆都是上一次操作所得答案的若干倍,所以对于一个操作,只要单独处理a[i] mod a[i-1]那一部分的答案就好了。至于前面那些没用的,就给第i-1个操作加上一个系数,到时候操作它的时候答案乘上这个系数即可
那单独处理的这一部分,做法也是一样的。跳过前面比这一段长度更大的操作,找到第一个小于它长度的a[t],前面一大堆都是第t次操作的若干倍,所以直接给第t次操作加上一个系数,再处理(a[i] mod a[i-1]) mod a[t]即可。就这样不断递归下去,直到没有比它更小的了,此时直接给答案区间[1, len]加上当前操作的系数即可(len表示当前区间长度)。区间加法可以用差分实现,最后还原即可
那这个过程显然要倒着来做,要不然你都不知道当前操作的系数是多少
至于复杂度,递归时每次长度模一个数,那层数显然不超过log层。因为要二分找上面说的那个t,所以复杂度两个log
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
namespace IO
{
const int S=(1<<20)+5;
char buf[S],*H,*T;
inline char Get()
{
if(H==T) T=(H=buf)+fread(buf,1,S,stdin);
if(H==T) return -1;return *H++;
}
inline LL read()
{
LL x=0;char c=Get();
while(!isdigit(c)) c=Get();
while(isdigit(c)) x=x*10+c-'0',c=Get();
return x;
}
char obuf[S],*oS=obuf,*oT=oS+S-1,c,qu[55];int qr;
inline void flush(){fwrite(obuf,1,oS-obuf,stdout);oS=obuf;}
inline void putc(char x){*oS++ =x;if(oS==oT) flush();}
template <class I>inline void print(I x)
{
if(!x) putc('0');
while(x) qu[++qr]=x%10+'0',x/=10;
while(qr) putc(qu[qr--]);
putc('\n');
}
}
using namespace IO;
const int N=100010;
int n,m,top=0;
LL a[N],f[N];
LL delta[N];
void solve(LL x,LL y)
{
if(!x) return;
int t=upper_bound(a+1,a+1+top,x)-a-1;
if(!t) delta[1]+=y,delta[x+1]-=y;
else f[t]+=x/a[t]*y,solve(x%a[t],y);
}
int main()
{
a[++top]=n=read();m=read();
for(int i=1;i<=m;i++)
{
LL x=read();
while(x<=a[top]) top--;
a[++top]=x;
}
f[top]=1;
for(int i=top;i>=2;i--) f[i-1]+=a[i]/a[i-1]*f[i],solve(a[i]%a[i-1],f[i]);
delta[1]+=f[1];delta[a[1]+1]-=f[1];
for(int i=1;i<=n;i++) print(delta[i]+=delta[i-1]);
flush();
return 0;
}