Time limit : 2sec / Memory limit : 256MB
Problem Statement
Snuke got an integer sequence from his mother, as a birthday present. The sequence has \(N\) elements, and the \(i\)-th of them is \(i\). Snuke performs the following \(Q\) operations on this sequence. The \(i\)-th operation, described by a parameter \(q_i\), is as follows:
- Take the first \(q_i\) elements from the sequence obtained by concatenating infinitely many copy of the current sequence, then replace the current sequence with those \(q_i\) elements.
After these \(Q\) operations, find how many times each of the integers \(1\) through \(N\) appears in the final sequence.
Constraints
- \(1≦N≦10^5\)
- \(0≦Q≦10^5\)
- \(1≦qi≦10^18\)
All input values are integers.
Input
The input is given from Standard Input in the following format:
\(N\;Q\)
\(q_1\)
\(...\)
\(q_Q\)
Output
Print N lines. The i-th line (1≦\(i\)≦\(N\)) should contain the number of times integer \(i\) appears in the final sequence after the \(Q\) operations.
Sample Input 1
5 3
6
4
11
Sample Output 1
3
3
3
2
0
Sample Input 2
10 10
9
13
18
8
10
10
9
19
22
27
Sample Output 2
7
4
4
3
3
2
2
2
0
0
题意
有一个数字串S,初始长度为n,是1 2 3 4 …… n。
有m次操作,每次操作给你一个正整数a[i],你先把S无穷重复,然后把前a[i]截取出来成为新的S。
求m次操作后,每个数字在S中出现的次数。
分析
发现如果\(a_i≥a_{i+1}\)那么\(a_i\)是无效的,把序列变成严格上升的
然后令f[i]为第i次操作后的序列在最终序列中出现多少次,有f[m]=1
整块整块的直接相乘
如果有边角料,可以发现,长为x边角料与序列前x是相等的
而所有序列除该位不存在以外,前x位都是相等的(\(a_i\)序列严格上升
那就找到一个最大的小于等于x的\(a_i\),对f[i]产生贡献,再用同样方式处理边角料
最后x<n
存s[x]表示序列1-i
在最终序列出现多少次
CODE
ps:中途变量混乱,n
m
num
难以分清
#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
int n,num,m;
ll a[101010];
ll f[101010],s[101010];
void sol(ll x,ll doe)
{
if (x<a[1])
{
s[x]+=doe;
return;
}
int l=1,r=n;
int y;
while (l<=r)
{
int mid=(l+r)/2;
if (a[mid]<=x)
{
y=mid;
l=mid+1;
}
else r=mid-1;
}
f[y]+=doe*(x/a[y]);
x=x%a[y];
if (x) sol(x,doe);
}
int main()
{
// freopen("E.in","r",stdin);
// freopen("E.out","w",stdout);
scanf("%d%d",&n,&m);
num=1;
a[1]=n;
ll x;
for (int i=1;i<=m;++i)
{
scanf("%lld",&x);
while (x<=a[num] && num)
--num;
++num;
a[num]=x;
}
m=n;
n=num;
f[n]=1;
for (int i=n-1;i>=1;--i)
{
f[i]+=f[i+1]*(a[i+1]/a[i]);
sol(a[i+1]%a[i],f[i+1]);
}
for (int i=a[1];i>=1;--i)
s[i]+=s[i+1];
for (int i=1;i<=a[1];++i)
s[i]+=f[1];
for (int i=1;i<=m;++i)
printf("%lld\n",s[i]);
return 0;
}