hdu-5505 (number theory)

Source: Internet
Author: User

Topic Links:

GT and Numbers

Time limit:2000/1000 MS (java/others)

Memory limit:65536/65536 K (java/others)


problem DescriptionYou are given and numbersNandM.

Every step you can get a newNIn the to that multiplyNby a factor ofN.

Work out how many steps canN is equal to M at least.

If N can ' t is to M forever,print −1.

InputIn the first line there is a numberT.Tis the test number.

In the nextTLines there is numbersNandM.

T≤ ,1≤N≤1000000 ,1≤M≤2.

Be careful to the range of M.

You ' d better print the ' Enter ' and ' last line ' hack others.

You ' d better not print space in the last of all line when you hack others.

OutputFor each test case,output an answer.

Sample Input 31 11 22 4

Sample Output 0-11 Test Instructions:Ask a number n, each time multiply its factor, ask at least how many times can become m; Ideas:The n,m quality factor decomposition, if the M can be obtained from N, then the decomposition of M of the mass factor and N is the same as the mass factor, and the number of each factorization is not less than n; otherwise it cannot be obtained by n ;the minimum number of steps is of course for each factorization, greedy to get the corresponding mass in M number, the final answer is that all the number of factorization into the number of m requirements of the largest one; already mentally retarded to not write scanf; AC Code:
//#include <bits/stdc++.h>#include<iostream>#include<queue>#include<cmath>#include<map>#include<cstring>#include<algorithm>#include<cstdio>using namespacestd;#defineRiep (n) for (int i=1;i<=n;i++)#defineRIOP (n) for (int i=0;i<n;i++)#defineRJEP (n) for (int j=1;j<=n;j++)#defineRJOP (n) for (int j=0;j<n;j++)#defineMST (SS,B) memset (ss,b,sizeof (ss));typedef unsignedLong LongLL;ConstLL mod=1e9+7;Const DoublePi=acos (-1.0);Const intinf=0x3f3f3f3f;Const intn=1e6+8; template<classT>voidRead (t&num) {    CharCH;BOOLf=false;  for(Ch=getchar (); ch<'0'|| Ch>'9'; f= ch=='-', ch=GetChar ());  for(num=0; ch>='0'&&ch<='9'; num=num*Ten+ch-'0', ch=GetChar ()); F&& (num=-num);}intstk[ -], Tp;template<classT> Inlinevoidprint (T p) {if(!p) {Puts ("0");return; }  while(p) stk[++ TP] = p%Ten, p/=Ten;  while(TP) Putchar (stk[tp--] +'0'); Putchar ('\ n');}intPrime[n];structnode{inta[9],m;} Po[n];voidInit () { for(intI=1; i<n;i++) {po[i].m=0; }     for(intI=2; i<n;i++)    {        if(!Prime[i]) {PO[I].A[PO[I].M++]=i;  for(intj=2*i;j<n;j+=i) {po[j].a[po[j].m++]=i; PRIME[J]=1; }}}}ll Check (intx) {LL s=1;  for(intI=1; i<=x;i++) {s*=2; }    returns;}intGetans (intXinty) {    intL=0, R= (int) (Log (y/x+1)/log (2.0))+1;  while(l<=r) {intMid= (l+r) >>1; if(Check (mid) *x>=y) r=mid-1; ElseL=mid+1; }    returnr+1;}voidSolveintn,ll m) {            if(n==1)            {                if(m==1) printf ("0\n"); Elseprintf"-1\n"); }            Else            {                intans=0; inttempn=N; LL TEMPM=m;  for(intI=0; i<po[n].m;i++)                {                    intnum1=0;  while(tempn%po[n].a[i]==0) {TEMPN/=Po[n].a[i]; NUM1++; }                    intNum2=0;  while(tempm%po[n].a[i]==0) {tempm/=Po[n].a[i]; Num2++; }                    if(num2<NUM1) {printf ("-1\n"); return ; } ans=Max (Ans,getans (num1,num2)); }                if(tempm>1) printf ("-1\n"); Elseprintf"%d\n", ans); }}intMain () {Init (); intT; scanf ("%d",&T); intN;        LL m;  while(t--) {read (n); scanf ("%i64u",&m);        Solve (n,m); }    return 0;}

hdu-5505 (number theory)

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.