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 .

输入输出样例

输入样例#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;
}

  

原文地址:https://www.cnblogs.com/JYYHH/p/8407239.html

时间: 2024-08-01 12:47:58

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,?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

[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

Codeforces 919 D Substring

题目描述 You are given a graph with nn nodes and mm directed edges. One lowercase letter is assigned to each node. We define a path's value as the number of the most frequently occurring letter. For example, if letters on a path are "abaca", then th

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

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

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\)