zoukankan      html  css  js  c++  java
  • BZOJ2118: 墨墨的等式(最短路 数论)

    题意

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

    Sol

    maya神仙题啊,感觉自己做题难度跨度太大了qwq。

    这里有一篇讲的非常好的博客https://blog.csdn.net/w4149/article/details/66476606?locationNum=3&fps=1

    思路大概就是 利用取余的性质,把能够构造出来的解表示成统一的形式

    发现该形式可以通过最短路更新

    然后就做完了。。

    #include<cstdio>
    #include<algorithm>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<cstring>
    #define lb(x) (x & (-x))
    #define Pair pair<int, int> 
    #define fi first
    #define se second
    #define MP(x, y) make_pair(x, y)
    #define LL long long 
    using namespace std;
    const int MAXN = 1e6 + 10;
    inline LL read() {
        char c = getchar(); LL x = 0, f = 1;
        while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
        while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); 
        return x * f;
    }
    int N, vis[MAXN];
    LL dis[MAXN], a[MAXN], Mi = 1e15, Bmin, Bmax;
    void SPFA() {
        queue<int> q;
        memset(dis, 0xf, sizeof(dis));
        dis[0] = 0; vis[0] = 1, q.push(0);
        while(!q.empty()) {
            int p = q.front(); q.pop(); vis[p] = 1;
            for(int i = 1; i <= N; i++) {
                int to = (p + a[i]) % Mi;//tag
                if(dis[to] > dis[p] + a[i]) {
                    dis[to] = dis[p] + a[i];
                    if(!vis[to]) vis[to] = 1, q.push(to);
                }
            }
        }
    }
    LL Query(LL x) {
        LL rt = 0;
        for(int i = 0; i < Mi; i++) 
            if(dis[i] <= x) 
                rt += (x - dis[i]) / Mi + 1;
        return rt;
    }
    int main() {
        N = read(); Bmin = read(); Bmax = read();
        for(int i = 1; i <= N; i++) {
            a[i] = read();
            if(a[i] != 0) Mi = min(Mi, a[i]);
        }
        SPFA();
        printf("%lld", Query(Bmax) - Query(Bmin - 1));
        return 0;
    }
    /*
    
    */
  • 相关阅读:
    javaBean的理解
    配置tomcat8数据源(采用局部数据源方式)
    windows下apache报os 10048错误
    Windows下Apache的下载安装启动停止
    java通过数据库连接池链接oracle
    java连接oracle数据库
    eclipse配置svn方法
    JAVA多线程中start方法与run方法区别
    java程序在没有java环境的电脑上执行的方法(关键词jar,exe)
    js监听不到组合键
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/9577484.html
Copyright © 2011-2022 走看看