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

\[ n \equiv \frac{b}{a ^ i}\ mod\ p\]

令$mm = \frac{b}{a ^ i} $

设 \[n = p * k + mm , n = (p - 1) * q + i \]

于是\(p * k + mm = p * q - q + i\)

在模p意义下可以得到

\(q \equiv (i - mm)\ (mod) p\)

然后就可以根据限制条件计算出有多少个满足条件的q 即答案了


#include<bits/stdc++.h>
#define LL long long
using namespace std;

LL qpow(LL x,LL y,LL mod){
    x %= mod;
    LL ans = 1;
    while(y){
        if(y & 1) ans = ans * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return ans;
}
int main(){

    LL a,b,p,X;
    cin>>a>>b>>p>>X;
    LL x,y,m = b,inva = qpow(a, p - 2,p);
    LL ans = 0;
    for(int i = 0;i < p - 1;i++){
        LL mm = (i - m + p) % p;
        LL R = (floor)((1.0 * (X - i) / (p -1) - mm)/p) ;
        LL L = mm / p;
       // for(int j = L;j <= R;j++) cout<<(p * j + mm) * (p - 1) + i<<" ";
        m = m * inva % p;
        ans += R - L + 1;
    }
    cout<<ans<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/jiachinzhao/p/8406784.html

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

cf 460 E. Congruence Equation 数学题的相关文章

[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 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<

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

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

第二周 3.6-3.12

3.6 CF 627 A XOR Equation a + b = a ^ b + (a & b << 1) 注意非法情况和0. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 6 int main(void) 7 { 8 LL s, x, t, ans = 1LL; 9 cin >> s >> x; 10

2018 CCPC网络赛

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