Xtuoj ABK (finding out the number of major conventions on A and B)

Source: Internet
Author: User
Tags greatest common divisor square root

Accepted:21 submit:171
Time limit:1000 MS Memory limit:65536 KB
Title Description

ABK is a more simple topic than a+b, give two integers, a, B, to find out the number of the k conventions of A and C.

Input

The first line is an integer N (n≤10000) that represents the number of samples. For each subsequent line, a sample of 3 integers a,b,k (1≤a,b≤109, 1≤k≤10)

Output

Each line outputs an integer d, which indicates that the number of major K conventions of A/b is output-1 without the number of conventions of the K-large.


Ideas: First find out A and B gcd,a and B of the number of conventions must also be greatest common divisor, so the number of K conventions can be from 1 to GCD enumeration K, the GCD divided by the K small approximate is the K large approximate, but 10^9 the amount of data will be t. So can be from 1 to the root of the GCD enumeration, assuming that the name of N, if the n>=k will be found, if n<k, the remaining k-n must be in (gcd-square root gcd) within the range, can not be enumerated, otherwise t, to use good approximate nature, So it can still be obtained by gcd the square root to 1 Inverse enumeration k-n gcd, the complexity o (square root n).

The code is as follows:

 #include < iostream> #include <cstdio> #include <cstring> #include <string> #include <map> #include < algorithm> #include <set> #include <cmath> #include <vector>using namespace Std;int a,b,k;int gcd (    int A,int b) {if (b==0) return A; else return gcd (b,a%b);}    void Solve () {int mx=gcd (A, b), cnt=0;        for (int i=1;i*i<=mx;i++)//Forward enumeration if (mx%i==0) {cnt++;     if (cnt==k) cout<<mx/i<<endl;        } for (int i=floor (sqrt (MX) +0.5); i>=1;i--)//Reverse enumeration//floor in order to avoid error if (mx%i==0) {cnt++;        if (I*I==MX) cnt--;    Small details, if gcd's arithmetic square root is exactly an integer, the first I of the inverse enumeration and the last I of the forward enumeration repeat if (CNT==K) cout<<i<<endl; } if (cnt<k) cout<< "-1" <<ENDL;}   int main () {int _;   cin>>_;       while (_--) {cin>>a>>b>>k;   Solve (); } return 0;} 

Xtuoj ABK (finding out the number of major conventions on A and B)

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.