费马小定理 Codeforces615D Multipliers

本文解决了一个数学问题,即计算由特定质数乘积构成的数的所有约数的乘积,并通过模运算简化计算过程。采用前缀思想和快速幂算法提高效率。

传送门:点击打开链接

题意:这些质数的乘积所组成的那个数,求这个数的所有约数的乘积,取模1e9+7

思路:先分别求出每个数出现的个数,那么对于这一类的数,计算它对答案的贡献。首先,假如最多有s个这样的数字,假设这个数为a,那么我要考虑,对于一个约数,出现了1次,2次......s次,然后其他的质数的出现情况有多少种呢,假如其他质数出现的次数为cnt[i],那么就有P = (cnt[1]+1)*(cnt[2]+1)*....*(cnt[i-1]+1)*(cnt[i+1]+1)*...(cnt[n]+1),那么答案就是power(a,P*(cnt[i]+1)*cnt[i]/2)

对于P,我们可以用前缀思想来维护。对于a^b%mod,因为b很大,但是根据费马小定理,b应该对mod-1取模

所以我们在幂上面应该对mod-1取模,然后一个快速幂,就搞定啦

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;

const int MX = 2e5 + 5;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;

LL L[MX], R[MX];
int A[MX], B[MX], cnt[MX], rear;

LL power(LL a, LL b, LL mod) {
    LL ret = 1;
    while(b) {
        if(b & 1) ret = ret * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ret;
}

int main() {
    int n; //FIN;
    while(~scanf("%d", &n)) {
        rear = 0;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &A[i]);
        }
        sort(A + 1, A + 1 + n);

        LL ans = 1;
        int l, r, w;
        L[0] = 1;
        for(l = 1; l <= n; l = r + 1) {
            for(r = l; r < n && A[r + 1] == A[r]; r++);
            w = r - l + 1;
            B[++rear] = A[l]; cnt[rear] = w;
            L[rear] = L[rear - 1] * (cnt[rear] + 1) % (mod - 1);
        }
        R[rear + 1] = 1;
        for(r = rear; r >= 1; r--) {
            R[r] = R[r + 1] * (cnt[r] + 1) % (mod - 1);
        }
        for(int i = 1; i <= rear; i++) {
            LL p = (LL)cnt[i] * (cnt[i] + 1) / 2 % (mod - 1) * L[i - 1] % (mod - 1) * R[i + 1] % (mod - 1);
            ans = ans * power(B[i], p, mod) % mod;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值