[Codeforces 919E]Congruence Equation

Description

题库链接

求满足 \[n\cdot a^n\equiv b \pmod{p}\] 的 \(n\) 的个数, \(1\leq n\leq x\) , \(a,b,p,x\) 均已给出。

\(2\leq p\leq 10^6+3,1\leq a,b < p, 1\leq x\leq 10^{12}\) , 保证 \(p\) 是质数。

Solution

对于 \(x\leq 10^{12}\) 显然不能枚举判断。但我们注意到当关于 \(n_1,n_2\) 的方程,若满足 \(n_1\equiv n_2\pmod{p(p-1)}\) 那么这两个方程就是等价的。

理由可以由费马小定理 \(a^{p-1}\equiv1\pmod{p}\) ,以及 \(x\equiv x-p\pmod{p}\) 得到。

我们假设 \(n=i(p-1)+j\) ,那么 \[\begin{aligned}n\cdot a^n&\equiv b &\pmod{p}\\ i(p-1)+j&\equiv b\cdot a^{-j}&\pmod{p}\\ j-i&\equiv b\cdot a^{-j}&\pmod{p}\end{aligned}\]

由于 \(j\) 可能的取值只有 \(p-1\) 个,我们可以枚举 \(j\) 来算出对应的 \(i\) 的个数,也就是 \(n\) 的个数。值得注意的是由于 \(n\) 不能取 \(0\) 所以为了方便处理,让 \(j=0\) 变为 \(j=p-1\) 。

枚举 \(j\) 后我们可以求出最小的 \(i\) : \(i\equiv j-b\cdot a^{-j}\pmod{p}\) ,进而求出最小的 \(n\) 。然后求出 \([1,x]\) 的范围内的等价的解的个数。

Code

//It is made by Awson on 2018.2.1
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define lowbit(x) ((x)&(-(x)))
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define writeln(x) (write(x), putchar('\n'))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
using namespace std;
void read(LL &x) {
    char ch; bool flag = 0;
    for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x < 0) putchar('-'); print(Abs(x)); }

LL a, b, p, x;

LL quick_pow(LL a, LL b, LL p) {
    LL ans = 1;
    while (b) {
        if (b&1) ans = ans*a%p;
        a = a*a%p, b >>= 1;
    }
    return ans;
}
void work() {
    read(a), read(b), read(p), read(x);
    LL inva = quick_pow(a, p-2, p);
    LL ans = 0, now = b;
    for (int i = 1; i < p; i++) {
        now = now*inva%p;
        LL n = (p-1)*((i-now+p)%p)+i;
        if (n > x) continue;
        ans += (x-n)/((LL)p*(p-1))+1;
    }
    writeln(ans);
}
int main() {
    work();
    return 0;
}

原文地址:https://www.cnblogs.com/NaVi-Awson/p/8401324.html

时间: 2024-08-01 12:48:09

[Codeforces 919E]Congruence Equation的相关文章

cf 460 E. Congruence Equation 数学题

cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2?≤?p?≤?10^{6}?+?3, 1?≤?a,?b?<?p, 1?≤?x?≤?10^{12}\) 思路: 题目中存在两个循环节 \(n % p\) 和 \(a ^ n % p\), 循环节分别为\(p,p-1\) 我们枚举\(i = n\ (mod)\ (p - 1)\) 可以得到两个方程 \[ n\ \equiv\ i\ mod\ (p-1)

Codeforces 919 E Congruence Equation

题目描述 Given an integer xx . Your task is to find out how many positive integers nn ( 1<=n<=x1<=n<=x ) satisfy  where a,b,pa,b,p are all known constants. 输入输出格式 输入格式: The only line contains four integers a,b,p,xa,b,p,x ( 2<=p<=10^{6}+32<

Codeforces 627A XOR Equation(思路)

题目大概说两个正整数a.b,已知s=a+b以及x=a xor b的值,问有几种a.b这样的数对. 我知道异或相当于无进位的加法,s-x就是其各个位置的进位,比如s-x=1010,那就表示a和b的第1位和第3位发生的进位. 这样,对于某些位其值就能确定,对于有些位其值不能确定(该位xor和为1且没有发生进位),这时a和b的该位都能选择0或者1,对于不确定的就是乘法原理答案累乘2. 另外还有一些情况是不可能的,首先s<x不可能,s-x是奇数不可能,某一位xor和是1且发生了进位这不可能. 最后注意是

CodeForces 635C XOR Equation

位运算. 又涨姿势了:$a + b = (aXORb) + 2*(aANDb)$,$ (aXORb)$是不进位的部分,$2*(aANDb)$为进位之后的部分,相加就是$a + b$. 知道了这个转换,这题就很容易了.设$n=a+b$,$m=(aXORb)$,$x=(aAND b)$:$n$.$m$和$x$都是已知的. 题目要求统计有多少$(a,b)$满足: $\left\{ {\begin{array}{*{20}{c}}{a + b = n}\\{aXORb = m}\end{array}}

[CF919E]Congruence Equation

题意:求关于$n$的方程$n\cdot a^n\equiv b\left(mod\ p\right)$在$[1,x]$中整数解的数量 果然是Chinese round,interesting round(幸好没打 首先注意到那个指数很令人痛苦,所以用费马小定理把指数弄掉 令$n=\left(p-1\right)i+j\left(i\geq0,0\leq j\lt p-1\right)$ $\left[\left(p-1\right)i+j\right]a^{\left(p-1\right)i+j

Codeforces Round #460 (Div. 2) 919 笔记

A. Supermarket 输入n,m, (1?≤?n?≤?5?000, 1?≤?m?≤?100)表示n组价格数据和目标重量m 接下来n组价格数据,表示为a元b千克,每组无限取 求最小花费 B. Perfect Number 输入k,1?≤?k?≤?10?000,求第k个完美数 完美数定义为数位和=10 (tutorial中说难度可以升级为k<1e18)->用数位dp可解 C. Seat Arrangements 输入n,m,k (1?≤?n,?m,?k?≤?2?000)表示nm的课室,有k

hunnu11562:The Triangle Division of the Convex Polygon(第n个卡特兰数取模)

Problem description   A convex polygon with n edges can be divided into several triangles by some non-intersect diagonals. We denote d(n) is the number of the different ways to divide the convex polygon. For example,when n is 6,there are 14 different

HDU 4569 Special equations(取模)

Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4569 Description Let f(x) = a nx n +...+ a 1x +a 0, in which a i (0 <= i <= n) are all known integers. We call f(x) 0 (mod

2018 CCPC网络赛 几道数学题

1002 Congruence equation 题目链接  : http://acm.hdu.edu.cn/showproblem.php?pid=6439 题解 : https://www.zybuluo.com/yang12138/note/1262592 相关定理 : 裴蜀定理 在数论中,裴蜀定理是一个关于最大公约数(或最大公约式)的定理.裴蜀定理得名于法国数学家艾蒂安·裴蜀,说明了对任何整数a.b和它们的最大公约数d,关于未知数x和y的线性丢番图方程(称为裴蜀等式): ax + by