这题主要是质因数分解!!

求出每个因子的幂,如果有负数,则输出-1;

如果2的幂数为0,这输出0;

最后就是开始凑阶乘了……

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. #include<iomanip>
  5. #include<cmath>
  6. #include<string>
  7. #include<cstdlib>
  8. #include<vector>
  9. using namespace std;
  10. int prime[],cnt,p[],q[],num[],len;
  11. int s[],r[],a[];
  12. bool f[];
  13. int min(int a,int b)
  14. {
  15. return a>b?b:a;
  16. }
  17. int max(int a,int b)
  18. {
  19. return a>b?a:b;
  20. }
  21. void init()
  22. {
  23. int i,j;
  24. cnt=;
  25. for(i=;i<=;i++)
  26. {
  27. if(f[i]==) prime[cnt++]=i;
  28. for(j=;j<cnt&&prime[j]*i<=;j++)
  29. {
  30. f[i*prime[j]]=;
  31. if(i%prime[j]==) break;
  32. }
  33. }
  34. }
  35. int count(int n,int p)//计算n!中p因子的个数
  36. {
  37. int sum=;
  38. while(n>=p)
  39. {
  40. sum+=(n/=p);
  41. }
  42. return sum;
  43. }
  44. int divs(int n,int m)//分解因子
  45. {
  46. int i,j;
  47. len=;
  48. memset(num,,sizeof(num));
  49. for(i=;i<n;i++)
  50. {
  51. for(j=;prime[j]<=p[i];j++)
  52. num[j]+=count(p[i],prime[j]);
  53. len=max(len,j);
  54. }
  55. for(i=;i<m;i++)
  56. {
  57. for(j=;prime[j]<=q[i];j++)
  58. {
  59. if(j>=len) return -;
  60. num[j]-=count(q[i],prime[j]);
  61. if(num[j]<) return -;
  62. }
  63. }
  64. if(num[]==) return ;
  65. return ;
  66. }
  67. int main()
  68. {
  69. init();
  70. int i,j,k,ans,n,m,temp,t,cur,mul;
  71. bool flag;
  72. cin>>t;
  73. while(t--)
  74. {
  75. cin>>n>>m;
  76. for(i=;i<n;i++) cin>>p[i];
  77. for(i=;i<m;i++) cin>>q[i];
  78. ans=divs(n,m);
  79. if(ans<=)
  80. {
  81. cout<<ans<<endl;
  82. continue;
  83. }
  84. for(i=;i<len&&num[i]>;i++) ;
  85. len=min(len,i);
  86. cur=prime[i]-;
  87. cnt=;
  88. while(num[]>&&cur>=)
  89. {
  90. flag=true;
  91. a[]=count(cur,prime[]);
  92. if(a[]>num[]) flag=false;
  93. else mul=num[]/a[];
  94. for(i=;flag&&i<len&&prime[i]<=cur;i++)
  95. {
  96. a[i]=count(cur,prime[i]);
  97. if(a[i]>num[i]) flag=false;
  98. else mul=min(mul,num[i]/a[i]);
  99. }
  100. if(flag && mul>)
  101. {
  102. if(i<len) len=i;
  103. r[cnt]=cur;
  104. s[cnt++]=mul;
  105. for(i=;i<len;i++)
  106. {
  107. num[i]-=(mul*a[i]);
  108. if(num[i]==) break;
  109. }
  110. if(len>i) len=i,cur=prime[len]-;
  111. else cur--;
  112. }
  113. else cur--;
  114. }
  115. cout<<cnt<<endl;
  116. for(i=;i<cnt;i++)
  117. cout<<r[i]<<' '<<s[i]<<endl;
  118. }
  119. return ;
  120. }

hdu 3758 Factorial Simplification的更多相关文章

  1. 题解报告:hdu 1124 Factorial(求N!尾数有多少个0。)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1124 Problem Description The most important part of a ...

  2. HDU 1124 Factorial (数论)

    http://acm.hdu.edu.cn/showproblem.php? pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長.我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙 ...

  3. hdu 1124 Factorial(数论)

    题意: 求n!的尾0的个数 分析: 0一定是由因子2和5相乘产生的: 2的个数显然大于5的个数,故只需统计因子5的个数 n/5不能完全表示n!中5的个数(egg: 25),应该n/=5后,累加上n/2 ...

  4. HDU 1124 Factorial (阶乘后缀0)

    题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将n个十进制数进行质因数分解,观察的得到只有2*5才会出现10.那么n!应含有min(2个数,5个数)个后缀0,明显5的个数必定比2少,所 ...

  5. [SinGuLaRiTy] 组合数学题目复习

    [SinGuLaRiTy] Copyright (c) SinGuLaRiTy 2017.  All Rights Reserved. [CQBZOJ 2011] 计算系数 题目描述 给定一个多项式( ...

  6. ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbilisi, November 24, 2010

    ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbil ...

  7. Factorial(hdu 1124)

    Description The most important part of a GSM network is so called Base Transceiver Station (BTS). Th ...

  8. HDU——PKU题目分类

    HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...

  9. leetcode N-Queens/N-Queens II, backtracking, hdu 2553 count N-Queens, dfs 分类: leetcode hdoj 2015-07-09 02:07 102人阅读 评论(0) 收藏

    for the backtracking part, thanks to the video of stanford cs106b lecture 10 by Julie Zelenski for t ...

随机推荐

  1. unity 3消 游戏

    3消游戏跟着智能手机流行到现在已经有很长一段时间,unity实现的3消 https://github.com/textcube/match3action 截图如下: 在阅读源码的时候不难发现,Game ...

  2. js 求前n项的 fibnaci 数列和

    function f(n) { var num1 = 1, num2 = 1; if (n == 1) document.write(num1);//n=1,输出1 else if (n > 1 ...

  3. c++11:iota

    iota: Fills the range [first, last) with sequentially(循环的) increasing values, starting with value an ...

  4. Linux C 程序 进程控制(17)

    进程控制 1.进程概述现代操作系统的特点在于程序的并行执行.Linux是一个多用户多任务的操作系统.ps .pstree 查看进程进程除了进程id外还有一些其他标识信息,可以通过相应的函数获得.// ...

  5. linux lsof命令的使用

    lsof(list open files)是一个列出当前系统打开文件的工具.在UNIX环境下,任何事物都是以文件的形式存在的,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件. 像传输协议(T ...

  6. php出现“syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”错误的一种情况,及解决方法

    PHP中的“syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”错误,可能是因为美元符号$的误用,看下面一种情况 class Test{         s ...

  7. Oracle 10g 数据文件的第一个数据块结构

    一.数据文件的第一个数据块结构kcvfh BBED> set file 1 FILE# 1 BBED> set block 1 BLOCK# 1 --查看第一个数据块的整体结构 BBED& ...

  8. Log.i()的用法

    2011-04-16 09:44 17486人阅读 评论(4) 收藏 举报 androidlayoutbuttonstringencodingeclipse 在调试代码的时候我们需要查看调试信息,那我 ...

  9. Nginx 老是抢先捕获了404 我想这个404我Yii项目来处理 要怎么配置?

    配置Nginx vhost的时候别指定error_page 它就不会跟Yii抢生意了 or 你弄一个frontendController   然后再里面 加上 $user->loginRequi ...

  10. 【h5-egret】如何快速开发一个小游戏

    1.环境搭建 安装教程传送门:http://edn.egret.com/cn/index.php?g=&m=article&a=index&id=207&terms1_ ...