A^X mod P(简单数论 + 思维打表)

一.题目链接:

A^X mod P

二.题目大意:

给出 T,n, A, K,a, b, m, P.

1 \leq n\leq 10^{6}

0 \leq A, K, a, b \leq 10^{9}

1 \leq m, P \leq 10^{9}

T 组样例.

 f(x) = \left\{\begin{matrix}1\;\;\;x=1 & & & & & & & & \\(a \times f(x - 1) + b)\;(mod\;\;m)\;\;\;x > 1 & & & & & & & & \end{matrix}\right.

求 A^{f(1)}+A^{f(2)}+....+A^{f(n)} \;\;(mod\;\;p).

三.分析:

由于 1 \leq m \leq 10^{9}

所以 1 \leq f(x) \leq 10^{9}

如果用快速幂求和的话会 TLE.

因为 A^{f(x)} = A^{a\times 10^{5} + b}

所以只需要求 sum1[] 和 sum2[].

sum1[i]:A^{i}\;\;(mod\;\;p)

sum2[i]:A^{10^{5}i}\;\;(mod\;\;p)

所以 A^{f(x)} = sum1[i \;mod\;M] \times sum2[i\;/\;M]

详见代码.

四.代码实现:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-6
#define PI acos(-1.0)
#define ll long long int
using namespace std;

const int M = (int)1e5;
ll n, A, K, a, b, m, p;
ll sum1[M + 5];
ll sum2[M + 5];

void init()
{
    sum1[0] = sum2[0] = 1;
    for(int i = 1; i <= M; ++i)
        sum1[i] = sum1[i - 1] * A % p;
    sum2[1] = sum1[M];
    for(int i = 1; i <= M / 10; ++i)
        sum2[i] = sum2[i - 1] * sum2[1] % p;
}

ll f()
{
    ll fx = K;
    ll sum = 0;
    for(ll i = 1; i <= n; ++i)
    {
        sum = (sum1[fx % M] * sum2[fx / M] % p + sum) % p;
        fx = (a * fx + b) % m;
    }
    return sum % p;
}

int main()
{

    int T;
    scanf("%d", &T);
    for(int ca = 1; ca <= T; ++ca)
    {
        cin >> n >> A >> K >> a >> b >> m >> p;
        init();
        ll ans = f();
        printf("Case #%d: %lld\n", ca, ans);
    }
    return 0;
}

 

在C语言中实现GF(2^8)上的多项式乘法以及求逆元,我们通常会利用有限域的性质和算法,例如中国剩余定理(Chinese Remainder Theorem, CRT)。不过这里我会简化讲解基本的多项式乘法部分,并给出一个基于GF(2^8)简单示例。对于不可约多项式如x^6 + x^5 + x^4 + x + 1,我们需要找到它的模逆元。 首先,我们可以用位运算来模拟GF(2^8),其中每个元素是一个8位的二进制数,加法、减法视为异或(XOR),乘法则可以看作是按位与(AND)然后左移一位。以下是一个简单的多项式乘法函数: ```c #include <stdio.h> #include <stdint.h> // 定义GF(2^8) typedef uint8_t GF2; // 单项式乘法 (GF(2^8)) GF2 multiply(GF2 a, GF2 b) { return a & b << 1; } // 多项式乘法 (两个GF2数组相乘) void polynomial_multiply(GF2* result, const GF2* poly1, const GF2* poly2, int degree1, int degree2, int max_degree) { for (int i = 0; i <= max_degree; ++i) { result[i] = 0; for (int j = 0; j <= i && j < degree1; ++j) { result[i] ^= multiply(poly1[j], poly2[(i - j) % (degree2 + 1)]); } } } // 主程序示例 int main() { GF2 poly1[] = {1, 0, 0, 0, 0, 1}; // x^5 + 1 GF2 poly2[] = {1, 1, 1, 1, 1, 0}; // x^5 + x^4 + x^3 + x^2 + 1 // 初始化结果数组,长度为两度数之和加一 GF2 result[11]; // 多项式乘法 polynomial_multiply(result, poly1, poly2, 5, 5, 10); // 输出结果 for (int i = 0; i <= 10; ++i) { printf("result[%d] = %d\n", i, result[i]); } return 0; } ``` 至于求逆元,对于不可约多项式,由于其不是单位元,通常没有逆元。但在一些特定的场合,比如如果我们要计算模该多项式的逆,这将变得复杂,可能需要借助更高级的数学方法,例如循环检测或者利用更复杂的数论算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值