题解
观察数据范围,就发现
很小,而其他很大,于是就可以根据这里来想。
将其他数换成
的形式。
可以知道只有出现了连续
个数在序列里面,
那么最大的数就是在这连续
的数前面一个数。
设
表示,用
来组成的数,在%
之后为i,最小在上面时候可以出现在数列中。
这样
就是%
=i第一次出现的时候。
连边就是t连向
%
,边权为
。如果这个数大于
,就取模,然后边权加一。
答案就是
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;
}