HDU 5505 GT and Numbers (GCD magic)

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: Give two numbers n and M, n multiplied by its factor into a new value, the minimum number of times can be changed to M.

Idea: Each time multiplied by the integer v has two requirements: 1. It is the factor of N; 2. It should be as large as possible.

And because if n can finally reach m, it must be multiplied by the K times N, so as long as n can be divided by M, then each fetch GCD (n, m/n) on the line.

See the code for details:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef unsigned long long ll;const double PI = ACOs ( -1.0); const double EPS = 1e-6;const int mo D = 1000000000 + 7;const int INF = 1000000000;const int maxn = 100;int t;ll n,m;ll gcd (ll A, ll b) {return b = = 0? A : gcd (b, a%b);}    int main () {scanf ("%d", &t);        while (t--) {scanf ("%i64u%i64u", &n,&m);        int ans = 0;        if (n > M | | m% n! = 0 | | n = = 1 && m > 1) {printf (" -1\n");                } else {while (n! = m) {ll v = gcd (n,m/n);                if (v = = 1) break;   n *= v;             ++ans;            } if (n = = m) printf ("%d\n", ans);        else printf (" -1\n"); }} return 0;}


HDU 5505 GT and Numbers (GCD magic)

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.