题目链接:

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+数论)的更多相关文章

  1. Codeforces Round #460 (Div. 2).E 费马小定理+中国剩余定理

    E. Congruence Equation time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  2. [Codeforces]Codeforces Round #460 (Div. 2)

    Supermarket 找最便宜的就行 Solution Perfect Number 暴力做 Solution Seat Arrangement 注意当k=1时,横着和竖着是同一种方案 Soluti ...

  3. Codeforces Round #460 (Div. 2) ABCDE题解

    原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...

  4. Codeforces Round #460 (Div. 2) 前三题

    Problem A:题目传送门 题目大意:给你N家店,每家店有不同的价格卖苹果,ai元bi斤,那么这家的苹果就是ai/bi元一斤,你要买M斤,问最少花多少元. 题解:贪心,找最小的ai/bi. #in ...

  5. Codeforces Round #460 (Div. 2)

    A. Supermarket We often go to supermarkets to buy some fruits or vegetables, and on the tag there pr ...

  6. 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 ...

  7. Codeforces Round #460 (Div. 2)-D. Substring

    D. Substring time limit per test3 seconds memory limit per test256 megabytes Problem Description You ...

  8. Codeforces Round #460 (Div. 2)-C. Seat Arrangements

    C. Seat Arrangements time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  9. Codeforces Round #460 (Div. 2)-B. Perfect Number

    B. Perfect Number time limit per test2 seconds memory limit per test256 megabytes Problem Descriptio ...

随机推荐

  1. HDU 1668 Islands and Bridges

    Islands and Bridges Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on HDU. Ori ...

  2. Uva 10081 Tight words (概率DP)

    Time limit: 3.000 seconds Given is an alphabet {0, 1, ... , k}, 0 <= k <= 9 . We say that a wo ...

  3. 【Swift初见】Swift数组(二)

    在苹果的开发文档中对Array还提供了其它的操作算法: 1.Sort函数: 对数组进行排序.依据指定的排序规则,看以下的代码: var array = [2, 3, 4, 5] array.sort{ ...

  4. js02---字符串

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. c++中重载、重写、覆盖的区别

    Overload(重载):在C++程序中,可以将语义.功能相似的几个函数用同一个名字表示,但参数或返回值不同(包括类型.顺序不同),即函数重载.(1)相同的范围(在同一个类中):(2)函数名字相同:( ...

  6. 76.Nodejs Express目录结构

    转自:https://blog.csdn.net/xiaoxiaoqiye/article/details/51160262 Express是一个基于Node.js平台的极简.灵活的web应用开发框架 ...

  7. 30.IntellJ Idea 导入已存在的Maven项目

    转自:https://blog.csdn.net/epdc2/article/details/53767386

  8. 判断浏览器是否支持某些新属性---placeholder兼容问题解决

    function is_true(){ return 'placeholder' in document.createElement('input'); } 实例:placeholder在低版本IE浏 ...

  9. kafka删除主题

    hdp集群默认不能删除kafka主题,如果要删除,需要在ambari上进行配置,将enable delete设置为true.

  10. WebMethod Description

    http://www.webxml.com.cn/WebServices/WeatherWebService.asmx https://www.cnblogs.com/wanganyi/p/72202 ...