HDU 5019 simple mathematical questions

Source: Internet
Author: User

Given a and B, this question is to calculate the approximate number of C.

The longest result is the maximum common divisor, that is, the common GCD algorithm. But now we want to ask for a common divisor in section C. we can imagine that if we make the common divisor in section C as X and the maximum common divisor as G, then what about x | G?

We can intuitively understand that the maximum common divisor is actually obtained by multiplying the common prime factor after the prime factor is decomposed by A and B respectively. For any common divisor A and B, it must include the prime factor of the largest common divisor. Therefore, X | G.

Therefore, we only need to enumerate the factors of G for the approximate number of C. We know that we can perform O (SQRT (N) for the factor of a number )) the N range of this question is 10 ^ 12, so the complexity is sufficient.

But for a long time, I haven't written such a question with a very tight time limit. I am a little bit confused about writing more or enumerating some content, so it is really powerless. Or you are too scum.

#include <iostream>#include <algorithm>#include <vector>#include <cstdio>#include <cstdlib>using namespace std;typedef __int64 LL;LL gcd(LL a, LL b){    if(b==0)        return a;    return gcd(b, a%b);}int main(){    LL a, b, c;    int T;    vector<LL> v;    scanf("%d", &T);    while(T--)    {        scanf("%I64d%I64d%I64d", &a, &b, &c);        LL temp = gcd(a, b);        v.clear();        int cnt = 0;        for(LL i=1; i*i<=temp; ++i)        {            if(temp%i==0)             {                 v.push_back(i);                 if(i*i!=temp)                    v.push_back(temp/i);             }        }        sort(v.begin(), v.end());        if(v.size() >= c)            printf("%I64d\n", v[v.size()-c]);        else            printf("-1\n");    }    return 0;}


HDU 5019 simple mathematical questions

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.