Harry Potter and the Hide Story

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2193    Accepted Submission(s): 530

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
 
题意:给你n和k,让你求出最大的i 满足n的阶乘能被k的i次方整除。

思路:对k进行质因数分解。求出每一个质因数在阶乘中的幂的大小。答案即为最小的那个幂。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std; #define LL unsigned long long const int maxn = 10000005;
bool isPrime[maxn];
vector<LL> prime,digit,cnt; void getPrime(){
prime.clear();
memset(isPrime,0,sizeof isPrime);
for(LL i = 2; i < maxn; i++){
if(!isPrime[i]){
prime.push_back(i);
for(LL j = i*i; j < maxn; j += i)
isPrime[j] = 1;
}
}
} void getDigit(LL k){ for(int i = 0; i < prime.size() && k >= prime[i]; i++){
if(k%prime[i]==0){
int tt = 0;
digit.push_back(prime[i]);
while(k%prime[i]==0){
tt++;
k /= prime[i];
}
cnt.push_back(tt);
}
}
if(k!=1){
digit.push_back(k);
cnt.push_back(1);
}
} LL getSum(LL n,LL p){
LL res = 0;
while(n){
n /= p;
res += n;
}
return res;
}
int main(){
int ncase,T=1;
LL k,n;
getPrime();
cin >> ncase;
while(ncase--){
cin >> n >> k;
if(k==1){
printf("Case %d: inf\n",T++);
continue;
}
LL ans = -1;
digit.clear();
cnt.clear();
getDigit(k); for(int i = 0; i < digit.size(); i++){
LL tk = getSum(n,digit[i])/cnt[i];
if(ans == -1) ans = tk;
else ans = min(ans,tk);
}
printf("Case %d: %I64u\n",T++,ans); }
return 0;
}

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

HDU3988-Harry Potter and the Hide Story(数论-质因数分解)的更多相关文章

  1. UVA 10892 LCM Cardinality(数论 质因数分解)

    LCM Cardinality Input: Standard Input Output: Standard Output Time Limit: 2 Seconds A pair of number ...

  2. HDU 3988 Harry Potter and the Hide Story(数论-整数和素数)

    Harry Potter and the Hide Story Problem Description iSea is tired of writing the story of Harry Pott ...

  3. Harry Potter and the Hide Story(hdu3988)

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

  4. 数学概念——J - 数论,质因数分解

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

  5. 简单数论之整除&质因数分解&唯一分解定理

    [整除] 若a被b整除,即a是b的倍数,那么记作b|a("|"是整除符号),读作"b整除a"或"a能被b整除".b叫做a的约数(或因数),a ...

  6. Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)

    Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...

  7. Leetcode 263 Ugly Number 数论 类似质因数分解

    Ugly Number的质因数仅为2,3,5 将输入的数分别除以2,3,5直到不能除,看是否为1,为1的是Ugly Number,其他则不是. class Solution { public: boo ...

  8. hdu 5108(数论-整数分解)

    Alexandra and Prime Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  9. 集训第六周 数学概念与方法 J题 数论,质因数分解

    Description Tomorrow is contest day, Are you all ready? We have been training for 45 days, and all g ...

随机推荐

  1. Error与Exception的区别

    错误和异常的区别(Error vs Exception) 错误和异常的区别(Error vs Exception) 今天面试问了这样一个问题,"Error" 和 "Exc ...

  2. 官方原版Windows XP SP3(VOL)中文简体版ISO下载

    今天,向各位朋友推荐今年五月,由MSDN官方集成的XP With SP3专业VOL版.    文 件:Windows XP Professional with Service Pack 3 (x86) ...

  3. 每日回顾Shell —cat,tail,head

    Shell中常常会用到cat命令.可是总是不是特别清楚: cat命令的用途是连接文件或标准输入并打印. 这个命令经常使用来显示文件内容.或者将几个文件连接起来显示.或者从标准输入读取内容并显示,它常与 ...

  4. 让UIView窄斜

    让UIView窄斜 by 吴雪莹 [UIView animateWithDuration:0.5 animations:^{ CGAffineTransform t = CGAffineTransfo ...

  5. poj1364(差分约束系统)

    poj1364 设s[i] 表示a1 + a2 + ... + a(i-1)的和 给我们n个点,m条约束 如果是a b gt c    那么表示 s[a+b+1] - s[a] > c     ...

  6. OPENVPN开启用户password认证

    一.服务端配置 1.改动openvpn的主配置文件,加入例如以下内容 [root@ttt openvpn]# cat /etc/openvpn/server.conf |more #########a ...

  7. 王立平--android发育,转让eclipse可选颜色

    android:background="@android:color/white" 版权声明:本文博主原创文章.博客,未经同意不得转载.

  8. 《Head First 设计模式》学习笔记——迭代模式 + 组合模式

    迭代模式设置共生死亡,一般来说.我们只是想实现一个集,我们需要的同时提供这个集合的迭代器,喜欢java中间Collection.List.Set.Map等,这些集合都有自己的迭代器.假如我们要实现一个 ...

  9. Delphi XE7 发布时间

    Delphi7 XE7 我可以下载: http://altd.embarcadero.com/download/radstudio/xe7/delphicbuilder_xe7_win.iso 安装包 ...

  10. Windows 8 应用开发 - 异步调用

    原文:Windows 8 应用开发 - 异步调用     不论是桌面客户端还是Web应用通常会出现处理时间较长的操作,为了在这段时间内不影响用户与应用之间的交互体验,开发人员通常会使用异步调用技术,使 ...