您的位置:首页 > 其它

BZOJ 2118 墨墨的等式

2016-12-10 21:12 337 查看
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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: