Codeforces Round #460 (Div. 2) E. Congruence Equation (CRT+数论)
题目链接:
http://codeforces.com/problemset/problem/919/E
题意:
让你求满足 \(na^n\equiv b \pmod p\) 的 \(n\) 的个数。
\(2 ≤ p ≤ 10^{6} + 3, 1 ≤ a, b < p, 1 ≤ x ≤ 10^{12}\).
题解:
因为:
$n \mod p $的循环节是 \(p\)
\(a^{n} \mod p\)的循环节是 \(p-1\)。(费马小定理)
所以: \(na^n \mod p\)的循环节为 \(p*(p-1)\)。
因为 \(p\)是质数。
假设: \(n \mod p \equiv i, a^n\mod p\equiv a^j\).
\(a^n \mod p \equiv i\) ----①
$a^n\mod p\equiv a^j $ ----②
\(na^n\equiv b \pmod p\) ----③
可以得到: \(i \times a^j \equiv b \pmod p\).
我们现在枚举的\(a^n\) 中的 \(n\) 为 \(j\) , 满足 \(n \times a^n\ mod\ p\ = \ b\) 的 \(n\) 为 \(i\).
列出同余方程:
$i \equiv b*a^{-j} \pmod p $ ---①
\(i\equiv j \pmod {p-1}\) ---②
利用 \(CRT\) 可以解出 :\(i=(p-1)^2ba^{-j}+pj\) ,其中 \(a^{-j}\) 是$ a^{j}$ 在 $\mod p $意义下的逆元。
因为在所有 \(<=x\) 的 \(i\) 的倍数都满足条件,除法统计一下即可。
复杂度:\(O(p*logp)\)
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll qpower(ll a,ll b, ll mod)
{
ll ans = 1;
while(b){
if(b&1) ans = ans * a % mod;
b>>=1;
a=a*a%mod;
}
return ans;
}
ll a,b,mod,x;
int main(int argc, char const *argv[]) {
std::cin >> a >> b >> mod >> x;
ll ans = 0;
for(int i = 1;i <= mod-1;i++) {
ll c = qpower( qpower(a, i , mod) , mod - 2, mod) * b % mod;
ll n = ((mod-1) * (mod-1) * c + mod * i) % (mod * (mod-1));
ans += ( x / (mod * (mod-1)) ) + (x % (mod * (mod-1)) >= n );
}
std::cout << ans << '\n';
return 0;
}
Codeforces Round #460 (Div. 2) E. Congruence Equation (CRT+数论)的更多相关文章
- Codeforces Round #460 (Div. 2).E 费马小定理+中国剩余定理
E. Congruence Equation time limit per test 3 seconds memory limit per test 256 megabytes input stand ...
- [Codeforces]Codeforces Round #460 (Div. 2)
Supermarket 找最便宜的就行 Solution Perfect Number 暴力做 Solution Seat Arrangement 注意当k=1时,横着和竖着是同一种方案 Soluti ...
- Codeforces Round #460 (Div. 2) ABCDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...
- Codeforces Round #460 (Div. 2) 前三题
Problem A:题目传送门 题目大意:给你N家店,每家店有不同的价格卖苹果,ai元bi斤,那么这家的苹果就是ai/bi元一斤,你要买M斤,问最少花多少元. 题解:贪心,找最小的ai/bi. #in ...
- Codeforces Round #460 (Div. 2)
A. Supermarket We often go to supermarkets to buy some fruits or vegetables, and on the tag there pr ...
- Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)
D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces Round #460 (Div. 2)-D. Substring
D. Substring time limit per test3 seconds memory limit per test256 megabytes Problem Description You ...
- Codeforces Round #460 (Div. 2)-C. Seat Arrangements
C. Seat Arrangements time limit per test1 second memory limit per test256 megabytes Problem Descript ...
- Codeforces Round #460 (Div. 2)-B. Perfect Number
B. Perfect Number time limit per test2 seconds memory limit per test256 megabytes Problem Descriptio ...
随机推荐
- react基础用法二(组件渲染)
react基础用法二(组件渲染) 如图所示组件可以是函数 格式:function 方法名(){ return <标签>内容</标签>} 渲染格式: <方法名 /> ...
- RocketMQ 就是耗内存
http://blog.csdn.net/loongshawn/article/details/51086876 https://rocketmq.incubator.apache.org/docs/ ...
- 保留原先小程序名称 更改微信小程序主体
首先给小程序开发者普及一些官方消息: 1.目前官方是不允许修改已经认证的小程序主体信息!(公众号可以修改) 2.小程序与公众号的名称是全平台唯一,即如果小程序叫‘ABC’其他小程序和公众号就不能存在‘ ...
- sublime 支持 vue 语法
具体步骤如下: 1.如果你没安装Package Control,请先安装,安装方法请自行百度.安装OK后,接下来步骤请参考第2步即可. 2.如果你已经安装过Package Control,安装vue高 ...
- 120.VS调试技巧
设置断点调试 在一行代码的左侧点击即可设置断点,按F5(调试->开始调试)即可运行到第一个端点处暂停 逐语句调试 按F11(调试->逐语句)即可开始一步一步执行 逐过程调试 按F10(调试 ...
- ubuntu下eclipse java ee首次打开提示找不到jdk的问题
最近想搭建一个本地服务器,方便写一些网络请求相关的demo,遂下载了eclipse ee版 ( IDEA证书好贵,暂时不想买-=-),下载---解压 一切正常,但是当在terminal下打开ecli ...
- Android自定义组件系列【12】——非UI线程绘图SurfaceView
一.SurfaceView的介绍 在前面我们已经会自定义View,使用canvas绘图,但是View的绘图机制存在一些缺陷. 1.View缺乏双缓冲机制. 2.程序必须重绘整个View上显示的图片,比 ...
- require和import的使用
一.前言 ES6标准发布后,module成为标准,标准的使用是以export指令导出接口,以import引入模块,但是在我们一贯的node模块中,我们采用的是CommonJS规范,使用require引 ...
- Css学习总结(1)——20个很有用的CSS技巧
1. 黑白图像 这段代码会让你的彩色照片显示为黑白照片,是不是很酷? img.desaturate { filter: grayscale(100%); -webkit-filter: graysca ...
- POJ 3461 Oulipo KMP算法题解
本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...