HDU 3988(数论)

浅浅的花香味﹌ 2022-05-31 05:06 276阅读 0赞

问题描述:

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

3b2eeb33c7d601e6b67343855809d70b_v_1517737046 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

  1. 2
  2. 2 2
  3. 10 10

Sample Output

  1. Case 1: 1
  2. Case 2: 2

题目分析:我们对k进行质因子分解,保存种类和对应的指数(factor[i][0]种类,factor[i][1]个数),我们也相应的求出N!中factor[i][0]的个数a,则ans=min(ans,a/factor[i][1]);

当k为1时则为inf

代码如下:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #define ll long long
  6. using namespace std;
  7. const int maxn=1e7+100;
  8. int prime[maxn],cnt;
  9. bool vis[maxn];
  10. void get_prime()//素数打表
  11. {
  12. memset (vis,true,sizeof (vis));
  13. vis[1]=false;
  14. for (int i=2;i<maxn;i++) {
  15. if (vis[i]) prime[cnt++]=i;
  16. for (int j=0;j<cnt&&i*prime[j]<maxn;j++) {
  17. vis[i*prime[j]]=false;
  18. if (i%prime[j]==0) break;
  19. }
  20. }
  21. }
  22. ll factor[1000][2],fcnt;
  23. void get_factor(ll m)//求质因子
  24. {
  25. memset (factor,0,sizeof (factor));
  26. fcnt=0;
  27. for (int i=0;i<cnt&&(ll) (prime[i]*prime[i])<=m;i++) {
  28. if (m%prime[i]==0) {
  29. factor[fcnt][0]=prime[i];
  30. while (m%prime[i]==0) {
  31. factor[fcnt][1]++;
  32. m=m/prime[i];
  33. }
  34. fcnt++;
  35. }
  36. }
  37. if (m!=1) {
  38. factor[fcnt][0]=m;
  39. factor[fcnt][1]=1;
  40. fcnt++;
  41. }
  42. }
  43. ll solve(ll m,ll k)//求m!中因子k的个数
  44. {
  45. if (m/k==0) return 0;
  46. else return m/k+solve(m/k,k);
  47. }
  48. int main()
  49. {
  50. int t,icase=1;
  51. get_prime();
  52. scanf("%d",&t);
  53. while (t--) {
  54. ll n,k;
  55. scanf("%lld%lld",&n,&k);
  56. if (k==1) {
  57. printf("Case %d: inf\n",icase++);
  58. continue;
  59. }
  60. get_factor(k);
  61. ll ans=1e18;
  62. for (int i=0;i<fcnt;i++) {
  63. ans=min(ans,solve(n,factor[i][0])/factor[i][1]);
  64. }
  65. printf("Case %d: %lld\n",icase++,ans);
  66. }
  67. return 0;
  68. }

发表评论

表情:
评论列表 (有 0 条评论,276人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU 4196 Remoteland (数论 n!相关)

    题意:用不大于n的所有正数去组成一个尽可能大的完全平方数。 思路:显然取n!是最大的,设其为a,但这不一定是一个完全平方数,需要把多余的部分除掉。 可以利用勒让德定理很快处