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)