日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uva10780 - Again Prime? No time

發(fā)布時間:2023/11/30 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uva10780 - Again Prime? No time 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

uva10780 - Again Prime? No time

?

?

Again Prime? No time.


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?and?n (0. 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?m?divides?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

?

題目大意:給定mn,求一個最大正整數(shù)k,使得m^k能夠被n!整除,變相問n!中有多少個m;

?

思路:開始想到的是從2-n遍歷,把每個數(shù)中有多少的m都找出來,后來發(fā)現(xiàn)缺陷是m不是質(zhì)數(shù),例如4*96,就找不出有6,實(shí)際上應(yīng)該有兩個!正確的解法是分解質(zhì)因數(shù),然后找出有多少對質(zhì)因數(shù)即可。當(dāng)然要找的是對于每個質(zhì)數(shù)的最小值.

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() //打素數(shù)表
{
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) //分解素數(shù),從前往后找
{
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); //選出最小的素數(shù)比,也就是ans個素數(shù)對
}
printf("Case %d:\n",ca);
if (ans==-1) cout<<"Impossible to divide"<<endl;
else cout<<ans<<endl;
}

}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的uva10780 - Again Prime? No time的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。