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 ...
随机推荐
- Android 设置进度条背景
Android 设置进度条背景 直接上代码 <ProgressBar android:id="@+id/progressBar" android:layout_width=& ...
- ref、out
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ref_ ...
- 转:Unicode汉字编码表
转自:http://blog.csdn.net/huangxy10/article/details/10012119 Unicode汉字编码表 1 Unicode编码表 Unicode只有一个字符集 ...
- mixamo fuse三维角色制作
软件下载: http://www.cgtsj.com/cg/yj/1302/index.html 资源名称: Mixamo Fuse三维角色制作软件V1.3版 本站编号: YJ1302 百度网盘:下 ...
- C# winform程序怎么打包成安装项目(图解)
1:新建安装部署项目 打开VS,点击新建项目,选择:其他项目类型->安装与部署->安装向导(安装项目也一样),然后点击确定.(详细见下图) 此主题相关图片如下: 2:安装向导 关闭后打开安 ...
- 批量Load/Store指令的寻址方式
批量Load/Store指令用于实现在一组寄存器和一块连续的内存单元之间传输数据.也称为多寄存器寻址方式,即一条指令可以完成多个寄存器值的传送.这种寻址方式可以用一条指令最多完成传送16个通用寄存器的 ...
- dubug
1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ============================= 作用域 功能 快捷键 全局 ...
- Android开发--Activity的创建
1.Activity概述 Activity是android四大基本组件之一.每一个activity文件对应一个界面,一个程序由多个activity组成. 2.Android工作目录
- Metro UI(Win 8风格)页面设计小记
一.Metro风格菜单——简单 HTML <div class="pagina "> <div class="linha"> <d ...
- Java--常用类summary
/* 2:API的概述(了解) (1)应用程序编程接口. (2)就是JDK提供给我们的一些提高编程效率的java类. 3:Object类(掌握) (1)Object是类层次结构的根类,所有的类都直接或 ...