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)