Codeforces Round #460 (Div. 2).E 费马小定理+中国剩余定理
3 seconds
256 megabytes
standard input
standard output
Given an integer x. Your task is to find out how many positive integers n (1 ≤ n ≤ x) satisfy
where a, b, p are all known constants.
The only line contains four integers a, b, p, x (2 ≤ p ≤ 106 + 3, 1 ≤ a, b < p, 1 ≤ x ≤ 1012). It is guaranteed that p is a prime.
Print a single integer: the number of possible answers n.
2 3 5 8
2
4 6 7 13
1
233 233 10007 1
1
In the first sample, we can see that n = 2 and n = 8 are possible answers.
题意:给出a, b, p, x,求有多少个n满足:n*a^n%p==b(n<=x)
思路:先要知道一个很简单的性质:a^n%p=(a%p)^(n%p-1)%p=a^(n%p-1),即a^n仅有p-1种结果。
①暴力枚举a^i%p (i从0到p-1)算出每个余数,可以得到式子n*a^i%p==b
②对于每个a^i,用逆元求出c,n%p = c= b*inv(a^i)%p
③则n%(p-1)==i 且n%p==c,最小的n可以用CRT求出,n的周期是P=p*(p-1).
代码:
//#include "bits/stdc++.h"
#include "cstdio"
#include "map"
#include "set"
#include "cmath"
#include "queue"
#include "vector"
#include "string"
#include "cstring"
#include "time.h"
#include "iostream"
#include "stdlib.h"
#include "algorithm"
#define db double
#define ll long long
//#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define inf 0x3f3f3f3f
#define rep(i, x, y) for(int i=x;i<=y;i++)
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = mod - ;
const db eps = 1e-;
const db PI = acos(-1.0);
using namespace std;
ll a,b,p,x;
ll m[],f[];
ll exgcd(ll a, ll b, ll &x, ll &y)
{
ll d;
//if (a == 0 && b == 0) return -1;
if (b == )
{
x = ;
y = ;
return a;
}
d = exgcd(b, a%b, y, x);
y -= a / b * x;
return d;
} ll inv(ll a, ll MOD)
{
ll x, y, d;
d = exgcd(a, MOD, x, y);
if (d == )
return (x % MOD + MOD) % MOD;
// else return -1;
}
ll china(ll *f, ll *m){
ll M = , ret = ;
for(int i = ; i < ; i ++) M *= m[i];
for(int i = ; i < ; i ++){
ll w = M / m[i];
ret = (ret + w * inv(w, m[i]) * f[i]) % M;
}
return (ret + M) % M;
}
ll qpow(ll x,ll n)
{
ll ans=;
x%=p;
while(n){
if(n&) ans=ans*x%p;
x=x*x%p;
n>>=;
}
return ans;
} int main()
{
cl(a),cl(b),cl(p),cl(x);
a%=p;
ll ans=,P=p*(p-);
m[]=p-,m[]=p;
for(int i=;i<p-;i++)
{
f[]=i;
f[]=inv(qpow(a,i),p)*b%p;
ll xx=china(f,m);
ans+=(x-xx+P)/P;
}
pl(ans);
return ;
}
Codeforces Round #460 (Div. 2).E 费马小定理+中国剩余定理的更多相关文章
- 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) E. Congruence Equation (CRT+数论)
题目链接: http://codeforces.com/problemset/problem/919/E 题意: 让你求满足 \(na^n\equiv b \pmod p\) 的 \(n\) 的个数. ...
- 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\), ...
- 【bzoj1951】[Sdoi2010]古代猪文 费马小定理+Lucas定理+中国剩余定理
题目描述 求 $g^{\sum\limits_{k|n}C_{n}^{\frac nk}}\mod 999911659$ 输入 有且仅有一行:两个数N.G,用一个空格分开. 输出 有且仅有一行:一个 ...
- 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 ...
随机推荐
- Python列表边遍历边修改问题解决方案:alist[:]
最近在看python,遇到个简单的问题:删除列表中指定的重复元素,发现一些实用并且有趣的东西. 1.错误示范 alist = [1,1,2,2,3,3,2,2,1,1] for i in alist: ...
- JavaScript 数组排序(从大到小,从小到大)
1.数组的从大到小排序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- 花3分钟了解下C/C++中的函数可变参简单实现
1.可变参函数的原理 C/C++函数的参数是存放在栈区的,并且参数的入栈是从参数的右边开始,即最后一个参数先入栈,而第一个参数最后才入栈,所以,根据栈的后进先出性质,函数总能找到第一个参数.所以,可变 ...
- 简单的NLog配置文件
NLog.config <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="h ...
- 如何把某个网站的SSL Server certificate链导入到ABAP Netweaver系统里
我们在用ABAP代码消费外网的url时会遇到一些异常,比如ICM_HTTP_SSL_PEER_CERT_UNTRUSTED,这是因为请求的url所在的网站的SSL Server certificate ...
- 【转】Android tools:context
tools:context="com.example.guolin.scrollertest.MainActivity" 有时候可以看到有这个东西,但是从来没有用过,不知道有什么作 ...
- ROS机器人程序设计
在<ROS机器人程序设计>中,在第二章创建节点时给出一个接收和发送的例子,但是按照书中步骤编译时,遇到按个三个问题,现在罗列出来解决方案供参考. 建议在工作空间直接输入 catkin_ ...
- OpenCV自带dnn的Example研究(5)— segmentation
这个博客系列,简单来说,今天我们就是要研究 https://docs.opencv.org/master/examples.html下的 6个文件,看看在最新的OpenCV中,它们是如何发挥作用的. ...
- 前端JavaScript之DOM使用案例
1.弹出框点击关闭 (这个例子关键自己创建标签以及属性,不是太好想啊,而且作用相对来数也不是太大) <!DOCTYPE html> <html> <head> &l ...
- td过长,将固定宽度table撑开
解决办法: 在table上加上样式: table{table-layout:fixed;word-break:break-all} table-layout:fixed tablle的列宽由表格宽 ...