zoukankan      html  css  js  c++  java
  • [图论训练]BZOJ 2118: 墨墨的等式 【最短路】

    Description

    墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N、{an}、以及B的取值范围,求出有多少B可以使等式存在非负整数解。

    Input

    输入的第一行包含3个正整数,分别表示N、BMin、BMax分别表示数列的长度、B的下界、B的上界。输入的第二行包含N个整数,即数列{an}的值。

    Output

    输出一个整数,表示有多少b可以使等式存在非负整数解。

    Sample Input

    2 5 10
    3 5

    Sample Output

    5

    HINT

    对于100%的数据,N≤12,0≤ai≤5*10^5,1≤BMin≤BMax≤10^12。

    思路:看起来是数论的题目,关键是对于一个ai 如果发现x是可行解,那么显然x+ai,x+2*ai,x+3*ai.....一直到BMax都是可行的恩 这是连续的 所以对于0 1 2 3 4 5 6 ...ai-1 那么我们分别记录它的最小的x使得x是个可行解,并且x mod ai == 0 1 2 3 ...ai-1 就可以不重不漏的找到所有解了

    至于怎么找,把0 1 2 3 4 5 6 ...ai-1 看成ai个点,向每个后面的点aj的边,由于非负 显然最短路就是我们寻找的x

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<queue>
    #include<algorithm>
    #define maxn 500009
    using namespace std;
    typedef pair<long long,int> pii;
    priority_queue<pii, vector<pii>, greater<pii> >q;
    int head[maxn],point[10000005],nex[10000005],now,a[maxn];
    int value[10000005];
    long long dist[maxn];
    long long read()
    {
        long long x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    void add(int x,int y,int v)
    {
        nex[++now] = head[x];
        head[x] = now;
        point[now] = y;
        value[now] = v;
    }
    void dijkstra(int s,int n)
    {
        for(int i=0;i<=n;i++)dist[i]=(long long)1e60;
        dist[s] = 0;
        int visit[maxn]={0};
        q.push(make_pair(0,s));
        while(!q.empty())
        {
            int u = q.top().second;
            q.pop();
            if(visit[u])continue;
            visit[u] = 1;
            for(int i = head[u];i;i=nex[i])
            {
                int k = point[i];
                if(dist[u]+value[i]<dist[k])
                {
                    dist[k] = dist[u] + value[i];
                    q.push(make_pair(dist[k],k));
                }
            }
        }
    }
    int main()
    {
        long long n=read(),bmin=read(),bmax=read(),ans=0;
        long long amin,aj=1;
        a[1] = read();
        amin = a[1];
        for(int i=2;i<=n;i++)
        {
            a[i] = read();
            if(a[i]<amin)
            {
                amin=a[i];
                aj=i;
            }
        }
        //printf("amin=%d
    ",amin);
        for(int i=0;i<amin;i++)
        {
            for(int j=1;j<=n;j++)if(j!=aj)
            {
                add(i,(i+a[j])%amin,a[j]);
            }
        }
        dijkstra(0,amin);
        //for(int i=0;i<amin;i++)printf("%d :  %lld
    ",i,dist[i]);
        for(int i=0;i<amin;i++)if(dist[i]<=bmax)
        {
            long long u = max(dist[i],(long long)bmin)-1;
            long long l = u / amin, r = bmax / amin;
            if(bmax % amin >= i)r++;
            if(u % amin >= i)l++;
            ans += r - l;
        }
        printf("%lld
    ",ans);
        return 0;
    }
  • 相关阅读:
    oracle常用hint的用法
    浅谈reverse函数与django哲学
    javascript console
    python os.path模块
    删除列表元素
    Python模块学习 pickle, cPickle 对象序列化/反序列化
    Python中zip()函数用法举例
    Python 字符串方法详解
    常用正则验证
    python中下划线的用法
  • 原文地址:https://www.cnblogs.com/philippica/p/4761614.html
Copyright © 2011-2022 走看看