前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BZOJ2118: 墨墨的等式(最短路 数论)

BZOJ2118: 墨墨的等式(最短路 数论)

作者头像
attack
发布2018-09-17 15:36:34
3730
发布2018-09-17 15:36:34
举报

题意

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

Sol

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

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

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

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

然后就做完了。。

代码语言:javascript
复制
#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;
}
/*

*/
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-09-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档