[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;
}
[Codeforces 919E]Congruence Equation的更多相关文章
- Codeforces.919E.Congruence Equation(同余 费马小定理)
题目链接 \(Description\) 给定a,b,x,p,求[1,x]中满足n*a^n ≡b (mod p) 的n的个数.\(1<=a,b<p\), \(p<=1e6+3\), ...
- Codeforces 919E Congruence Equation ( 数论 && 费马小定理 )
题意 : 给出数 x (1 ≤ x ≤ 10^12 ),要求求出所有满足 1 ≤ n ≤ x 的 n 有多少个是满足 n*a^n = b ( mod p ) 分析 : 首先 x 的范围太大了,所以使 ...
- cf 460 E. Congruence Equation 数学题
cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...
- E. Congruence Equation
E. Congruence Equation 思路: 中国剩余定理 \(a^n(modp) = a^{nmod(p-1)}(modp)\),那么枚举在\([0,n-2]\)枚举指数 求\(a^i\)关 ...
- Codeforces Round #460 (Div. 2) E. Congruence Equation (CRT+数论)
题目链接: http://codeforces.com/problemset/problem/919/E 题意: 让你求满足 \(na^n\equiv b \pmod p\) 的 \(n\) 的个数. ...
- Codeforces Round #460 E. Congruence Equation
Description 题面 \(n*a^n≡b (\mod P),1<=n<=x\) Solution 令 \(n=(P-1)*i+j\) \([(P-1)*i+j]*a^{[(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&l ...
- 【Codeforces】Round #460 E - Congruence Equation 中国剩余定理+数论
题意 求满足$na^n\equiv b \pmod p$的$n$的个数 因为$n \mod p $循环节为$p$,$a^n\mod p$循环节为$p-1$,所以$na^n \mod p$循环 ...
- 【codeforces 20B】Equation
[题目链接]:http://codeforces.com/contest/20/problem/B [题意] 给你一个方程,让你输出这个方程的解的情况. [题解] a==0,b==0,c==0时,为恒 ...
随机推荐
- php缩放gif和png图透明背景变成黑色的解决方法_php技巧
工作中需要缩放一些gif图然后在去Imagecopymerge,可是发现使用了imagecreatetruecolor和imagecopyresampled后发现背景图不对,本来透明的背景图变成了黑色 ...
- 《统计学习方法》P89页IIS的中间步骤Zw+δ(X)/Zw(X)的推导
共有两个方法:
- 201621123050 《Java程序设计》第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2.1 简述如何使用数据库技术改造你的系统.要建立什么表?截图你的表设计. 答 ...
- 项目Alpha冲刺Day4
一.会议照片 二.项目进展 1.今日安排 学习熟悉前台框架且搭建前台页面框架. 2.问题困难 使用了前端的构建工具webpack,困难在于怎么使用gradle结合它连同后台框架中的配置一起打包,因为本 ...
- electron-vue工程创建
没有vue创建经验请移步至 vue下载与安装 使用vue创建electron-vue工程 vue init simulatedgreg/electron-vue my-project 安装elemen ...
- 安装QT5.02
1.下载QT5 SDK 下载地址:http://qt-project.org/downloads. 2.安装QT5 下载完后,假设放在Download/,切换到该目录,输入:./qt-linux-op ...
- Flask Session 详解
会话session ,允许你在不同请求 之间储存信息.这个对象相当于用密钥签名加密的 cookie ,即用户可以查看你的 cookie ,但是如果没有密钥就无法修改它. from flask impo ...
- 关于安装win7系统时出现0x0000007b电脑蓝屏代码的问题
问题解析: 0X0000007B 这个错误网上都说是sata硬盘的什么引导模式的原因引起. 在网上查找了很久,大概引起错误的原因就是:sata和ide两种模式不同,前者可以装win7系统,后者是xp系 ...
- ruby:TypeError: 对象不支持此属性或方法
解决办法. 1.下载对应版本 下载node.js,根据ruby版本决定下载32还是x64,我的ruby版本x64 https://npm.taobao.org/mirrors/node/v8.9.3/ ...
- vue初尝试--项目结构
新建一个项目之后,我们来看一下项目的目录结构 几个主要文件的内容 index.html文件(入口文件,系统进入之后先进入index.html) <!DOCTYPE html> <ht ...