Pow

题目描述

定义a^b为a的b次方,并且^是满足右结合的,即a^b^c^d=a^(b^(c^d))。例如,2^3^2=2^(3^2)=2^9=512。
现在给定n个数a1,a2,…,an 求a1^a2^…^an对p取模的值。

事后补充

Drin_E大爷发现下面的方法有错因为(d,p)不一定为1。
唉不管了反正n最大20,你们想保证正确性就用相同方法递归求解呗(逃,因为dx1%p也是形如ax%p的。

一个东西

我们只需要考虑如何计算ax%p
那么就可以解决这道题。
d=(a,p),a=a/d,p=p/d,ans=ax%p
也就是axans(mod p)
dxaxans(mod dp)
两端同时除以d
dx1axansd(mod p)
那么此时(a,p)=1,说明aϕ(p)1(mod p)
那么我们可以计算x%ϕ(p)
需要注意在模意义下d1dϕ(p)1(mod p)

#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxd=10000000+10;
int pri[maxd],phi[maxd],b[30];
bool bz[maxd];
int i,j,k,l,t,n,m,p,top,ca;
int quicksortmi(int x,int y,int p){
    if (!y) return 1;
    int t=quicksortmi(x,y/2,p);
    t=(ll)t*t%p;
    if (y%2) t=(ll)t*(x%p)%p;
    return t;
}
int gcd(int a,int b){
    if (!b) return a;else return gcd(b,a%b);
}
int solve(int i,int p){
    if (i==n+1) return 1;
    int a=b[i],d=gcd(a,p);
    a/=d;p/=d;
    int x=solve(i+1,phi[p]);
    int t=quicksortmi(a,x,p);
    if (x) t=(ll)t*quicksortmi(d,x-1,p)%p;else t=(ll)t*quicksortmi(d,phi[p]-1,p)%p;
    return t*d;
}
int main(){
    phi[1]=1;
    fo(i,2,maxd-10){
        if (!bz[i]){
            pri[++top]=i;
            phi[i]=i-1;
        }
        fo(j,1,top){
            if ((ll)i*pri[j]>maxd-10) break;
            bz[i*pri[j]]=1;
            if (i%pri[j]==0){
                phi[i*pri[j]]=phi[i]*pri[j];
                break;
            }
            phi[i*pri[j]]=phi[i]*(pri[j]-1);
        }
    }
    scanf("%d",&ca);
    while (ca--){
        scanf("%d%d",&n,&p);
        fo(i,1,n) scanf("%d",&b[i]);
        printf("%d\n",solve(1,p));
    }
}
### 关于 `pow` 函数在编程中的应用 在编程中,`pow` 函数通常用于执行幂运算,即计算一个数的某个次方。以下是关于 `pow` 函数的一些关键点: #### 1. C/C++ 中的 `pow` 函数 在 C 和 C++ 编程语言中,`pow` 是一个标准库函数,定义在 `<math.h>` 或 `<cmath>` 头文件中。它接受两个参数:底数和指数,并返回底数的指数次幂[^4]。例如: ```c #include <stdio.h> #include <math.h> int main() { double base = 2.0; double exponent = 3.0; double result = pow(base, exponent); printf("Result: %f\n", result); // 输出 8.000000 return 0; } ``` 需要注意的是,在 GCC/G++ 编译器下使用 `pow` 函数时,可能会遇到链接错误。这是因为 `pow` 函数位于 `libm.so` 库中,因此需要显式链接数学库,例如通过 `-lm` 参数[^1]。 #### 2. JavaScript 中的幂运算 在 JavaScript 中,虽然没有直接的 `pow` 函数,但可以通过 `Math.pow` 方法实现相同的功能。此外,ES7 引入了幂运算符 `**`,使代码更加简洁[^2]。例如: ```javascript // 使用 Math.pow let result1 = Math.pow(2, 3); // 8 // 使用幂运算符 ** let result2 = 2 ** 3; // 8 ``` #### 3. Python 中的幂运算 Python 提供了内置的 `pow` 函数以及 `**` 运算符来执行幂运算。此外,`pow` 函数还支持三参数形式,用于模幂运算(modular exponentiation)。例如: ```python # 使用 pow 函数 result1 = pow(2, 3) # 8 # 使用 ** 运算符 result2 = 2 ** 3 # 8 # 模幂运算 result3 = pow(2, 3, 5) # 等价于 (2 ** 3) % 5 = 3 ``` #### 4. Lua 中的幂运算 在 Lua 中,幂运算通过 `^` 运算符实现。此外,也可以使用 `math.pow` 函数完成相同功能[^3]。例如: ```lua -- 使用 ^ 运算符 local result1 = 2 ^ 3 -- 8 -- 使用 math.pow 函数 local result2 = math.pow(2, 3) -- 8 ``` #### 5. 高阶函数封装示例 在函数式编程中,可以将幂运算封装为高阶函数,从而实现更灵活的复用。以下是一个基于 JavaScript 的示例: ```javascript function makePowerFn(power) { return function(base) { return Math.pow(base, power); }; } const square = makePowerFn(2); console.log(square(3)); // 9 ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值