Harry Potter and the Hide Story

Problem Description
iSea is tired of writing the story of Harry Potter, so, lucky you, solving the following problem is enough.



 
Input
The first line contains a single integer T, indicating the number of test cases.

Each test case contains two integers, N and K. 



Technical Specification



1. 1 <= T <= 500

2. 1 <= K <= 1 000 000 000 000 00

3. 1 <= N <= 1 000 000 000 000 000 000
 
Output
For each test case, output the case number first, then the answer, if the answer is bigger than 9 223 372 036 854 775 807, output “inf” (without quote).
 
Sample Input
2
2 2
10 10
 
Sample Output
Case 1: 1
Case 2: 2
 
Author
iSea@WHU
 
Source
 
Recommend
 

题目大意:

给定 n和k , 求 n! % k^i 等于0时,i 的最大取值是多少?

解题思路:

将 k分解质因素。n也依据k的质因素求出关系限制i,最后算出最大的i就可以。

解题代码:

#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#include <cstring>
using namespace std; typedef unsigned long long ll;
ll n,k; const int maxn=10000010;
bool isPrime[maxn];
vector <ll> v;
ll tol; void get_prime(){
tol=0;
memset(isPrime,true,sizeof(isPrime));
for(ll i=2;i<maxn;i++){
if(isPrime[i]){
tol++;
v.push_back(i);
}
for(ll j=0;j<tol && i*v[j]<maxn;j++){
isPrime[i*v[j]]=false;
if(i%v[j]==0) break;
}
}
//for(ll i=v.size()-1;i>=v.size()-100;i--) cout<<v[i]<<endl;
} map <ll,ll> getPrime(ll x){
map <ll,ll> mp;
for(ll i=0;i<tol && x>=v[i];i++){
while(x>0 && x%v[i]==0){
x/=v[i];
mp[v[i]]++;
}
}
if(x>1) mp[x]++;
return mp;
} void solve(){
if(k==1){
printf("inf\n");
return;
}
map <ll,ll> mp=getPrime(k);
ll ans=1e19;
for(map <ll,ll>::iterator it=mp.begin();it!=mp.end();it++){
ll tmp=n,sum=0;
while(tmp>0){
sum+=tmp/(it->first);
tmp/=(it->first);
}
if(sum/(it->second)<ans) ans=sum/(it->second);
}
cout<<ans<<endl;
} int main(){
get_prime();
int t;
scanf("%d",&t);
for(int i=0;i<t;i++){
cin>>n>>k;
printf("Case %d: ",i+1);
solve();
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)的更多相关文章

  1. HDU3988-Harry Potter and the Hide Story(数论-质因数分解)

    Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 ...

  2. Harry Potter and the Hide Story(hdu3988)

    Harry Potter and the Hide Story Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 ...

  3. HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)

    Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/ ...

  4. hdu 3986 Harry Potter and the Final Battle (最短路径)

    Harry Potter and the Final Battle Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/6553 ...

  5. hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割

    view code//hdu 3987 #include <iostream> #include <cstdio> #include <algorithm> #in ...

  6. 【Dijstra堆优化】HDU 3986 Harry Potter and the Final Battle

    http://acm.hdu.edu.cn/showproblem.php?pid=3986 [题意] 给定一个有重边的无向图,T=20,n<=1000,m<=5000 删去一条边,使得1 ...

  7. hdu 3986 Harry Potter and the Final Battle

    一个水题WA了60发,数组没开大,这OJ也不提示RE,光提示WA...... 思路:先求出最短路,如果删除的边不是最短路上的,那么对结果没有影响,要有影响,只能删除最短路上的边.所以枚举一下最短路上的 ...

  8. HDU Stealing Harry Potter's Precious(状压BFS)

    状压BFS 注意在用二维字符数组时,要把空格.换行处理好. #include<stdio.h> #include<algorithm> #include<string.h ...

  9. hdu 3982 Harry Potter and J.K.Rowling (半平面交 + 圆与多边形交)

    Problem - 3982 题意就是给出一个圆心在原点半径为R的圆形蛋糕,上面有一个cherry,对蛋糕切若干刀,最后要求求出有cherry的那块的面积占整个蛋糕的多少. 做法显而易见,就是一个半平 ...

随机推荐

  1. Linux目录文件详解FHS标准(2013.09.05)

    Linux 目录配置的依据FHS(Filesystem Hierarchy Standard)标准,将目录分成为四种交互作用的形态: 四种形态的具体解释: 可分享的:可以分享给其他系统挂载使用的目录, ...

  2. Unix Domain Socket 域套接字实现

    主要注意流程: STREAM SOCKET: Server :  socket() --->  bind() ---> listen()  ---> accept() Client: ...

  3. 32位Linux文件限制大小

    线上程序不断重新启动,查看log发现是进程由于SIGXFSZ信号退出.对过大的文件进行操作的时候会产生此信号,一般仅仅在32位机器上出现,文件限制大小为2G.用lsof查看进程打开的文件,果然有一个文 ...

  4. 用java代码实现环圈报数

    环圈报数就是围一圈人,每一次数数数到三的人自动出圈,再接着数, 用数据结构的思想实现 public class Count3Quit {     public static void main(Str ...

  5. Java正則表達式演示样例

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { public s ...

  6. Netflix公司监控内部安全的开源项目

    Netfix公司已经公布了三个内部工具,用于捕捉黑客在使用互联网服务时留下的痕迹. AndyHoernecke和Netflix公司的云安全团队成员ScottBehrens指出:"很多安全团队 ...

  7. 使用Google Roads API抓取道路信息(java实现)

    Google Roads API提供了强大的道路获取接口,用户仅仅需调用对应的API就能够获取对应区域的道路坐标以及道路的限速信息. 详细的调用方法例如以下: https://roads.google ...

  8. Linux下SVN账户密码保存设置

    Linux下用SVN进行更新等操作时,总是提示输入用户名和密码,很不方便.因此搜了下解决办法,总结如下: 打开SVN配置文件: vim /home/<user>/.subversion/c ...

  9. LAMP配置参考地址

    http://www.linuxidc.com/Linux/2014-07/104563.htm

  10. wp天气预报

    ak url  http://developer.baidu.com/map/index.php?title=car 后台cs using System; using System.Collectio ...