Uva10780-again Prime? No time

Source: Internet
Author: User

uva10780-again Prime? No Time

Again Prime? No time.


The problem statement is very easy. Given a number n you had to determine the largest power of M., not necessarily prime, that divides n !.

Input

The input file consists of several test cases. The first line in the file was the number of cases to handle. The following lines is the cases each of the which contains the integers m (1 and N (0. The integers is separated by a space. There'll be no invalid cases given and there is not more that 's test cases.

Output

For each case in the input, print the case number and result in separate lines. The result is either an integer if m divides n! or a line "impossible to divide" (without the Q Uotes). Check the sample input and output format.

Sample Input

2
2 10
2 100

Sample Output

Case 1:
8
Case 2:
97

The main topic: given M and N, to find a maximum positive integer k, so that m^k can be divisible by n!, in disguise to ask N. The number of M in the

Idea: Began to think of is from the 2-N traversal, the number of each of the m are found out, and later discovered that the defect is m not prime, such as 4*9 and 6, can not find 6, there should actually be two. The correct solution is to decompose the factorization, and then find out how many pairs of factorization can be. Of course, the minimum value for each prime number is to be found. Code

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace Std;
const int M=0X7FFFFFFF;
const int n=10010;

BOOL Ft[n];
int prime[n],t=0;
void init ()//Prime list
{
memset (ft,true,sizeof (ft));
Ft[0]=ft[1]=false;
for (int i=2;i<n;i++)
{
if (!ft[i]) continue;
Prime[++t]=i;
for (int j=2*i;j<n;j+=i)
Ft[j]=false;
}
}
int main ()
{
Init ();
int T;
int m,n;
int i,j;
scanf ("%d", &t);
for (int ca=1;ca<=t;ca++)
{
scanf ("%d%d", &m,&n);
int ans=m,p=1;
while (m>1)
{
int ct=0;
while (m%prime[p]==0)//decomposition prime, from the go after
{
ct++;
M/=PRIME[P];
}
if (ct==0) {p++; continue;}
if (prime[p]>n) {ans=-1; break;}
int nt=0;
for (I=prime[p];i<=n;i*=prime[p])
nt+=n/i;
Ans=min (ANS,NT/CT); Select the smallest prime ratio, which is the number of ans primes
}
printf ("Case%d:\n", CA);
if (ans==-1) cout<< "impossible to divide" <<endl;
else cout<<ans<<endl;
}

}

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.