3 1 1 1 2 2 4
0 -1 1
如果A大于B那么显然无解。
考虑把A和B分解质因数。
若B存在A没有的质因数也显然无解。
对于某一个A的质因数的次数。为了加速接近B,它一定是每次翻倍,最后一次的时候把剩下的加上。
那么答案就是最小的k使得2?k???A?num??≥B?num??。
最后把每个质因数的答案max起来即可。(B可以是2^63,这样就得用unsigned long long了,这是个坑点)
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm> using namespace std; const double eps = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int MOD = 1000000007; #define ll long long #define CL(a) memset(a,0,sizeof(a)) unsigned ll n,m; ll gcd(ll a, ll b) { if(a%b) return gcd(b, a%b); return b; } int main() { int T; cin>>T; while(T--) { cin>>n>>m; int ans=0; while(n!=m) { if(m%n){cout<<"-1"<<endl; break;} ll k=gcd(m/n, n); if(k==1){cout<<"-1"<<endl; break;} n*=k; ans++; } if(n==m) cout<<ans<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdu5505 GT and numbers(BestCoder Round #60)
原文:http://blog.csdn.net/d_x_d/article/details/49227637