Codeforces 615D Multipliers 【组合数学】

本篇介绍了一个算法问题,通过质因数分解计算一个数的所有因子乘积模10^9+7的值。利用组合数学原理,通过预处理质因数及其出现次数,高效地计算出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Multipliers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ayrat has number n, represented as it's prime factorization pi of size m, i.e. n = p1·p2·...·pm. Ayrat got secret information that that the product of all divisors of n taken modulo 109 + 7 is the password to the secret data base. Now he wants to calculate this value.

Input

The first line of the input contains a single integer m (1 ≤ m ≤ 200 000) — the number of primes in factorization of n.

The second line contains m primes numbers pi (2 ≤ pi ≤ 200 000).

Output

Print one integer — the product of all divisors of n modulo 109 + 7.

Sample test(s)
input
2
2 3
output
36
input
3
2 3 2
output
1728
Note

In the first sample n = 2·3 = 6. The divisors of 6 are 123 and 6, their product is equal to 1·2·3·6 = 36.

In the second sample 2·3·2 = 12. The divisors of 12 are 12346 and 121·2·3·4·6·12 = 1728.



题意:给定m个质因子p[],有p[1]*p[2]*...*p[m] == n,问n所有因子的乘积 % 1e9 + 7。


思路:组合数学。

先把所有因子存起来,并统计个数为top(不相同的)。考虑每个质因子rec[i]做出的贡献,结果为rec[i]^num。

其中num为rec[i]在n所有因子乘积中存在的个数。

定义1-i质因子的组合方案数l[i],后i-top个质因子的组合方案数r[i]。

考虑第i个质因子的状态,取或不取,不取有l[i-1],取则有l[i-1] * cnt[rec[i]](可能取1 - cnt[rec[i]])

同理求法r[]。


对于一个质因子rec[i],可以与前、后组合,那么它的可组合方案数为temp = l[i-1] * r[i+1]。

若该质因子有Num个,则有方案rec[i] * temp 、rec[i] * rec[i] * temp... rec[i]^(Num) * temp。

统计rec[i]出现次数有num = (Num+1) * Num/2 * temp.下面ans *= rec[i] ^ num就好了。 

num值很大,费马小定理a^n = a^(n%(m-1)) (%m)搞下就ok了。也就是说num % (1e9+7-1)。


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN (200000+10)
#define MAXM (200000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
using namespace std;
LL pow_mod(LL a, LL n)
{
    LL ans = 1;
    while(n)
    {
        if(n & 1LL)
            ans = ans * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return ans;
}
LL l[MAXN], r[MAXN];
LL cnt[MAXN];
int rec[MAXN];
int main()
{
    int m; Ri(m);
    int top = 0;
    for(int i = 1; i <= m; i++)
    {
        int a; Ri(a);
        if(!cnt[a])
            rec[++top] = a;
        cnt[a]++;
    }
    l[0] = r[top+1] = 1;
    for(int i = 1; i <= top; i++)
        l[i] = l[i-1] * (cnt[rec[i]] + 1) % (MOD-1);
    for(int i = top; i >= 1; i--)
        r[i] = r[i+1] * (cnt[rec[i]] + 1) % (MOD-1);
    LL ans = 1;
    for(int i = 1; i <= top; i++)
    {
        LL Num = cnt[rec[i]];
        if(Num)
        {
            LL num = l[i-1] * r[i+1] % (MOD-1);
            num = (Num + 1) * Num / 2 % (MOD-1) * num % (MOD-1);
            ans = ans * pow_mod(rec[i], num) % MOD;
        }
    }
    Pl(ans);
    return 0;
}


### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值