ISIJ 2018 很多序列(Training Round D4T3) 最短路+数学

版权声明:虽然是个蒟蒻但是转载还是要说一声的哟 https://blog.csdn.net/jpwang8/article/details/82660425

Description


给定n个递增正整数,求不能由这些数字线性组合表示出的最大正整数
其中 x 1 10 6 n x 2 10 11 + n x n 12 + n g c d ( x 1 , x 2 ) = 1

Solution


noip要是考成这样真得回家种田了⊙﹏⊙∥
注意到n=2的情况就是NOIP2017D1T1的数学题
我们发现x1很小,考虑简化剩余系来做。
建立x1个点,对于第a个点向 ( a + x i ) % x 1 连权值为 x i 的边跑最短路
考虑dis[a]的含义,显然表示模x1余a的最小能组成的数字。于是最大的dis[a]-x1就是答案了

Code


#include <stdio.h>
#include <string.h>
#include <queue>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define drp(i,st,ed) for (int i=st;i>=ed;--i)

typedef long long LL;
const LL INF=1000000000000000000;
const int N=2000005;

bool vis[N];

LL dis[N];
LL a[7],wjp;

int spfa(int n) {
    std:: queue <int> que;
    rep(i,1,a[1]) dis[i]=INF;
    que.push(0); vis[0]=true;
    for (;!que.empty();) {
        int now=que.front(); que.pop();
        rep(i,2,n) {
            int tar=(a[i]%a[1]+now)%a[1];
            if (dis[now]+a[i]<dis[tar]) {
                dis[tar]=dis[now]+a[i];
                if (!vis[tar]) {
                    que.push(tar); vis[tar]=true;
                }
            }
        } vis[now]=false;
    }
    LL ans=0;
    rep(i,0,a[1]) if (dis[i]!=INF) ans=std:: max(ans,dis[i]-a[1]);
    printf("%lld\n", ans);
}

int main(void) {
    freopen("sequence.in","r",stdin);
    freopen("sequence.out","w",stdout);
    int n; scanf("%d",&n);
    rep(i,1,n) scanf("%lld",&a[i]);
    if (n==2) {
        printf("%lld\n", a[1]*a[2]-a[1]-a[2]);
        return 0;
    }
    spfa(n);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jpwang8/article/details/82660425