Codeforces 919 E Congruence Equation
题目描述
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 .
输入输出样例
2 3 5 8
2
4 6 7 13
1
233 233 10007 1
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的更多相关文章
- cf 460 E. Congruence Equation 数学题
cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...
- E. Congruence Equation
E. Congruence Equation 思路: 中国剩余定理 \(a^n(modp) = a^{nmod(p-1)}(modp)\),那么枚举在\([0,n-2]\)枚举指数 求\(a^i\)关 ...
- 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 题库链接 求满足 \[n\cdot a^n\equiv b \pmod{p}\] 的 \(n\) 的个数, \(1\leq n\leq x\) , \(a,b,p,x\) 均已 ...
- 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) ...
- 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\), ...
- 【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$循环 ...
- Codeforces 919E Congruence Equation ( 数论 && 费马小定理 )
题意 : 给出数 x (1 ≤ x ≤ 10^12 ),要求求出所有满足 1 ≤ n ≤ x 的 n 有多少个是满足 n*a^n = b ( mod p ) 分析 : 首先 x 的范围太大了,所以使 ...
- Codeforces Little Dima and Equation 数学题解
B. Little Dima and Equation time limit per test 1 second memory limit per test 256 megabytes input s ...
随机推荐
- hdu Shell Necklace 5730 分治FFT
Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view, a shell neckl ...
- Sed basic and practice
定义:Sed 是针对数据流的非交谈式编辑器,它在命令行下输入编辑命令并指定文件,然后可以在屏幕上看到编辑命令的输出结果. 好处:Sed 在缓冲区内默认逐行处理数据,所以源文件不会被更改和破坏. 格式: ...
- 【BZOJ2286】【SDOI2011】消耗战 [虚树][树形DP]
消耗战 Time Limit: 20 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一场战争中,战场由n个岛屿和n-1 ...
- [bzoj4516][Sdoi2016]生成魔咒——后缀自动机
Brief Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔咒串 [1,2]. 一个魔咒串 S 的非空字串被称为魔咒串 S 的生 ...
- MS笔试中的一个关于函数返回的“小”题
Which of following C++ code is correct ? A. int f() { ); return *a; } B. int *f() { int a[3] = {1,2, ...
- Python 基础总结
1.执行python脚本的两种方式: 答:1../run.py.shell直接调用python脚本 2.python run.py 调用python 解释器来调用python脚本 5.python单行 ...
- C++格式输出控制
#include<iostream> #include<string> #include<vector> #include<set> #include& ...
- (12)Linux shell之read 用法
Linux shell之read 用法 #!/bin/bash#read 用来读取屏幕输入或是读取文件内容.read -p "please input you name: " ...
- Spark优化之一:分布式下的map操作是闭包
例如对一个JavaPairRDD<String, String>做遍历操作,常见的,我们可以通过先通过collect()操作将它转化为Map对象再进行遍历,也可以使用Spark提供的map ...
- 四维偏序(K-D-Tree+rebuild)
其实只是放个代码,会K-D-Tree的同学看了就知道怎么rebuild了,其实也是很简单粗暴的…… 单独再发一次吧,感觉把代码跟之前的一起发不知道啥时候就找不到了…… #include<bits ...