Number theory +SPFA algorithm Bzoj equation of 2118 ink ink

Source: Internet
Author: User

2118: Ink equation time limit:10 Sec Memory limit:259 MB
submit:1283 solved:496
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.

/* I didn't think it was the shortest one at first. The topic can change this way: N items, can use 0-, positive infinity, ask [l,r] how much value in the interval can be pooled out. Contact the shortest road surface: Choose a ai>0, if a value k∗ai+x (0≤x<ai,k≥0) can be pooled out, then obviously (k+1) ∗ai+x, (k+2) ∗ai+x,... Can be gathered out (so that the range of X is less than the AI) obviously if we find the smallest k to meet the k∗ai+x of each x can be gathered out, this problem solved, if the minimum cost to meet the x is greater than B, then can not be in the [l,r] interval mn*k+x, this number, Otherwise, calculate the number of [l,r] can be gathered out. Shortest circuit, SPFA time complexity O (n∗ai∗log2ai) because the complexity is related to AI, so we choose the smallest AI, for example: When the smallest AI equals 1 o'clock, all the numbers within the natural range can be pooled. */

1 /*Online AC code, I added the note, notice to change the i64d to LLD*/2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <algorithm>6#include <cmath>7 8 #defineMd9 #definell Long LongTen #defineINF 1000000000000000LL One #defineEPS 1e-8 A #defineN 500010 - using namespacestd; - intQ[n]; the ll Dis[n]; - BOOLVis[n]; - intMn,n; - inta[ -]; + voidSPFA () - { +     intH=0, w=1, X, y; q[1]=0; vis[0]=1;/*the first number that can be pooled is 0.*/  A      while(h!=W) at     { -h++;if(h>mn+5) h=1; X=Q[H];/*loop queue, take out the number of team heads*/ -          for(intI=1; i<=n;i++) -         { -Y= (X+a[i])%mn;/*using Y, which can be achieved with this value and other value combinations, calculates the minimum cost of Y (because only the minimum cost is calculated) in order to use MN to make more of the number that satisfies the interval condition.*/ -             if(dis[y]>dis[x]+A[i]) in             { -dis[y]=dis[x]+A[i]; to                 if(!Vis[y]) +                 { -vis[y]=1; thew++;if(w>mn+5) w=1; q[w]=y; *                 } $             }Panax Notoginseng         } -vis[x]=0; the     } + } A      the ll Query (ll x) + { -ll ans=0; $      for(intI=0; i<mn;i++) $         if(dis[i]<=x) ans+= (X-dis[i])/mn+1;/*calculate how many K satisfies k*mn+i<=x, because k>=0, so also add 1*/ -     returnans; - } the          - /*windows with I64d Linux with LLD*/    Wuyi intMain () the { -mn=(1e9); Wu ll L,r; -scanf"%d%i64d%i64d",&n,&l,&R); About      for(intI=1; i<=n;i++) {scanf ("%d", &a[i]);if(a[i]==0) {i--; n--;Continue;} Mn=min (Mn,a[i]);}/*Take out the smallest an, but not 0, well understood.*/ $      for(intI=1; i<mn;i++) Dis[i]=inf;/*the minimum cost for each k*mn+i (I<MN) is set, so that only I can be less than MN in the array dis (*/ - SPFA (); -printf"%i64d\n", query (R)-query (l1)); -     return 0; A}

1 /*2 First, the answer =ans (Bmax)-ans (Bmin-1)//Use differential3 finding the Minimum value p from A1 to an, if you can construct an answer x, you can construct an answer x+p4 so we only need to calculate the smallest k for each q (0<=q<p), so that k*p+q can be constructed, so it can be constructed for K ' (K ' >k) K ' *p+q .5 so for each Q to build a point, for each AI, from Q to (Q+ai)%p with a length of the AI side, first run the shortest way, calculate the minimum cost of each q, if the minimum cost is greater than Bmax, then there is no way to get out. Otherwise you can figure out how many. 6 7 */8 #defineN 159 #defineS 500010//pay attention to the problem when 5*1e5Ten#include <iostream> One using namespacestd; A#include <cstdio> -#include <queue> -typedefLong Longll; the ll L,r; - BOOLvis[s]={0}; - intN,mn= (1<< to)-1, a[n]; - ll Dis[s]; + voidinput () - { +Cin>>n>>l>>R; A      for(intI=1; i<=n;++i) at     { -scanf"%d",&a[i]); -         if(a[i]==0) -         { -i--;n--; -             Continue; in          }  -mn=min (mn,a[i]); to     } + } - voidSPFA () the { *queue<int>Q; $Q.push (0);Panax Notoginsengvis[0]=true; -dis[0]=0;/*Note that getting 0 of the cost is 0.*/ the     intx, y; +      while(!q.empty ()) A     { thex=Q.front (); Q.pop (); +vis[x]=false; -          for(intI=1; i<=n;++i) $         { $Y= (X+a[i])%mn; -             if(dis[y]>dis[x]+A[i]) -             { thedis[y]=dis[x]+A[i]; -                 if(!Vis[y])Wuyi                 { thevis[y]=true; - Q.push (y); Wu                 } -             } About         } $     } - } - ll Query (ll x) - { All ans=0; +      for(intI=0; i<mn;++i)/*don't forget to cycle from 0, because it's 0, you can use MN to get it all together.*/ the       if(dis[i]<=x) ans+= (X-dis[i])/mn+1; -     returnans; $ } the intMain () the { the input (); the      for(intI=0; i<mn;++i) -DIS[I]=100000000000000000LL;//when assigning a number of longlong, the suffix ll (uppercase and lowercase) is allowed, otherwise it will be wrong.  in SPFA (); theCout<<query (R)-query (l1); the     return 0; About}

Number theory +SPFA algorithm Bzoj equation of 2118 ink ink

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.