AtCoder Grand Contest 003 E - Sequential operations on Sequence

题目传送门:https://agc003.contest.atcoder.jp/tasks/agc003_e

题目大意

一串数,初始为\(1\sim N\),现有\(Q\)个操作,每次操作会把数组长度变成\(L_i\),多余的长度直接截断;长度不够则循环填充,问最后\(1\sim N\)每个数的出现次数


首先维护一个单调递增的栈,因为较短的\(L_i\)可以让较长的\(L_{i'}\)失去其意义

然后我们倒推,对于一个\(L_i\),它能对\(L_{i-1}\)产生\(F_i×\lfloor\dfrac{L_i}{L_{i-1}}\rfloor\)的贡献,那么剩余的\(L_i\%L_{i-1}\),我们可以递归处理,找到最大的小于其的\(L_j\),按同样的方法处理即可

记得使用差分

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
	int x=0,f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<3)+(x<<1)+ch-'0';
	return x*f;
}
inline int read(){
	int x=0,f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<3)+(x<<1)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=1e5;
ll stack[N+10],f[N+10],delta[N+10];
int n,m,top;
void solve(ll x,ll v){
	int tmp=upper_bound(stack+1,stack+1+top,x)-stack-1;
	if (!tmp){
		delta[1  ]+=v;
		delta[x+1]-=v;
	}else	f[tmp]+=v*(x/stack[tmp]),solve(x%stack[tmp],v);
}
int main(){
	n=read(),m=read();
	stack[++top]=n;
	for (int i=1;i<=m;i++){
		ll x; scanf("%lld",&x);
		while (top&&x<=stack[top])	top--;
		stack[++top]=x;
	}f[top]=1;
	for (int i=top;i>1;i--)	f[i-1]+=f[i]*(stack[i]/stack[i-1]),solve(stack[i]%stack[i-1],f[i]);
	delta[1]+=f[1],delta[stack[1]+1]-=f[1];
	for (int i=1;i<=n;i++)	printf("%lld\n",delta[i]+=delta[i-1]);
	return 0;
}
posted @ 2018-12-18 16:36  Wolfycz  阅读(143)  评论(0编辑  收藏  举报