Total 51nod 1352 episodes (extended Euclidean)

Source: Internet
Author: User

Topic Link: Portal

Test instructions: Slightly

Analysis:

It's easy to get an equation a*x + b*y = N + 1

This equation can be extended gcd to find gcd,x and Y, and then we find a minimum of more than 0 x,a*x the first satisfies the set Firstset, the remainder of the N-firstset set can be directly in addition to the LCM (A, B) (A and a least common multiple) statistics of the quantity.

The code is as follows:

#include <stdio.h> #include <string.h> #include <iostream> #define LL long longusing namespace std;    ll EXGCD (ll A, ll B, LL &x, ll &y) {ll r,t;        if (b==0) {x=1;        y=0;    return A;    } r=exgcd (B,a%b,x,y);    T=x;    X=y;    Y=t-a/b*y; return r;}    int main () {int t;    scanf ("%d", &t);        while (t--) {LL ans = 0;        LL N, A, B;        LL Xx,yy,d,r;        scanf ("%i64d%i64d%i64d", &n, &a, &b);        D=EXGCD (A, B, XX, yy);        if ((((N + 1)% B)% d! = 0) ans = 0;            else {LL LCM = A * B/D;            xx = xx * (((N + 1)% B)/d);            R = b/d;            xx= (xx% r + R)% R;            if (xx = = 0) {xx = lcm/a;                } if (XX * A > N) {ans = 0;                printf ("%i64d\n", ans);            Continue            } ans + = ((N-XX * A)/LCM);        ans++;    } printf ("%i64d\n", ans); } return 0;} 


Total 51nod 1352 episodes (extended Euclidean)

Related Article

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.