E. Congruence Equation
E. Congruence Equation
思路:
中国剩余定理
\(a^n(modp) = a^{nmod(p-1)}(modp)\),那么枚举在\([0,n-2]\)枚举指数
求\(a^i\)关于p的逆元\(ni\)得原式为\(k = ni*b(modp)\),那么可以得到两个式子
\(1.ni*b = n(modp)\)
\(2.i = n(mod(p-1))\)
然后通过中国剩余定理求出最小非负整数解t,那么通解\(t + s*(p*(p-1))\)
用除法在x范围内求下个数即可。复杂度\(log(n)\).
题链
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL quick(LL n,LL m,LL mod);
pair<LL,LL>ex_gcd(LL n,LL m);
int main(void)
{
LL a,b,p,x;
scanf("%lld %lld %lld %lld",&a,&b,&p,&x);
LL mod = p*(p-1);
LL sum = 0;
LL ask = 0;
for(LL i = 0;i < p-1;i++)
{
LL a_i = quick(a,i,p);
LL a_ni = quick(a_i,p-2,p);
LL a_b = b*a_ni%p;
sum = (a_b*(p-1)*(p-1)%mod + i*p*p%mod)%mod;
//cout<<sum<<endl;
if(x >= sum)
{
ask+=1;
ask += (x - sum)/mod;
}
}
printf("%lld\n",ask);
return 0;
}
LL quick(LL n,LL m,LL mod)
{
n%=mod;
LL ask = 1;
while(m)
{
if(m&1)
ask = ask*n%mod;
n = n*n%mod;
m/=2;
}
return ask;
}
pair<LL,LL>ex_gcd(LL n,LL m)
{
if(m == 0)
return make_pair(1,0);
else
{
pair<LL,LL>ans = ex_gcd(m,n%m);
return make_pair(ans.second,ans.first - n/m*ans.second);
}
}
E. Congruence Equation的更多相关文章
- cf 460 E. Congruence Equation 数学题
cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2 ≤ p ≤ 10^{6} + 3, 1 ≤ a ...
- [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 919 E Congruence Equation
题目描述 Given an integer xx . Your task is to find out how many positive integers nn ( 1<=n<=x1&l ...
- [CF919E]Congruence Equation
题意:求关于$n$的方程$n\cdot a^n\equiv b\left(mod\ p\right)$在$[1,x]$中整数解的数量 果然是Chinese round,interesting roun ...
- 【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 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 ( 数论 && 费马小定理 )
题意 : 给出数 x (1 ≤ x ≤ 10^12 ),要求求出所有满足 1 ≤ n ≤ x 的 n 有多少个是满足 n*a^n = b ( mod p ) 分析 : 首先 x 的范围太大了,所以使 ...
随机推荐
- a.out的由来
用过linux的都知道,在linux下编译链接程序,如果不加-o参数,生成的binary代码的名字都是默认的a.out.一不小心,a.out还会覆盖上次其他code生成的binary代码. a.out ...
- mysql—MySQL数据库中10位或13位时间戳和标准时间相互转换
1.字符串时间转10位时间戳 select FLOOR(unix_timestamp(create_time)) from page; #create_time为字段名 page为表名 eg:sele ...
- Markdown-写作必备
Markdown--入门指南 导语: Markdown 是一种轻量级的「标记语言」,它的优点很多,目前也被越来越多的写作爱好者,撰稿者广泛使用.看到这里请不要被「标记」.「语言」所迷惑,Markdow ...
- LetNet、Alex、VggNet分析及其pytorch实现
简单分析一下主流的几种神经网络 LeNet LetNet作为卷积神经网络中的HelloWorld,它的结构及其的简单,1998年由LeCun提出 基本过程: 可以看到LeNet-5跟现有的conv-& ...
- 9 — springboot整合jdbc、druid、druid实现日志监控 — 更新完毕
1.整合jdbc.druid 1).导入依赖 <dependency> <groupId>org.springframework.boot</groupId> &l ...
- C/C++ Qt 数据库与TreeView组件绑定
在上一篇博文<C/C++ Qt 数据库QSql增删改查组件应用>介绍了Qt中如何使用SQL操作函数,并实现了对数据库的增删改查等基本功能,从本篇开始将实现数据库与View组件的绑定,通过数 ...
- 学习java的第二十三天
一.今日收获 1.java完全学习手册第三章算法的3.2排序,比较了跟c语言排序上的不同 2.观看哔哩哔哩上的教学视频 二.今日问题 1.快速排序法的运行调试多次 2.哔哩哔哩教学视频的一些术语不太理 ...
- Hadoop入门 常见错误及解决方案
常见错误及解决方案 目录 常见错误及解决方案 ResourceManager连接失败 root用户和ranan用户两个用户启动集群不统一 不识别主机名 DataNode和NameNode进程同时只能工 ...
- A Child's History of England.34
'Prince!' said Fitz-Stephen, 'before morning, my fifty and The White Ship shall overtake [超过, 别和take ...
- day10 ajax的基本使用
day10 ajax的基本使用 今日内容 字段参数之choices(重要) 多对多的三种创建方式 MTV与MVC理论 ajax语法结构(固定的) 请求参数contentType ajax如何传文件及j ...