E. Congruence Equation
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print a single integer: the number of possible answers n.

Examples
input
2 3 5 8
output
2
input
4 6 7 13
output
1
input
233 233 10007 1
output
1
Note

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 费马小定理+中国剩余定理的更多相关文章

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

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

  2. Codeforces Round #460 (Div. 2) E. Congruence Equation (CRT+数论)

    题目链接: http://codeforces.com/problemset/problem/919/E 题意: 让你求满足 \(na^n\equiv b \pmod p\) 的 \(n\) 的个数. ...

  3. 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\), ...

  4. 【bzoj1951】[Sdoi2010]古代猪文 费马小定理+Lucas定理+中国剩余定理

    题目描述 求  $g^{\sum\limits_{k|n}C_{n}^{\frac nk}}\mod 999911659$ 输入 有且仅有一行:两个数N.G,用一个空格分开. 输出 有且仅有一行:一个 ...

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

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

  6. Codeforces Round #460 (Div. 2)

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

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

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

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

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

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

随机推荐

  1. springmvc实现文件下载到Android手机设备pda端

    1:首先要添加相关得jar文件,在pom.xml中 <dependency> <groupId>commons-fileupload</groupId> <a ...

  2. wamp环境初步使用

    在wamp的www目录下部署页面,localhost/目录/index.html

  3. JS条件语句优化

    1.对多个条件使用Array.includes eg: function test(fruit){                                                    ...

  4. C++ Knowledge series Inheritance & RTTI & Exception Handling

    Inheritance The pointer or reference to base class can address/be assigned with any of the classes d ...

  5. Thymeleaf基础知识

    Thymeleaf是一个Java类库,它是一个xml/xhtml/html5的模板引擎,可以作为MVC的Web引用的View层. Thymeleaf还提供了额外的模块与SpringMVC集成,因此推荐 ...

  6. Android - 通过真实案例学习解内存泄漏问题,最终发现Android原生Bug

    作为一个Android新手小白,刚到新公司,最近的工作就是在学习解各类Bug.转型之初,面临各种新知识,会有压力,但是学习的过程是快乐的. 上周刚遇上一类bug,就是应用的内存泄漏问题.最终通过前辈的 ...

  7. 监控系统-nagios

    https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/4/en/ install yum -y install nagios-4 ...

  8. php中增删改查以及返回结果(一)

    虽然毕业后找的第一份正式的工作并不那么令人满意,但是在度过最初的迷茫期后,自己还是决定成为一个程序猿. 最近也是利用上班偶尔闲下来的时间,开始看书,撸代码,写一些小程序. 这两个礼拜主要的写的都是有关 ...

  9. 关于Linux主流框架运维工作剖析

    LINUX是开源的,这也是最主要的原因,想学Windows,Unix对不起,没有源代码.也正是因为这样,LINUX才能够像雪球一样越滚越大,发展到现在这种规模.今天将为大家带来关于Linux主流框架运 ...

  10. react+webpack 引入字体图标

    在使用react+webpack 构建项目过程中免不了要用到字体图标,在引入过程中报错,不能识别字体图标文件中的@符,报错 Uncaught Error: Module parse failed: U ...