[CF919E]Congruence Equation

题意:求关于$n$的方程$n\cdot a^n\equiv b\left(mod\ p\right)$在$[1,x]$中整数解的数量

果然是Chinese round,interesting round(幸好没打

首先注意到那个指数很令人痛苦,所以用费马小定理把指数弄掉

令$n=\left(p-1\right)i+j\left(i\geq0,0\leq j\lt p-1\right)$

$\left[\left(p-1\right)i+j\right]a^{\left(p-1\right)i+j}\equiv b$

$\left(p-1\right)i+j\equiv\dfrac{b}{a^j}$

$i\equiv j-\dfrac{b}{a^j}$

所以对于每个给定的$j$,$i$的取值是$j-\dfrac{b}{a^j}+tp$的形式

所以我们可以枚举$0\leq j\lt p-1$,直接按$i\geq0,1\leq n\leq x$统计一下就好

注意减去$i=0$且$j=0$,也就是$n=0$的情况

#include<stdio.h>
#define ll long long
ll a,b,p,x,y,j,r,l,res;
int main(){
	scanf("%I64d%I64d%I64d%I64d",&a,&b,&p,&x);
	r=1;
	for(j=0;j<p-2;j++)r=r*a%p;
	y=b;
	for(j=0;j<p-1;j++){
		l=j-y;
		if(l<0)l+=p;
		if(x>=j&&l<=(x-j)/(p-1)){
			res+=((x-j)/(p-1)-l)/p+1;
			if(l==0&&j==0)res--;
		}
		y=y*r%p;
	}
	printf("%I64d\n",res);
}

原文地址:https://www.cnblogs.com/jefflyy/p/8400614.html

时间: 2024-08-01 12:48:08

[CF919E]Congruence Equation的相关文章

cf 460 E. Congruence Equation 数学题

cf 460 E. Congruence Equation 数学题 题意: 给出一个x 计算<=x的满足下列的条件正整数n的个数 \(p是素数,2?≤?p?≤?10^{6}?+?3, 1?≤?a,?b?<?p, 1?≤?x?≤?10^{12}\) 思路: 题目中存在两个循环节 \(n % p\) 和 \(a ^ n % p\), 循环节分别为\(p,p-1\) 我们枚举\(i = n\ (mod)\ (p - 1)\) 可以得到两个方程 \[ n\ \equiv\ i\ mod\ (p-1)

[Codeforces 919E]Congruence Equation

Description 题库链接 求满足 \[n\cdot a^n\equiv b \pmod{p}\] 的 \(n\) 的个数, \(1\leq n\leq x\) , \(a,b,p,x\) 均已给出. \(2\leq p\leq 10^6+3,1\leq a,b < p, 1\leq x\leq 10^{12}\) , 保证 \(p\) 是质数. Solution 对于 \(x\leq 10^{12}\) 显然不能枚举判断.但我们注意到当关于 \(n_1,n_2\) 的方程,若满足 \(n

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<

hunnu11562:The Triangle Division of the Convex Polygon(第n个卡特兰数取模)

Problem description   A convex polygon with n edges can be divided into several triangles by some non-intersect diagonals. We denote d(n) is the number of the different ways to divide the convex polygon. For example,when n is 6,there are 14 different

HDU 4569 Special equations(取模)

Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4569 Description Let f(x) = a nx n +...+ a 1x +a 0, in which a i (0 <= i <= n) are all known integers. We call f(x) 0 (mod

Codeforces Round #460 (Div. 2) 919 笔记

A. Supermarket 输入n,m, (1?≤?n?≤?5?000, 1?≤?m?≤?100)表示n组价格数据和目标重量m 接下来n组价格数据,表示为a元b千克,每组无限取 求最小花费 B. Perfect Number 输入k,1?≤?k?≤?10?000,求第k个完美数 完美数定义为数位和=10 (tutorial中说难度可以升级为k<1e18)->用数位dp可解 C. Seat Arrangements 输入n,m,k (1?≤?n,?m,?k?≤?2?000)表示nm的课室,有k

2018 CCPC网络赛 几道数学题

1002 Congruence equation 题目链接  : http://acm.hdu.edu.cn/showproblem.php?pid=6439 题解 : https://www.zybuluo.com/yang12138/note/1262592 相关定理 : 裴蜀定理 在数论中,裴蜀定理是一个关于最大公约数(或最大公约式)的定理.裴蜀定理得名于法国数学家艾蒂安·裴蜀,说明了对任何整数a.b和它们的最大公约数d,关于未知数x和y的线性丢番图方程(称为裴蜀等式): ax + by

2018 CCPC网络赛

2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物品,问最终赚的钱的最大值. solution 用两个堆来维护,一个堆维护已经找到卖家的,一个堆维护还没找到卖家的. 对于第\(i\)个地点,在已经找到卖家的堆里找出卖的钱的最小值,如果最小值小于\(a_i\),则将卖家换成\(i\),然后将原来的卖家放到没找到卖家的那里:如果最小值对于\(a_i\)

HDU 2199 Can you solve this equation?(二分搜索)

题目链接 Problem Description Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100;Now please try your lucky. Input The first line of the input contains an integer T(1<=T<=100) which means the number of