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

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

题解

观察数据范围,就发现 x1 x 1 很小,而其他很大,于是就可以根据这里来想。
将其他数换成 ax1+b a x 1 + b 的形式。
可以知道只有出现了连续 x1 x 1 个数在序列里面,
那么最大的数就是在这连续 x1 x 1 的数前面一个数。
fi f i 表示,用 x2,x3,...,xn x 2 , x 3 , . . . , x n 来组成的数,在% x1 x 1 之后为i,最小在上面时候可以出现在数列中。
这样 fix1+i f i ∗ x 1 + i 就是% x1 x 1 =i第一次出现的时候。
连边就是t连向 t+xi t + x i % x1 x 1 ,边权为 xi/x1 x i / x 1 。如果这个数大于 x1 x 1 ,就取模,然后边权加一。
答案就是 (maxfi1)x1+i ( 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值