[AGC003]E - Sequential operations on Sequence

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;
}

转载于:https://www.cnblogs.com/dogcdt/p/8361736.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值