BZOJ 2118 墨墨的等式

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


【题目分析】
我们可以发现,其实就是有许多个物品,然后一些物品选出来(不限制个数),然后和为一个定值。可以考虑图论(一切问题皆图论),拆成%a[1]为0——a[1]-1的这些点,那么对于每一个ai,就可以在两个同余类之间连单向边,然后最短路算出每个点的最短路径长度,然后就可以用数学的方法直接统计答案了。
隔壁的SilverNebula貌似弄出了一种不需要建边的写法,速度快了5倍。%%%
不愧是山西第一,大家可以去看看他的代码,直接百度,传送门不设置了。


【代码】

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>

#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>

using namespace std;

#define maxn 6000005
#define inf (0x3f3f3f3f)
#define ll long long

int int_read()
{
    int 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;
}

ll ll_read()
{
    ll 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;
}

int h[maxn],to[maxn],ne[maxn],w[maxn],en=0;
int n;

int a[15];
ll dis[500005],bu,bd;
int inq[500005];
queue <int> q;

void add(int a,int b,int c)
{
    to[en]=b;
    ne[en]=h[a];
    w[en]=c;
    h[a]=en++;
}

void SPFA()
{
    memset(dis,0x3f,sizeof dis);
    memset(inq,0,sizeof inq);
    q.push(0);
    dis[0]=0; inq[0]=1;
    while (!q.empty())
    {
        int x=q.front(); q.pop(); inq[x]=0;
        for (int i=h[x];i>=0;i=ne[i])
        {
            if (dis[to[i]]>dis[x]+(ll)w[i])
            {
                dis[to[i]]=dis[x]+(ll)w[i];
                if (!inq[to[i]])
                {
                    inq[to[i]]=1;
                    q.push(to[i]);
                }
            }
        }
    }
//  for (int i=0;i<a[1];++i) printf("%lld ",dis[i]); printf("\n");
}

int main()
{
    memset(h,-1,sizeof h);
    n=int_read(); bd=ll_read(); bu=ll_read();
    for (int i=1;i<=n;++i) a[i]=int_read();
    sort(a+1,a+n+1);
    for (int i=2;i<=n;++i)
        for (int j=0;j<a[1];++j)
            add(j,(j+a[i])%a[1],a[i]);
    SPFA();
    ll ans=0;
    for (int i=0;i<a[1];++i)
    {
        ll tmp=max((ll)0,(ll)bd-(ll)dis[i])/(ll)a[1]*(ll)a[1]+(ll)dis[i];
//      printf("now tmp is %lld\n",tmp);
        while ((ll)tmp<(ll)bd) tmp+=(ll)a[1];
//      printf("now tmp is %lld\n",tmp);
        if ((ll)tmp>(ll)bu) continue;
        ans+=(ll)1+((ll)bu-(ll)tmp)/(ll)a[1];
//      printf("ans += %lld\n",(ll)1+((ll)bu-(ll)tmp)/(ll)a[1]);
    }
    printf("%lld\n",ans);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值