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 的范围太大了,所以使 ...
随机推荐
- printf 的 转义词 -转
\n 换行 \r 回车键 \b 退后一格 \f 换页 \t 水平制表符 \v 垂直制表符 \a 发出鸣响 \? 插入问号 \" 插入双引号 \' ...
- 非寻常方式学习ApacheTomcat架构及10.0.12源码编译
概述 开启博客分享已近三个月,感谢所有花时间精力和小编一路学习和成长的伙伴们,有你们的支持,我们继续再接再厉 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Tomcat官 ...
- admire, admit
admire 当别人admire你时,小心掉进泥潭(mire).词源:to wonder. wonderful夸人,awful骂人,awesome夸人.admiral与admire词源不同,碰巧长得像 ...
- acquaint
Interpersonal relationships are dynamic systems that change continuously during their existence. Lik ...
- 数仓day04----日志预处理2
1.详细描述idmap的整个计算方案 (1)使用SparkSession对象读取用户不同类别的埋点日志,解析并抽取出相应的标识id,使用union进行合并,得到装有汇总标识id的rdd(ids) (2 ...
- Lottie 使用
原文:https://mp.weixin.qq.com/s?__biz=MzIxNjc0ODExMA==&mid=2247485033&idx=1&sn=54dd477b4c4 ...
- Spring MVC入门(二)—— URI Builder模式
URI Builder Spring MVC作为一个web层框架,避免不了处理URI.URL等和HTTP协议相关的元素,因此它提供了非常好用.功能强大的URI Builder模式来完成,这就是本文重点 ...
- MyBatis中关于大于,小于写法
第一种写法(1): 原符号 < <= > >= & ' " 替换符号 < <= > >= & ' " ...
- markDodn使用技巧
markdown 标题 一级标题书写语法: 井符(#)加上空格加上标题名称 二级标题书写语法: 两个井符(#)加上空格加上标题名称 三级标题书写语法: 三个井符(#)加上空格加上标题名称 字体 字体加 ...
- hbuilder打包app基本流程
声明:本文可能用到一些工具和第三方网站,都是为了达到目的而使用的工具,绝不含有广告成分 1.下载.最新的Hbuilder X貌似不能直接创建移动app了(自己不会用),建议旧版.可去腾某讯软件中心下载 ...