JZOJ5864. 【NOIP2018模拟9.11】很多序列

这里写图片描述
这里写图片描述

题解

观察数据范围,就发现 x 1 很小,而其他很大,于是就可以根据这里来想。
将其他数换成 a x 1 + b 的形式。
可以知道只有出现了连续 x 1 个数在序列里面,
那么最大的数就是在这连续 x 1 的数前面一个数。
f i 表示,用 x 2 , x 3 , . . . , x n 来组成的数,在% x 1 之后为i,最小在上面时候可以出现在数列中。
这样 f i x 1 + i 就是% x 1 =i第一次出现的时候。
连边就是t连向 t + x i % x 1 ,边权为 x i / x 1 。如果这个数大于 x 1 ,就取模,然后边权加一。
答案就是 ( m a x f i 1 ) x 1 + i

code

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string.h>
#include <cmath>
#include <math.h>
#define ll long long
#define P putchar
#define G getchar
using namespace std;
char ch;
void read(ll &n)
{
    n=0;
    ch=G();
    while((ch<'0' || ch>'9') && ch!='-')ch=G();
    ll w=1;
    if(ch=='-')w=-1,ch=G();
    while('0'<=ch && ch<='9')n=(n<<3)+(n<<1)+ch-'0',ch=G();
    n*=w;
}

ll n,a[13],ans,f[1000003],v[13],w;
int q[2000003],head,tail,b[13],x,t;
bool bz[1000003];

int main()
{
    freopen("sequence.in","r",stdin);
    freopen("sequence.out","w",stdout);

    read(n);
    for(int i=1;i<=n;i++)read(a[i]),b[i]=a[i]%a[1],v[i]=a[i]/a[1];
    if(n==2)
    {
        printf("%lld",(a[1]-1)*(a[2]-1)-1);
        return 0;
    }

    memset(f,127,sizeof(f));
    memset(bz,1,sizeof(bz));
    for(head=q[tail=1]=f[0]=bz[0]=0;head<tail;)
    {
        x=q[++head];
        for(int i=2;i<=n;i++)
        {
            t=x+b[i];w=f[x]+v[i];
            if(t>=a[1])w++,t=t-a[1];
            if(f[t]>w)
            {
                f[t]=w;
                if(bz[t])bz[q[++tail]=t]=0;
            }
        }
        bz[x]=1;
    }
    for(int i=0;i<a[1];i++)
        if(f[i]>=ans)ans=f[i],x=i;

    printf("%lld\n",(ans-1)*a[1]+x);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/lijf2001/article/details/82669597