BestCoder Round #60/HDU 5505 暴力数学

GT and numbers

问题描述

给出两个数NN和MM。
NN每次可以乘上一个自己的因数变成新的NN。
求最初的NN到MM至少需要几步。
如果永远也到不了输出-1−1。

输入描述

第一行读入一个数TT表示数据组数。
接下来TT行,每行两个数NN和MM。
T\leq1000T≤1000, 1\leq N \leq 10000001≤N≤1000000,1 \leq M \leq 2^{63}1≤M≤2?63??.

注意M的范围。hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格。

输出描述

对于每组数据,输出一个数表示答案。

输入样例

3
1 1
1 2
2 4

输出样例

0
-1
1

题解:对n质数分解成x个指数因子,再对m进行整除到底,知道所得m为1为止,记录步数ans就是答案,否则-1;

///1085422276
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef unsigned long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a)  memset(a,-1,sizeof(a));
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)

#define inf 100000000
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<‘0‘||ch>‘9‘)
    {
        if(ch==‘-‘)f=-1;
        ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘)
    {
        x=x*10+ch-‘0‘;
        ch=getchar();
    }
    return x*f;
}
//***************************************
#define maxn 1000000+5
  bool P[maxn];
  int k;
  int  prime[maxn];
  vector<int  >V;
  int H[maxn],S[maxn];
  void init()
  {
      P[1]=1;
    for(int i=1;i<=1000001;i++)
      {
          if(!P[i])
          {
              for(int j=i+i;j<=1000001;j=j+i)
                P[j]=1;
          }
      }
      k=0;
      for(int i=1;i<=1000001;i++)
      {
          if(!P[i])
             prime[++k]=i;
      }
  }
 __int64 get( __int64 x,__int64 y)
  {
      __int64 j=1;
      while(y<x)
      {
          x-=y;
          j++;
          y+=y;
      }
      if(j==0)j++;
      return j;
  }
  int main()
  {
      int T;
      init();
      scanf("%d",&T);

      while(T--)
      {
          V.clear();
          ll n=read();
          ll m=read();
          if(n==m)
          {
              printf("0\n");
              continue;
          }
          if(n>m)
          {
            printf("-1\n");
              continue;
          }
          if(n==1)
          {
             printf("-1\n");
              continue;
          }
          if(m%n){
            cout<<-1<<endl;
            continue;
          }
          ll tmp=m/n;//cout<<tmp<<endl;
         for(int i=1;i<=k;i++)
         {
             while(n%prime[i]==0)
             {
                 if(H[prime[i]]==0)
                 V.push_back(prime[i]);
                 H[prime[i]]++;
                 n/=prime[i];
             }if(prime[i]>n)break;
         } // for(int i=0;i<V.size();i++)cout<<V[i]<<endl;
        __int64  ans=0;
         for(int i=0;i<V.size();i++)
         {
             if(tmp%V[i]==0) {
                 ll jj=0;
                 while(tmp%V[i]==0)
                 {
                     tmp/=V[i];
                     jj++;
                 }
                ans=max(ans,get(jj,H[V[i]]));
             }
             H[V[i]]=0;
         }
         if(tmp!=1){
            printf("-1\n");
         }
         else printf("%I64d\n",ans);

      }

      return 0;
  }

代码

时间: 2024-11-12 13:16:54

BestCoder Round #60/HDU 5505 暴力数学的相关文章

[BestCoder Round #4] hdu 4932 Miaomiao&#39;s Geometry (贪心)

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 92 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

[BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)

Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 743    Accepted Submission(s): 275 Problem Description Hanamichi is taking part in a programming contest, and he is assigned to so

Bestcoder round #65 &amp;&amp; hdu 5592 ZYB&#39;s Premutation 线段树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 74 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutat

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

hdu5504 GT and sequence(BestCoder Round #60 )

GT and sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 334 Accepted Submission(s): 85 Problem Description You are given a sequence of N integers. You should choose some numbers(at least o

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

[BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

Task schedule Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行. 机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求. Input 输入的第一行包含一个整数T, 表示一共有T组测试数据. 对于每组测试数据: 第一行是两个数字n, m,表示工作表里面有n个任务,

[BestCoder Round #4] hdu 4931 Happy Three Friends

Happy Three Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 70    Accepted Submission(s): 62 Problem Description Dong-hao , Grandpa Shawn , Beautful-leg Mzry are good friends. One day ,

HDU 5749 BestCoder Round #84 Colmerauer(暴力贡献)

Colmerauer Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 220    Accepted Submission(s): 94 Problem Description Peter has an n×m matrix M. Let S(a,b) be the sum of the weight all a×b submat