题目描述

Given an integer xx . Your task is to find out how many positive integers nn ( 1<=n<=x1<=n<=x ) satisfy

 where a,b,pa,b,p are all known constants.

输入输出格式

输入格式:

The only line contains four integers a,b,p,xa,b,p,x ( 2<=p<=10^{6}+32<=p<=106+3 , 1<=a,b<p1<=a,b<p , 1<=x<=10^{12}1<=x<=1012 ). It is guaranteed that pp is a prime.

输出格式:

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

输入输出样例

输入样例#1:

2 3 5 8
输出样例#1:

2
输入样例#2:

4 6 7 13
输出样例#2:

1
输入样例#3:

233 233 10007 1
输出样例#3:

1

说明

In the first sample, we can see that n=2n=2 and n=8n=8 are possible answers.

利用同余乱搞即可,系数是mod p的同余系,指数是mod (p-1)的同余系。

不过写之前一定要想好,不然特别容易写错(细节略多)。。。

#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define ll long long
#define maxn 1000010
using namespace std;
ll ans,inv[maxn],b,d;
ll ci[maxn],p,tp; inline void init(const ll ha){
ci[0]=1;
for(int i=1;i<ha;i++) ci[i]=ci[i-1]*d%ha;
inv[1]=1;
for(int i=2;i<ha;i++) inv[i]=-inv[ha%i]*(ha/i)%ha+ha;
} inline void solve(const ll ha){
ll tmpc,tmpd;
ll basec=(tp+1)/(ha-1),lefc=tp+1-basec*(ha-1);
//basec表示指数上有多少个完整的循环节
//lefc表示最后多出来的可以走的步数
for(int i=p-2;i>=0;i--){
int to=b*inv[ci[i]]%ha;
tmpc=basec+(i<lefc);
//tmpc是计算a^i这个数出现的次数
tmpd=tmpc/ha+((i-to+ha)%ha<tmpc%ha);
//因为每次指数对应的系数都会-1,
//所以就相当于计算在系数的同余系下
//从i开始倒退走tmp-1步能走到to多少次
ans+=tmpd;
}
} int main(){
scanf("%lld%lld%lld%lld",&d,&b,&p,&tp);
init(p);
solve(p);
printf("%lld\n",ans);
return 0;
}

  

Codeforces 919 E Congruence Equation的更多相关文章

  1. cf 460 E. Congruence Equation 数学题

    cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...

  2. E. Congruence Equation

    E. Congruence Equation 思路: 中国剩余定理 \(a^n(modp) = a^{nmod(p-1)}(modp)\),那么枚举在\([0,n-2]\)枚举指数 求\(a^i\)关 ...

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

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

  4. [Codeforces 919E]Congruence Equation

    Description 题库链接 求满足 \[n\cdot a^n\equiv b \pmod{p}\] 的 \(n\) 的个数, \(1\leq n\leq x\) , \(a,b,p,x\) 均已 ...

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

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

  7. 【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​$循环 ...

  8. Codeforces 919E Congruence Equation ( 数论 && 费马小定理 )

    题意 : 给出数 x (1 ≤ x ≤ 10^12 ),要求求出所有满足 1 ≤ n ≤ x 的 n 有多少个是满足 n*a^n  = b ( mod p ) 分析 : 首先 x 的范围太大了,所以使 ...

  9. Codeforces Little Dima and Equation 数学题解

    B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...

随机推荐

  1. P2764 最小路径覆盖问题

    题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开 ...

  2. bzoj 2425 [HAOI2010]计数 dp+组合计数

    [HAOI2010]计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 451  Solved: 289[Submit][Status][Discus ...

  3. ansible+docker

    1.准备镜像: 1007 docker run -itd --name client2 ff37bc5ab732 1008 docker run -itd --name client ff37bc5a ...

  4. svn“Previous operation has not finished; run 'cleanup' if it was interrupted“ 或者不能cleanup,或者提示空目录 报错的解决方法

    参考了文档: http://blog.csdn.net/superch0054/article/details/38668017 今天碰到了个郁闷的问题,svn执行clean up命令时报错“Prev ...

  5. PowerDesigner使用教程(转)

    PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...

  6. jsp中的路径问题

    在学jsp的时候我每次都遇到路径的问题,每次都不能够说100%的把这搞定,这让我很烦恼,今天下午花了点时间来把路径问题整理了下. 一:首先我们在加载项目(我的项目名称是FinalExam)是的路径是h ...

  7. 对request.getSession(false)的理解(附程序员常疏忽的一个漏洞)

    本文属于本人原创,转载请注明出处:http://blog.csdn.net/xxd851116/archive/2009/06/25/4296866.aspx [前面的话] 在网上经常看到有人对req ...

  8. Python基础(4)_集合、布尔类型

    一.集合 集合的作用一:关系运算集合的作用二:去重 定义集合:集合内的元素必须是唯一的:集合内的元素必须是可hash的,也是就不可变类型:集合是无序的 s={'egon',123,'egon','1' ...

  9. 02-更改窗口的根控制器 Demo示例程序源代码

      源代码下载链接:02-更改窗口的根控制器.zip18.0 KB // MJAppDelegate.h // //  MJAppDelegate.h //  02-更改窗口的根控制器 // //  ...

  10. Apache多网站虚拟目录域名

    一台服务器安装了Apache,如何绑定多个域名或网站呢? 最简单高效的方法如下: 1. 先打开Apache的配置文件httpd.conf,在这个文件, 找到这句:“#Include etc/extra ...