Number Sequence

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

Problem Description
Given a number sequence b1,b2…bn. Please count how many number sequences a1,a2,...,an satisfy the condition that a1*a2*...*an=b1*b2*…*bn (ai>1).
 
Input
The input consists of multiple test cases. For each test case, the first line contains an integer n(1<=n<=20). The second line contains n integers which indicate b1, b2,...,bn(1<bi<=1000000, b1*b2*…*bn<=1025).
 
Output
For each test case, please print the answer module 1e9 + 7.
 
Sample Input
2 3 4
 
Sample Output
4

Hint

For the sample input, P=3*4=12. Here are the number sequences that satisfy the condition:

2 6
3 4
4 3
6 2
 
Source
 
 

将每一个数分解质因数,得到每个质因数出现的次数(和质数本身没有关系),然后就要用到容斥原理了,

也就是将每个质数出现的次数放到n个容器中去,这里要注意下1的情况也就是某个容器里面没有放数。

这样结果=总的方案数-有一个容器没放数+有2个容器没有放数……

将m个数放入n个容器的方法数有C(n+m-1,n-1) 。

#include <iostream>
#include <stdio.h>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <stack>
#include <math.h>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int M_P= ;
const LL Mod= ;
bool isprime[M_P+] ;
int prime[M_P] ,id;
map<int ,int>my_hash ;
map<int ,int>::iterator p ;
void make_prime(){
id= ;
memset(isprime,,sizeof(isprime)) ;
for(int i=;i<=M_P;i++){
if(!isprime[i])
prime[++id]=i ;
for(int j= ;j<=id&&i*prime[j]<=M_P;j++){
isprime[i*prime[j]]= ;
if(i%prime[j]==)
break ;
}
}
}
LL C[][] ;
void get_C(){
C[][]=C[][]= ;
for(int i=;i<=;i++){
C[i][]=C[i][i]= ;
for(int j=;j<i;j++){
C[i][j]=C[i-][j-]+C[i-][j] ;
if(C[i][j]>=Mod)
C[i][j]%=Mod ;
}
}
}
void gao(int N){
for(int i=;i<=id&&prime[i]*prime[i]<=N;i++){
if(N%prime[i]==){
while(N%prime[i]==){
my_hash[prime[i]]++ ;
N/=prime[i] ;
}
}
if(N==)
break ;
}
if(N!=)
my_hash[N]++ ;
}
vector<int>vec ;
int N ;
LL Sum(){
LL ans= ;
for(int k=;k<vec.size();k++){
ans*=C[vec[k]+N-][N-] ;
if(ans>=Mod)
ans%=Mod ;
}
for(int i=;i<=N;i++){
LL sum=C[N][i] ;
for(int k=;k<vec.size();k++){
int m=N-i ;
sum*=C[vec[k]+m-][m-] ;
if(sum>=Mod)
sum%=Mod ;
}
if(i&)
ans-=sum ;
else
ans+=sum ;
ans=(ans%Mod+Mod)%Mod ;
}
return ans ;
}
int main(){
make_prime() ;
get_C() ;
int M ;
while(scanf("%d",&N)!=EOF){
my_hash.clear() ;
vec.clear() ;
for(int i=;i<=N;i++){
scanf("%d",&M) ;
gao(M) ;
}
for(p=my_hash.begin();p!=my_hash.end();p++)
vec.push_back(p->second) ;
cout<<Sum()<<endl;
}
return ;
}

HDU 4390 Number Sequence 容斥原理的更多相关文章

  1. HDU 4390 Number Sequence (容斥原理+组合计数)

    HDU 4390 题意: 大概就是这样.不翻译了: Given a number sequence b1,b2-bn. Please count how many number sequences a ...

  2. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  3. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  4. HDU 1005 Number Sequence(数论)

    HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...

  5. HDU 1711 Number Sequence (字符串匹配,KMP算法)

    HDU 1711 Number Sequence (字符串匹配,KMP算法) Description Given two sequences of numbers : a1, a2, ...... , ...

  6. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  7. HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  8. HDU 1711 Number Sequence(KMP裸题,板子题,有坑点)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. HDU 1711 Number Sequence (KMP简单题)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. servlet & filter & listener & interceptor

    web.xml 的加载顺序是:context- param -> listener -> filter -> servlet * Servlet 对URL生效,用户处理用户的URL请 ...

  2. [转]wireshark 实用过滤表达式(针对ip、协议、端口、长度和内容)

    首先说几个最常用的关键字,“eq” 和 “==”等同,可以使用 “and” 表示并且,“or”表示或者.“!" 和 "not” 都表示取反. 一.针对wireshark最常用的自然 ...

  3. wcf stream 不知道长度的情况下,读取stream

    http://bbs.csdn.net/topics/360163784 string filepath = @"http://ww4.sinaimg.cn/thumbnail/6741e0 ...

  4. android学习笔记29——Intent/IntentFilter

    Intent/IntentFilter Intent封装android应用程序需要启动某个组件的“意图”,Intent还是应用程序组件之间通信的重要媒介. EG:Activity之间需要交换数据时,使 ...

  5. [tty与uart]2.tty和uart的函数调用流程

    以下是在include/uapi/linux/tty.h中定义了现有的线规号,如果需要定义新的,则需要在后面添加新的 /* line disciplines */ #define N_TTY 0 #d ...

  6. Tomcat插件与Jetty插件在MyEclipse中的配置

    -Djetty.port=8101 jetty:run tomcat6:run <plugin> <groupId>org.apache.tomcat.maven</gr ...

  7. PLSQL_性能优化工具系列17_Best Practices: Proactive Data Collection for Performance Issues

    占位符 https://support.oracle.com/epmos/faces/DocumentDisplay?_afrLoop=2082062510193540&id=1366133. ...

  8. cf 605B B. Lazy Student 构造 好题

    题意: 一个n个节点的图,有m条边,已知这个图的一个mst 现在如果我们知道这个图的m条边,和知道mst的n-1条边是哪些,问能不能构造出一个满足条件的图 思路:排序+构造 数组deg[i]表示节点i ...

  9. [复变函数]第05堂课 1.4 复球面与 $\infty$; 作业讲解; 2 解析函数 2.1 解析函数的概念与 Cauchy-Riemann 方程

    1. 复球面 大漠孤烟直, 长河落日圆. $$\bex \bbC\cong \bbS^2\bs \sed{N},\quad \bbC_\infty=\bbC\cup \sed{\infty}\mbox ...

  10. JAVA 想让类无法new,可以使用private将类的构造函数改为私有的,这样new的时候就会报错了

    JAVA 想让类无法new,可以使用private将构造函数改为私有的,这样new的时候就会报错了 主要用于,静态工具类,静态类不需要new,直接使用   类名.静态方法  即可调用 class D{ ...