HDU5478 原根求解
看别人做的很简单我也不知道是怎么写出来的
自己拿到这道题的想法就是模为素数,那必然有原根r ,将a看做r^a , b看做r^b
那么只要求出幂a,b就能得到所求值a,b
自己慢慢化简就会发现可以抵消n
然后扩展欧几里得解决,一个个枚举所有模的情况。。。。
中间利用了欧拉准则可以知道 对所有奇素数而言: a^((p-1)/2) = -1(mod p)
利用上述准则,这样就不用baby_step giant_step的办法了
#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N = ; LL P , k1,b1 , k2;
bitset<N> prime;
int p[N],pri[N];
int k,cnt; void isprime()
{
prime.set();
for(int i=; i<N; i++)
{
if(prime[i])
{
p[k++] = i;
for(int j=i+i; j<N; j+=i)
prime[j] = false;
}
}
} void Divide(int n)
{
cnt = ;
int t = (int)sqrt(1.0*n+0.5);
for(int i=; p[i]<=t; i++)
{
if(n%p[i]==)
{
pri[cnt++] = p[i];
while(n%p[i]==) n /= p[i];
}
}
if(n > )
pri[cnt++] = n;
} void gcd(LL a , LL b , LL &d , LL &x , LL &y)
{
if(!b){d=a;x=;y=;}
else{gcd(b,a%b,d,y,x);y-=x*(a/b);}
} LL inv(LL a , LL n)
{
LL d,x,y;
gcd(a,n,d,x,y);
return d==?(x+n)%n:-;
} LL quick_mod(LL a,LL b,LL m)
{
LL ans = ;
a %= m;
while(b)
{
if(b&)
{
ans = ans * a % m;
b--;
}
b >>= ;
a = a * a % m;
}
return ans;
} LL pow_mod(LL a , LL p , LL n)
{
LL ret= ;
while(p){
if(p&) ret = ret*a%n;
a = a*a%n;
p>>=;
}
return ret;
} LL mul_mod(LL a , LL b , int n)
{
return a*b%n;
} //int log_mod(int a , int b , int n)
//{
// int m , v , e=1 , i;
// m = (int)sqrt(n+0.5);
// v = inv(pow_mod(a,m,n) , n);
// map<int,int>x;
// x.clear();
// x[1] = 0;
// for(i=1 ; i<m ; i++){
// e = mul_mod(e , a , n);
// if(!x.count(e)) x[e]=i;
// }
// for(i=0 ; i<m ; i++){
// if(x.count(b)) return i*m+x[b];
// b = mul_mod(b,v,n);
// }
// return -1;
//} #define pii pair<int,int>
pii ans[N]; int main()
{
// freopen("a.in" , "r" , stdin);
int cas = ;
isprime();
//>>k1>>b1>>k2
while(cin>>P>>k1>>b1>>k2)
{
printf("Case #%d:\n" , ++cas);
int tot = ;
/*P==2另外处理*/
if(P==){
printf("%d %d\n" , ,);
continue;
}
if(P==){
printf("%d %d\n" , ,);
continue;
}
Divide(P-); for(int g=; g<P; g++)
{
bool flag = true;
for(int i=; i<cnt; i++)
{
int t = (P - ) / pri[i];
if(quick_mod(g,t,P) == )
{
flag = false;
break;
}
}
if(flag)
{
LL root = g;
LL val = (P-)/;
// cout<<root<<" "<<val<<endl;
LL d,x,y;
gcd(b1+k1 , - , d , x , y);
x = x*val , y = y*val; x = ((x%(P-))+(P-))%(P-);
y = ((y%(P-))+(P-))%(P-);
for(int i= ; i<P- ; i++){ if((x*k1-y*k2)%(P-)==){
LL a = pow_mod(root , x , P);
LL b = pow_mod(root , y , P);
if(a>&&b>) ans[tot++] = make_pair((int)a , (int)b);
}
x = (x+)%(P-);
y = (y+b1+k1)%(P-);
}
break;
}
}
sort(ans , ans+tot);
tot = unique(ans , ans+tot)-ans;
if(tot==) puts("-1");
else{
for(int i= ; i<tot ; i++) printf("%d %d\n" , ans[i].first , ans[i].second);
}
}
return ;
}
HDU5478 原根求解的更多相关文章
- 原根求解算法 && NTT算法
原根求解算法: 获取一个数\(N\)的原根\(root\)的算法 #include<bits/stdc++.h> #define ll long long #define IL inlin ...
- bzoj2219: 数论之神
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- codeforces#536题解
CodeForces#536 A. Lunar New Year and Cross Counting Description: Lunar New Year is approaching, and ...
- 【bzoj2219-数论之神】求解x^a==b(%n)-crt推论-原根-指标-BSGS
http://www.lydsy.com/JudgeOnline/problem.php?id=2219 弄了一个晚上加一个午休再加下午一个钟..终于ac..TAT 数论渣渣求轻虐!! 题意:求解 x ...
- x^a=b(mod c)求解x在[0,c-1]上解的个数模板+原根求法
/************************************* 求解x^a=b(mod c) x在[0,c-1]上解的个数模板 输入:1e9>=a,b>=1,1e9>= ...
- 51nod1135(求最小原根)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1135 题意:中文题诶- 思路:设m是正整数,a是整数,若a模 ...
- 数论算法 剩余系相关 学习笔记 (基础回顾,(ex)CRT,(ex)lucas,(ex)BSGS,原根与指标入门,高次剩余,Miller_Rabin+Pollard_Rho)
注:转载本文须标明出处. 原文链接https://www.cnblogs.com/zhouzhendong/p/Number-theory.html 数论算法 剩余系相关 学习笔记 (基础回顾,(ex ...
- CF1106F Lunar New Year and a Recursive Sequence 原根、矩阵快速幂、BSGS
传送门 好久没写数论题了写一次调了1h 首先发现递推式是一个乘方的形式,线性递推和矩阵快速幂似乎都做不了,那么是否能够把乘方运算变成加法运算和乘法运算呢? 使用原根!学过\(NTT\)的都知道\(99 ...
- 数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho
数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p ...
随机推荐
- Single Number III
Description: Given an array of numbers nums, in which exactly two elements appear only once and all ...
- golang时间
//获取本地location toBeCharge := "2015-01-01 00:00:00" //待转化为时间戳的字符串 注意 这里的小时和分钟还要秒必须写 因为是跟着模板 ...
- Bootstrap列表
一.HTML的列表 在HTML文档中,列表结构主要有三种:有序列表.无序列表和定义列表.具体使用的标签说明如下: 1.无序列表 <ul> <li>…</li> &l ...
- root的方法大体上有以下三种
root的方法大体上有以下三种一.手机软件安卓版直接root.这种方法不需要电脑的支持,也很安全.安卓版软件有:kingroot,360一键root,一键root大师,Towelroot,支持云roo ...
- libCEF总结01下载、编译、入门
目 录 第1章 下载 1 1.1 下载 1 1.2 合并 1 第2章 cmake 4 2.1 编译简介 4 2.2 下载cmake 4 2.3 运行cmake ...
- java 集合(ArrayList)
ArrayList: ------------|Collection 单列集合的跟接口 ----------------------|List 有序,可重复. ------------------- ...
- Show()和ShowDialog()
show()仅仅是显示出来窗口界面而已```也就是和你执行的结果在同一窗口显示```所显示的窗口可以在后台运行```而showDialog()是一个对话框窗口界面```执行结果以新窗口界面出现```不 ...
- js 返回上一页和刷新
1. Javascript 返回上一页history.go(-1), 返回两个页面: history.go(-2); 2. history.back(). 3. window.history.forw ...
- JavaScript基础知识(1)
表单的确认 : 客户端确认 --减少服务器负载 --缩短用户等待时间 --兼容性难 服务器端确认: ----统一确认 ----兼容性强 ----服务器负载重 JavaScript基本的写法: ...
- 初学java之异常类
//异常类 package st; public class example_1 { public static void main(String args[]) { int n=0,m=0,t=10 ...