题目描述

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. 【BZOJ 4514】[Sdoi2016]数字配对 费用流

    利用spfa流的性质,我直接拆两半,正解分奇偶(妙),而且判断是否整除且质数我用的是暴力根号,整洁判断质数个数差一(其他非spfa流怎么做?) #include <cstdio> #inc ...

  2. nc用法小记

    By francis_hao    Jun 30,2017   ncat:连接和重定向套接字 概要 ncat [OPTIONS...] [hostname] [port]   描述 ncat 是一个集 ...

  3. [codechef MEXDIV]Mex division

    题目链接:https://vjudge.net/contest/171650#problem/I 直接用set+dp水过去了... /* 设dp[i]表示前i个做划分满足条件的方案数 有一个显然的转移 ...

  4. libusb 示例

    #include <usb.h> #include <stdio.h> #define VERSION "0.1.0" #define VENDOR_ID ...

  5. ERROR: Found lingering reference file hdfs

    Found lingering reference异常 ERROR: Found lingering reference file hdfs://jiujiang1:9000/hbase/month_ ...

  6. Java内存区域与内存异常

    参考:深入理解Java虚拟机 周志明 方法区 虚拟机战 本地方法栈 堆 程序计数器 其他 设置 方法区 线程共享,加载类信息.常量.静态变量.JIT后的代码,别名Non-Heap 对于HotSpot, ...

  7. memcached启动脚本(class练习)

    说明:使用类的方式编写程序启动脚本(memcached) import time import os from subprocess import Popen,PIPE class Process(o ...

  8. 网络基础(osi、协议)

    *互联网协议 人和人沟通需要一套共同的标准,英语就是普遍的一种,计算机如果需要进行联网互通,也需要一种统一的标准,如果所有的计算机都遵守这种标准,就会实现网络的互联. 1.一系列统一的标准,这些标准称 ...

  9. 自定义UINavigationController push和pop动画

    http://segmentfault.com/q/1010000000143983 默认的UINavigationController push和pop的默认动画都是左右滑动推出,我的应用要求这种界 ...

  10. Objective-C中ORM的运用:实体对象和字典的相互自动转换

    http://blog.csdn.net/cooldragon/article/details/18991973 iOS开发中基于ORM的框架很多,如SQLitePersistentObject,实际 ...