HDU5377
题意:给sum,m组询问,每组x,y求\(x^t=y\mod p,p|sum\),p是素数,求最小的t
题解:先处理sum的所有质因子p,求出p的原根rt,\(rt^a=x\mod p,rt^b=y\mod p\),\(rt^{a*t}=rt^b\mod p\),
\(a*t=b\mod p-1\),先预处理bsgs的表,然后求出ab,再exgcd求t,找最小值
//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
//#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll mul(ll a,ll b,ll c){return (a*b-(ll)((ld)a*b/c)*c+c)%c;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=mul(ans,a,c);a=mul(a,a,c),b>>=1;}return ans;}
using namespace std;
//using namespace __gnu_pbds;
const ld pi=acos(-1);
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=1000000+7,maxn=2000000+10,inf=0x3f3f3f3f;
struct prime{
int p,rt;
struct HashTable
{
int top, head[N];
struct Node
{
int x, y, next;//x是原根p的幂,y是幂次数,next是y+1次幂所在的head桶
}node[N];
void init()
{
top = 0;
memset(head, 0, sizeof(head));
}
void Insert(int x, int y)
{
node[top].x = x; node[top].y = y; node[top].next = head[x%N];
head[x%N] = top++;
}
int Find(int x)//求出x是原根proot的几次幂
{
for (int tx = head[x%N]; tx; tx = node[tx].next)
if (node[tx].x == x)return node[tx].y;
return -1;
}
}ma;
void init()
{
ma.init();
int a=rt;
int m=100000,now=1;
ma.Insert(now,0);
for(int i=1;i<=m;i++)
{
now=(1ll*now*a)%p;
ma.Insert(now,i);
}
}
int query(int b)
{
int m=100000;
int ans=-1,t=qp(rt,m,p),now=qp(b,p-2,p);
for(int i=1;i<=m;i++)
{
now=1ll*now*t%p;
int te=ma.Find(now);
if(~te)
{
ans=i*m-te;
break;
}
}
if(ans!=-1)ans=(ans%p+p)%p;
return ans;
}
}p[15];
vi v;
int findroot(int n)
{
int p=n-1;v.clear();
for(int i=2;i*i<=p;i++)if(p%i==0)
{
v.pb(i);
while(p%i==0)p/=i;
}
if(p!=1)v.pb(p);
for(int i=1;i<=n;i++)
{
bool ok=1;
for(int j=0;j<v.size();j++)
if(qp(i,(n-1)/v[j],n)==1)
ok=0;
if(ok)return i;
}
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b){x=1,y=0;return a;}
ll ans=exgcd(b,a%b,x,y);
ll t=x;x=y;y=t-a/b*y;
return ans;
}
int main()
{
int t,cas=0;scanf("%d",&t);
while(t--)
{
int cnt=0;
int s,m;scanf("%d%d",&s,&m);
for(int i=2;i*i<=s;i++)if(s%i==0)
{
p[++cnt].p=i,p[cnt].rt=findroot(i),p[cnt].init();
while(s%i==0)s/=i;
}
if(s!=1)p[++cnt].p=s,p[cnt].rt=findroot(s),p[cnt].init();
printf("Case #%d:\n",++cas);
for(int o = 1; o <= m; o++)
{
int x,y,ans=inf;scanf("%d%d",&x,&y);
for(int i=1;i<=cnt;i++)
{
int a=p[i].query(x),b=p[i].query(y);
assert(a!=-1&&b!=-1);
if(b%gcd(a,p[i].p-1)!=0)continue;
else
{
ll c,d;
exgcd(a,p[i].p-1,c,d);
c=c*b/gcd(a,p[i].p-1);
ll te=(p[i].p-1)/gcd(a,p[i].p-1);
c=(c%te+te)%te;
ans=min(ans,(int)c);
}
}
printf("%d\n",ans==inf?-1:ans);
}
}
return 0;
}
/********************
1
175 2
2 3
********************/
HDU5377的更多相关文章
随机推荐
- ArcGis Python常用脚本
ArcGis Python脚本——ArcGIS 中使用的 Python 是什么版本 ArcGis Python脚本——批量添加字段 ArcGis Python脚本——批量删除字段 ArcGis Pyt ...
- JS数组 选定元素slice() slice() 方法可从已有的数组中返回选定的元素。 语法 arrayObject.slice(start,end)
选定元素slice() slice() 方法可从已有的数组中返回选定的元素. 语法 arrayObject.slice(start,end) 参数说明: 1.返回一个新的数组,包含从 start 到 ...
- 利用字节流文件生成包含多文件的zip文件
InputStream[] inputStreamsList = new InputStream[jsonArr.size()]; String[] fileNameList = new String ...
- 【基础】Hint控制语句执行
mysql常用的hint对于经常使用oracle的朋友可能知道,oracle的hint功能种类很多,对于优化sql语句提供了很多方法.同样,在mysql里,也有类似的hint功能.下面介绍一些常用的. ...
- thinkphp PATH_INFO支持
如果发生在本地测试正常,但是一旦部署到服务器环境后会发生只能访问首页的情况,很有可能是你的服务器或者空间不支持PATH_INFO所致. 系统内置提供了对PATH_INFO的兼容判断处理,但是不能确保在 ...
- 2018-2019-2-20175323 java实验五 网络编程与安全
20175323 java实验五 网络编程与安全 任务一 ①编写MyBC.java实现中缀表达式转后缀表达式的功能 ②编写MyDC.java实现从上面功能中获取的表达式中实现后缀表达式求值的功能 基本 ...
- c++ 11新特性学习1
static_assert 静态断言,特点是编译期的断言检查 assert 运行时期的断言检查 二者参数用法相同
- Spring源码由浅入深系列二 类结构
BeanFactory 上一章中,我们提过Spring的依赖注入容器是BeanFactory.BeanFactory是一个基础接口,它有一个默认实现类:DefaultListableBeanFacto ...
- Date转换为LocalDateTime
一.在Java 8中将Date转换为LocalDateTime 方法1: 将Date转换为LocalDatetime,我们可以使用以下方法: 1.从日期获取ZonedDateTime并使用其方法toL ...
- 面向对象编程思想(OOP)总结
本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性.七大设计原则和23种设计模式之间的关系. 软件开发中疑难问题: 软件复杂庞 ...