uva 10780Again Prime? No Time.(簡單數論)

來源:互聯網
上載者:User

Again Prime? No time.
Input: standard input
Output: standard output
Time Limit: 1 second


The problem statement is very easy. Given a number n you have 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 is the number of cases to handle. The following lines are the cases each of which contains two integers m (1<m<5000) and n (0<n<10000). The integers are separated by an space. There will be no invalid cases given and there are not more that 500 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 mdivides n! or a line "Impossible to divide" (without the quotes). Check the sample input and output format.

 

Sample Input

2
2 10
2 100

Sample Output

Case 1:
8
Case 2:
97

 

題意:給你m,n求最大的k使得是n。的因子;

思路:把m質因子分解為,設p為pi中最大的素數,,然後求n!中p的個數即為為b,即為答案(對於i,求出相應的b,最小的)

代碼:

<pre name="code" class="cpp">#include <iostream>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<vector>#include<stdlib.h>#include<string>#include<map>#include<set>#include<queue>#include<stack>#include<set>#define inf 0x3f3f3f3f#define eps 1e-5#define max(a,b) (a)>(b)?(a):(b)#define min(a,b) (a)<(b)?(a):(b)using namespace std;int sum(int n,int p){    int x=p,ans=0;    while(n/p!=0)    {        ans+=n/p;        p*=x;    }    return ans;}int main(){    int t,n,m,i,j;    scanf("%d",&t);    {        for(i=1;i<=t;i++)        {            scanf("%d%d",&m,&n);            printf("Case %d:\n",i);            int ans=inf,pnum;            for(j=2;m>1;j++)            {                pnum=0;                while(m%j==0)                {                    pnum++;                    m/=j;                }                if(pnum)                {                    int tt=sum(n,j)/pnum;                    if(ans>tt)ans=tt;                }            }            if(ans)                printf("%d\n",ans);            else                printf("Impossible to divide\n");        }    }    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.