2118: Ink equation time limit:10 Sec Memory limit:259 MB
submit:1317 solved:504
[Submit] [Status] [Discuss] Description
Ink is interested in a sudden equivalence, he is studying the condition of a1x1+a2y2+...+anxn=b existence nonnegative integer solution, he asks you to write a program, given the range of N, {an}, and B, to find out how many B can make the equation existence nonnegative integer solution.
Input
The first line of the input contains 3 positive integers, representing N, Bmin, Bmax, respectively, the length of the sequence, the lower bound of B, and the upper bound of B. The second line of input contains n integers, which is the value of the sequence {an}.
Output
Outputs an integer that indicates how many B can cause a nonnegative integer solution to be present in the equation.
Sample Input2 5 10
3 5
Sample Output5
HINT
For 100% of data, n≤12,0≤ai≤5*10^5,1≤bmin≤bmax≤10^12.
sdsc2016 Morning Sister's courseware:
- First, the answer =ans (Bmax)-ans (Bmin-1)
- Finding the minimum value p from A1 to an, if you can construct an answer x, you can construct an answer x+p
- So we only need to calculate the smallest k for each B (0<=b<p), so that k*p+b can be constructed, then it can be constructed for K ' (K ' >k) K ' *p+b.
- So for each B build a point, for each AI, from B-to (B+ai)%p a length AI-side
model P and then build the diagram, goodpay attention to calculate the answer contribution there,/d[i] words have honey juice re
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<queue>using namespaceStd;typedefLong Longll;Constll n=5*1e5+5, inf=1e19;ll n;ll P=inf,a[ -];; ll Bmx,bmn,ans=0;structedge{ll V,w,ne;} E[n* the];ll h[n],cnt=0;voidins (ll u,ll v,ll W) {cnt++; E[CNT].V=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=CNT; //cnt++; //e[cnt].v=u;e[cnt].w=w;e[cnt].ne=h[v];h[v]=cnt;}voidbuildgraph () { for(LL i=0; i<p;i++) for(LL j=1; j<=n;j++){ if(a[j]==p)Continue; Ins (i, (i+A[J])%P,a[j]); //PRLLF ("ins%d%lld%lld\n", I, (I+a[j])%p,a[j]); }}structhn{ll U,d; BOOL operator< (ConstHN &RHS)Const{returnD>rhs.d;}}; ll D[n];BOOLDone[n];p riority_queueQ;voidDijkstra (ll s) { for(LL i=0; i<p;i++) d[i]=INF; D[s]=0; Q.push ((HN) {s,0}); while(!Q.empty ()) {HN x=q.top (); Q.pop (); ll u=x.u; if(Done[u])Continue; Done[u]=1; for(LL i=h[u];i;i=e[i].ne) {LL v=e[i].v; if(d[v]>d[u]+E[I].W) {D[v]=d[u]+E[I].W; Q.push ((HN) {v,d[v]}); } } }}intMain () {scanf ("%lld%lld%lld",&n,&bmn,&BMX); for(LL i=1; i<=n;i++) scanf ("%lld", &a[i]), p=min (p,a[i]); Buildgraph (); Dijkstra (0); for(LL i=0; i<p;i++){ if(D[I]>BMX)Continue; ll L=max (0LL, (Bmn-d[i])/p), r= (Bmx-d[i])/p; if(L*P+D[I]<BMN) l++; Ans+=r-l+1; } printf ("%lld", ans); return 0;}
BZOJ2118 ink equation [number theory shortest circuit modeling]