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.

大意:求使上式成立的n的数量(1<=n<=x)

题解:看了tag才有了灵感:

n*a^n≡b(mod p)

可以变形为

n%p*a^(n%(p-1))≡b(mod p)         ——费马小定理和同余原理

令n%p= i , n%(p-1)=j.

可以倒过来考虑,对于一组使同余方程成立的 i 和 j ,求有多少个n。

枚举 j,解方程求出 i ,然后求最小的 n ,易得n+k*(p-1)*p也是合法的解(k为任意自然数)。

解方程求出 i 应该不用讲了,求出最小的n以后算出这组 i , j 贡献的答案数也不难。(细节可以见代码)

最大的问题是如何解出n

中国剩余定理!

想要求n,可以做如下变形:

n≡ i (mod p)

n≡ j (mod p-1)

可以用中国剩余定理来求解。

推荐中国剩余定理讲解:

https://www.cnblogs.com/MashiroSky/p/5918158.html

最后一个小细节:p等于2的时候用费马小定理求逆元会出现问题,特判,如果是exgcd求逆元应该不会碰到这个问题。

 1 /*
 2 Welcome Hacking
 3 Wish You High Rating
 4 */
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<ctime>
 9 #include<cstdlib>
10 #include<algorithm>
11 #include<cmath>
12 #include<string>
13 using namespace std;
14 int read(){
15     int xx=0,ff=1;char ch=getchar();
16     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
17     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
18     return xx*ff;
19 }
20 long long READ(){
21     long long xx=0,ff=1;char ch=getchar();
22     while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
23     while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
24     return xx*ff;
25 }
26 int mypow(int x,int p,int MOD){
27     int re=1;
28     while(p){
29         if(p&1)
30             re=1LL*re*x%MOD;
31         p>>=1;
32         x=1LL*x*x%MOD;
33     }
34     return re;
35 }
36 int a,b,p;
37 long long x,ans;
38 int main(){
39     //freopen("in","r",stdin);
40     a=read(),b=read(),p=read();
41     x=READ();
42     if(p==2){
43         cout<<x/2+(x%2==1)<<endl;
44         return 0;
45     }
46     long long mul=1LL*p*(p-1);
47     for(int i=0;i<=p-2;i++){
48         int y=1LL*b*mypow(mypow(a,i,p),p-2,p)%p;
49         long long temp=(1LL*i*p*mypow(p,p-3,p-1)%mul+1LL*y*(p-1)*mypow(p-1,p-2,p)%mul)%mul;
50         ans+=x/mul+(x%mul>=temp);
51     }
52     cout<<ans<<endl;
53     return 0;
54 }

View Code

转载于:https://www.cnblogs.com/lzhAFO/p/8401078.html

codeforces 919E Congruence Equation相关推荐

  1. [2.7]【CF933A】A Twisty Movement【CF926B】Add Points【CF917A】The Monster【CF919E】Congruence Equation

    文章目录 T1:A Twisty Movement 题目 题解 code T2:Add Points 题目 题解 code T3:The Monster 题目 题解 code T4:Congruenc ...

  2. Codeforces Round #460 (Div. 2): E. Congruence Equation(枚举)

    题意:给出a, b, p, x,求有多少个n满足①n*a^n%p==b:②n<=x 思路:先要知道一个很简单的性质:a^n%p的值一定存在循环节(n=0就进入循环),且周期T一定是p-1的约数 ...

  3. [ 数论 ] Codeforces919E Congruence Equation

    假设 n=Ap+B(B<p) n = A p + B ( B < p ) n=Ap+B(B B⋅aAp+B≡b ( mod p ) B · a A p + B ≡ b ( m o d p ...

  4. Codeforces 题目合集+分类+代码 【Updating...】【361 in total】

    961A - Tetris                                                模拟                                      ...

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

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

  6. 2018中国大学生程序设计竞赛-网络选拔赛题解

    以下所有AC题解程序来自"仙客传奇"团队. A. Buy and Resell AC的C++语言程序: #include<iostream> #include<c ...

  7. oracle求非偶非素数的和,Sub Maths__写给非数学专业的朋友们

    [本文系寄托天下原创,转载请注明出处,拜托.] [综合版版主]GOGO ^^ 附4.GOGO的数学词汇 map image fixed point composite function one to ...

  8. 线性同余方程和中国剩余定理学习笔记

    线性同余方程介绍 形如 a x ≡ c ( m o d b ) ax \equiv c \pmod b ax≡c(modb) 的方程被称为 线性同余方程(Congruence Equation). 求 ...

  9. ACM-ICPC模板整理

    备注其一:正在整理中,内容不全,部分代码测试次数较少或还未在OJ上尝试,可能会有代码不健全的情况发生. 备注其二:部分图片来自百度百科.wiki百科. 备注其三:CSDN一天只能上传十篇blog... ...

最新文章

  1. 网络营销——网络营销浅析网站不发文还能维持稳定排名吗?
  2. 自动备份网站和数据库打包并上传FTP服务器并删除前30天文件
  3. 5G精华问答 | 5G关键技术解读
  4. 两个三维向量叉积_线性代数的本质08 叉积
  5. Javascript的原型链
  6. 【学习笔记】深入理解Linux内核第三版 ——第二章 内存寻址
  7. 小程序UI库 iView Weapp
  8. 安卓开发eclipse+adt下载
  9. 28.STM32电阻与电容触摸屏幕
  10. n维椭球体积公式_考前必背!数量关系、资料分析常用公式汇总!不再头大!...
  11. 笔记本Windows7无法连接上家庭wifi,急急急!!!
  12. xshell复制粘贴快捷键
  13. 学好Linux运维决心书
  14. 解决 go get获取package时候time out超时问题
  15. python彩虹图_python绘制彩虹图教程
  16. 码农翻身之我是一个线程 --- 读书笔记
  17. 榆熙电商:拼多多商家怎样开通电子面单服务?有何优势?
  18. Servlet规范之安全
  19. 广东b级计算机考试试题,计算机等级考试级B考试试题真题试卷.doc
  20. 脏话越多,代码越好!

热门文章

  1. spring是如何管理 事务的
  2. DNN Experience
  3. 设计模式C++学习笔记之十三(Decorator装饰模式)
  4. Express 入门之Router - worldtree_keeper的专栏 - CSDN博客
  5. Dcloud HTML5 监听蓝牙设备 调用 原生安卓实现 - aspirant - 博客园
  6. 算法 --- [队列结构]二叉树的层次遍历
  7. javascript --- typeof方法和instanceof方法
  8. 利用数据的商业智能分析工具
  9. 写一个js向左滑动删除 交互特效的插件——Html5 touchmove
  10. 顺利搭建了oracle