51nod 1352: Collection Count

Source: Internet
Author: User

1352 set total base time limit: 1 seconds space limit: 131072 KB score: 20 Difficulty: 3-level algorithm topic collection attention

Give N fixed set {1,n},{2,n-1},{3,n-2},..., {n-1,2},{n,1}. Find out how many sets are satisfied: The first element is a multiple of a and the second element is a multiple of B.

Tips:

For the second set of test data, the collection is: {1,10},{2,9},{3,8},{4,7},{5,6},{6,5},{7,4},{8,3},{9,2},{10,1}. The conditions are met by 2nd and 8th.


Input
Line 1th: An integer T (1<=t<=50000) that indicates how many sets of test data. Line 2-t+1: three integers per line n,a,b (1<=n,a,b<=2147483647)
Output
For each set of test data output one number represents the number of sets that satisfy the condition, which is a row.
Input example
25 2 410) 2 3
Output example
12

Obviously, it is necessary to satisfy the equation a*xx+b*yy=1+n. My idea is to use the extension Euclid to find the minimum value greater than 0 xx, take its remain=n-(xx) *a, and then use remain divided by A, b least common multiple can be.

Feel 51nod on the topic for algorithm optimization requirements are very high, many times a careless out of the results of tle more than WA, so many places have to pay attention to the algorithm of time AH.

Code:

#include <iostream> #include <vector> #include <algorithm>using namespace Std;long long N,a,b,result, D,z,xx,yy;void EX_GCD (Long long A,long long B,long long &xx,long long &yy) {if (b==0) {xx=1;yy=0;d=a;} ELSE{EX_GCD (B,A%B,XX,YY); Long long t=xx;xx=yy;yy=t-(A/b) *yy;}} Long Long Cal2 () {RESULT=0;EX_GCD (A,B,XX,YY); Z=a*b/d;if ((1+n)%d) return 0;else{xx=xx* ((1+n)/d); long long r=b/d;xx = (xx %r+r)%r;if (xx==0) Xx+=r;long long remain=n-(xx) *a;if (remain<0) return 0;else{result++;result + = remain/z;} return result;} int main () {int count;scanf ("%d", &count), while (count--) {scanf ("%lld%lld%lld", &n,&a,&b);cout< <cal2 () <<endl;} return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

51nod 1352: Collection Count

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.