Test instructions: Gives x,y,l,r, how many numbers in [l,r] can be linearly combined by x, Y. Example X=4,y=5,l=7,r=13, then x, y can be combined out 8,9,10,12,13
Solution: The number of all x, y linear combinations can be converted into intervals [x,y],[2x,2y],[3x,3y] ...
From [kx,ky] start interval overlap, then (k+1) x<=ky calculate the interval before k,k is separated
Number of numbers in the [l,r] interval that are combined by x, y linearly before the numbers can be converted to f[r]-f[l-1],f[x]
#include <string.h> #include <algorithm> #include <iostream>using namespace Std;typedef long ll; LL Sox (ll X,ll y,ll Z,ll k) {ll tempx=0,tempy=z; while (tempx<=tempy)//Find the first ky>z interval {ll m= (tempx+tempy)/2; if ((double) m*y) >=z) tempy=m-1; else tempx=m+1; } if (tempx<=k) return (y-x) *tempx* (tempx-1)/2+ (tempx-1) + (tempx*x>z?0:z-tempx*x+1); The interval is calculated as before K: (y-x+1) + (2y-2x+1) +...+ ((k-1) y (k-1) x+1), followed by z-kx else return (y-x) *k* (k-1)/2+ (k-1) + (k*x>z?0:z-k *x+1);} ll FFF (ll X,ll y,ll L,ll R) {ll t=y/(y-x); Locate the (k+1) x<=ky interval if (y% (y-x)) t++; Return Sox (X,Y,R,T-1)-sox (x,y,l-1,t-1);} int main () {ll x,y,l,r; while (Cin>>x>>y>>l>>r) {if (r<x)//excludes two special cases {COUT<< ;0<<endl; Continue } if (x==y) {cout<<r/x-(L-1)/x<<endl; Continue } cout<<fff (X,y,l,r) <<endl; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
URAL 1720 Summit Online Judge