hdu 3758 Factorial Simplification
这题主要是质因数分解!!
求出每个因子的幂,如果有负数,则输出-1;
如果2的幂数为0,这输出0;
最后就是开始凑阶乘了……
- #include<iostream>
- #include<stdio.h>
- #include<algorithm>
- #include<iomanip>
- #include<cmath>
- #include<string>
- #include<cstdlib>
- #include<vector>
- using namespace std;
- int prime[],cnt,p[],q[],num[],len;
- int s[],r[],a[];
- bool f[];
- int min(int a,int b)
- {
- return a>b?b:a;
- }
- int max(int a,int b)
- {
- return a>b?a:b;
- }
- void init()
- {
- int i,j;
- cnt=;
- for(i=;i<=;i++)
- {
- if(f[i]==) prime[cnt++]=i;
- for(j=;j<cnt&&prime[j]*i<=;j++)
- {
- f[i*prime[j]]=;
- if(i%prime[j]==) break;
- }
- }
- }
- int count(int n,int p)//计算n!中p因子的个数
- {
- int sum=;
- while(n>=p)
- {
- sum+=(n/=p);
- }
- return sum;
- }
- int divs(int n,int m)//分解因子
- {
- int i,j;
- len=;
- memset(num,,sizeof(num));
- for(i=;i<n;i++)
- {
- for(j=;prime[j]<=p[i];j++)
- num[j]+=count(p[i],prime[j]);
- len=max(len,j);
- }
- for(i=;i<m;i++)
- {
- for(j=;prime[j]<=q[i];j++)
- {
- if(j>=len) return -;
- num[j]-=count(q[i],prime[j]);
- if(num[j]<) return -;
- }
- }
- if(num[]==) return ;
- return ;
- }
- int main()
- {
- init();
- int i,j,k,ans,n,m,temp,t,cur,mul;
- bool flag;
- cin>>t;
- while(t--)
- {
- cin>>n>>m;
- for(i=;i<n;i++) cin>>p[i];
- for(i=;i<m;i++) cin>>q[i];
- ans=divs(n,m);
- if(ans<=)
- {
- cout<<ans<<endl;
- continue;
- }
- for(i=;i<len&&num[i]>;i++) ;
- len=min(len,i);
- cur=prime[i]-;
- cnt=;
- while(num[]>&&cur>=)
- {
- flag=true;
- a[]=count(cur,prime[]);
- if(a[]>num[]) flag=false;
- else mul=num[]/a[];
- for(i=;flag&&i<len&&prime[i]<=cur;i++)
- {
- a[i]=count(cur,prime[i]);
- if(a[i]>num[i]) flag=false;
- else mul=min(mul,num[i]/a[i]);
- }
- if(flag && mul>)
- {
- if(i<len) len=i;
- r[cnt]=cur;
- s[cnt++]=mul;
- for(i=;i<len;i++)
- {
- num[i]-=(mul*a[i]);
- if(num[i]==) break;
- }
- if(len>i) len=i,cur=prime[len]-;
- else cur--;
- }
- else cur--;
- }
- cout<<cnt<<endl;
- for(i=;i<cnt;i++)
- cout<<r[i]<<' '<<s[i]<<endl;
- }
- return ;
- }
hdu 3758 Factorial Simplification的更多相关文章
- 题解报告:hdu 1124 Factorial(求N!尾数有多少个0。)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1124 Problem Description The most important part of a ...
- HDU 1124 Factorial (数论)
http://acm.hdu.edu.cn/showproblem.php? pid=1124 題目好長好長,好可怕,看完腎都萎了,以後肯定活不長.我可不能死在這種小事上,小灰灰我勵志死在少女的超短裙 ...
- hdu 1124 Factorial(数论)
题意: 求n!的尾0的个数 分析: 0一定是由因子2和5相乘产生的: 2的个数显然大于5的个数,故只需统计因子5的个数 n/5不能完全表示n!中5的个数(egg: 25),应该n/=5后,累加上n/2 ...
- HDU 1124 Factorial (阶乘后缀0)
题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将n个十进制数进行质因数分解,观察的得到只有2*5才会出现10.那么n!应含有min(2个数,5个数)个后缀0,明显5的个数必定比2少,所 ...
- [SinGuLaRiTy] 组合数学题目复习
[SinGuLaRiTy] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [CQBZOJ 2011] 计算系数 题目描述 给定一个多项式( ...
- 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 ...
- Factorial(hdu 1124)
Description The most important part of a GSM network is so called Base Transceiver Station (BTS). Th ...
- HDU——PKU题目分类
HDU 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 ...
- 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 ...
随机推荐
- unity 3消 游戏
3消游戏跟着智能手机流行到现在已经有很长一段时间,unity实现的3消 https://github.com/textcube/match3action 截图如下: 在阅读源码的时候不难发现,Game ...
- js 求前n项的 fibnaci 数列和
function f(n) { var num1 = 1, num2 = 1; if (n == 1) document.write(num1);//n=1,输出1 else if (n > 1 ...
- c++11:iota
iota: Fills the range [first, last) with sequentially(循环的) increasing values, starting with value an ...
- Linux C 程序 进程控制(17)
进程控制 1.进程概述现代操作系统的特点在于程序的并行执行.Linux是一个多用户多任务的操作系统.ps .pstree 查看进程进程除了进程id外还有一些其他标识信息,可以通过相应的函数获得.// ...
- linux lsof命令的使用
lsof(list open files)是一个列出当前系统打开文件的工具.在UNIX环境下,任何事物都是以文件的形式存在的,通过文件不仅仅可以访问常规数据,还可以访问网络连接和硬件. 像传输协议(T ...
- php出现“syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”错误的一种情况,及解决方法
PHP中的“syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM”错误,可能是因为美元符号$的误用,看下面一种情况 class Test{ s ...
- Oracle 10g 数据文件的第一个数据块结构
一.数据文件的第一个数据块结构kcvfh BBED> set file 1 FILE# 1 BBED> set block 1 BLOCK# 1 --查看第一个数据块的整体结构 BBED& ...
- Log.i()的用法
2011-04-16 09:44 17486人阅读 评论(4) 收藏 举报 androidlayoutbuttonstringencodingeclipse 在调试代码的时候我们需要查看调试信息,那我 ...
- Nginx 老是抢先捕获了404 我想这个404我Yii项目来处理 要怎么配置?
配置Nginx vhost的时候别指定error_page 它就不会跟Yii抢生意了 or 你弄一个frontendController 然后再里面 加上 $user->loginRequi ...
- 【h5-egret】如何快速开发一个小游戏
1.环境搭建 安装教程传送门:http://edn.egret.com/cn/index.php?g=&m=article&a=index&id=207&terms1_ ...