hdu4390-Number Sequence(容斥计算)

题意:给定b数列,计算有多少种数列 a1,a2,...,an 满足条件
a1*a2*...*an=b1*b2*…*bn (ai>1).

解法:处理出b数列中出现的所有质因子的数量记录在map中,然后进行容斥计算:

代码:

/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std;

#define eps 1e-8
#define zero(_) (abs(_)<=eps)
const double pi=acos(-1.0);
typedef long long LL;
const int Max=1000010;
const int INF=1000000007;

map<int,int> maps;
int num[30];
int n;
bool rem[1000010];
LL C[500][500];
int prime[Max/2];
int p=0;
void init()
{
    for(LL i=2; i<Max; i++)
        if(!rem[i])
        {
            prime[p++]=i;
            for(LL j=i*i; j<Max; j+=i)
                rem[j]=1;
        }
    for(int i=0; i<500; i++)
        for(int j=0; j<=i; j++)
            C[i][j]=j?(C[i-1][j-1]+C[i-1][j])%INF:1;
}
void make(int t)
{
    int tool=t;
    for(int i=0; prime[i]*prime[i]<=t; i++)
    {
        while(tool%prime[i]==0)
            maps[prime[i]]++,tool/=prime[i];
    }
    if(tool>=2)
        maps[tool]++;
}
LL getans(int m)
{
    LL ans=1;
    int k=n-m;
    for(map<int,int>::iterator p=maps.begin();p!=maps.end();p++)
    {
        int t=p->second;
        ans=(ans*C[k+t-1][k-1])%INF;
    }
    return ans;
}
int main()
{
    init();
    while(scanf("%d",&n)==1)
    {
        maps.clear();
        for(int i=0; i<n; i++)
            scanf("%d",num+i);
        for(int i=0; i<n; i++)
        {
            make(num[i]);
        }
        int ans=0;
        for(int i=0; i<n; i++)
        {
            if(i&1)
            {
                ans-=C[n][i]*getans(i)%INF;
                if(ans<0)
                    ans+=INF;
            }
            else
            {
                ans+=C[n][i]*getans(i)%INF;
                if(ans>=INF)
                    ans-=INF;
            }
        }
        cout<<ans<<'\n';
    }
    return 0;
}

hdu4390-Number Sequence(容斥计算),布布扣,bubuko.com

时间: 2024-09-29 22:49:22

hdu4390-Number Sequence(容斥计算)的相关文章

HDU 5297 Y sequence 容斥/迭代

Y sequence Problem Description Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hates those numbers which can be written as a^b (a, b are positive integers,2<=b<=r),so he removed them all.Yellowstar ca

HDU 4135 Co-prime(容斥+数论)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B

hdu5072 Coprime 2014鞍山现场赛C题 计数+容斥

http://acm.hdu.edu.cn/showproblem.php?pid=5072 Coprime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 354    Accepted Submission(s): 154 Problem Description There are n people standing in a

POJ 2773 Happy 2006 (分解质因数+容斥+二分 或 欧几里德算法应用)

Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10309   Accepted: 3566 Description Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are a

Number Sequence(hdu4390)

Number Sequence Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 895 Accepted Submission(s): 374 Problem Description Given a number sequence b1,b2…bn.Please count how many number sequences a1,a2,..

XTU 1242 Yada Number 容斥

Yada Number Problem Description: Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,13 in its prime factors is even. For instance, 18=2 * 3 * 3 is no

ZOJ 3233 Lucky Number 容斥

给你a数组和b数组 求x到y之间有多少个数至少被a中一个数整除并且至少不被b中一个数整除 容斥第一问很简单 第二问可以考虑反面 设满足被a中至少一个数整除的数有sum1个 在被a中至少一个数整除的前提下 被b中所有数整除的数有sum2 答案就是sum1-sum2 在dfs的时候溢出了 借鉴了某大牛的方法 #include <cstdio> #include <cstring> using namespace std; typedef long long LL; const int

双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)

题意:https://codeforc.es/contest/1207/problem/D n个元素,每个元素有a.b两个属性,问你n个元素的a序列和b序列有多少种排序方法使他们不同时非递减(不同时good). 思路: 真难则反+容斥,反向考虑,ans1=如果a序列非递减则有a中各个数字出现次数的阶乘的乘积个,ans2=b序列也是一样. ans3=然后还要减去a序列和b序列都是good的方案数,就是元素相同的出现次数阶乘的乘积(注意,如果不存在双good就不算ans3). ANS就是:全排列 -

(容斥)Codeforces Round #428 (Div. 2) D. Winter is here

D. Winter is here time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the res