URAL 1720 Summit Online Judge

Source: Internet
Author: User

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&LT;&LT            ;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

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.