Root(hdu5777+扩展欧几里得+原根)
Root
Time Limit: 30000/15000 MS (Java/Others) Memory
Limit: 262144/262144 K (Java/Others)
Total Submission(s): 34 Accepted Submission(s): 6
have m queries
which contains a pair (xi,yi)
and would like to know the smallest nonnegative integer kisatisfying xkii=yi mod p when
the prime number p (sum mod p=0)(ps:00=1)
For each case, each case contains two integers sum,m(1≤sum≤100000000,1≤m≤100000) in
the first line.
The next m lines
will contains two intgeers xi,yi(0≤xi,yi≤1000000000)
and m lines.(X is
the case number)
Each line cotain a integer which is the smallest integer for (xi,yi)
,if we can't find such a integer just output "-1" without quote.
1
175 2
2 1
2 3
Case #1:
0
3Hint175 =52∗7 20 mod 5 = 1 23 mod 7 = 1 So the answer to (2,1) is 0
比較经典一道扩展欧几里得
如今。我们首先来解决下原根的问题:简单的解释能够參考:>>原根<<
资源下载:http://download.csdn.net/detail/u010579068/8993383
不急看懂的,能够先去切道 定义题 链接:1135 原根
解题 http://www.cnblogs.com/yuyixingkong/p/4722821.html
转载请注明出处:寻找&星空の孩子
题目链接:http://acm.hdu.edu.cn/showproblem.php?
pid=5377
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;
typedef long long ll;
const int maxn=1e6+1;
const int maxv=1e5+1;
bool isnp[maxv];
int prime[maxv],pnum;//素数数组,素数个数
int cas;
void get_prime()//素数打表
{
pnum=0;
int i,j;
memset(isnp,0,sizeof(isnp));
isnp[0]=isnp[1]=true;
for(i=2; i<maxv; i++)
{
if(!isnp[i])prime[pnum++]=i;
for(j=0; j<pnum&&prime[j]*i<maxv; j++)
{
isnp[i*prime[j]]=true;
if(i%prime[j]==0)break;
}
}
}
ll qukpow(ll k,ll base,ll p)
{
ll res=1;
for(; k; k>>=1)
{
if(k&1)res=(res*base)%p;
base=(base*base)%p;
}
return res;
}
ll ppow(ll a,ll b,ll mod)
{
ll c=1;
while(b)
{
if(b&1) c=c*a%mod;
b>>=1;
a=a*a%mod;
}
return c;
}
ll fpr[maxv]; ll find_primitive_root(ll p)//求p的原根 g^(p-1) = 1 (mod p); 求g
{
ll cnt=0,num=p-1,res;
int i;
if(p==2)return 1;
for(i=0; i<pnum && prime[i]*prime[i]<=num && num>1 ; i++)
{
if(num%prime[i]==0)//
{
fpr[cnt++]=prime[i];
while(num%prime[i]==0)num/=prime[i];
}
}
if(num>1)fpr[cnt++]=num;//fpr[]存的是p-1的因子
for(res=2; res<=p-1; res++)//暴力
{
for(i=0; i<cnt; i++)
if(ppow(res,p/prime[i],p)==1)break;
if(i>=cnt)return res;
}
return -1;
}; const int mod=1e6+7; struct solve
{
struct HashTable
{
int top,head[mod];
struct Node
{
int x,y,next;
} node[mod];
void init()
{
top=0;
memset(head,0,sizeof(head));
}
void insert(ll x,ll y)
{
node[top].x=x;
node[top].y=y;
node[top].next=head[x%mod];
head[x%mod]=top++;
}
ll query(ll x)
{
for(int tx=head[x%mod]; tx; tx=node[tx].next)
if(node[tx].x==x)return node[tx].y;
return -1;
}
} mp; ll p;
ll discretelog(ll prt,ll a) //取对数
{
ll res,am=ppow(prt,maxn-1,p),inv=ppow(a,p-2,p),x=1;
for(ll i=maxn-1;; i+=(maxn-1))
{
if((res=mp.query((x=x*am%p)*inv%p))!=-1)
{ return i-res;
}
if(i>p)break;
}
return -1;
}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)//扩展欧几里得 x为最后须要的k
{
if(!b)
{
d=a;
x=1;
y=0;
}
else
{
ex_gcd(b,a%b,d,y,x);
y-=x*(a/b);
}
} ll proot;
void init()
{
mp.init();
ll tmp,x,y,d;
int i;
proot=find_primitive_root(p);//找到素数p的原根
for(i=0,tmp=1; i<maxn-1; i++,tmp=tmp*proot%p)
mp.insert(tmp%p,i*1ll);
}
ll query(ll x,ll y)
{
ll d;
x%=p;
y%=p; if(y==1)return 0;
else if(x==0)
{
if(y==0)return 1;
else return -1;
}
else if(y==0)return -1;
else
{
ll s=discretelog(proot,x); ll t=discretelog(proot,y); ex_gcd(s,p-1,d,x,y);
if(t%d)return -1;
else
{
ll dx=(p-1)/d;
x=(x%dx+dx)%dx;
x*=(t/d);
x=(x%dx+dx)%dx;
return x;
}
}
}
} sol[32];
int main()
{
int i,j,q,con,T;
ll sum,x,y;
scanf("%d",&T);
get_prime();
cas=1;
while(cas<=T)
{
con=0;
scanf("%I64d %d",&sum,&q); for(i=0; i<pnum&&prime[i]*prime[i]<=sum&&sum!=1; i++)
{
if(sum%prime[i]==0)//素数存起来
{
sol[con].p=prime[i];
sol[con].init();
con++;
while(sum%prime[i]==0)sum/=prime[i];
}
}
if(sum>1)
{
sol[con].p=sum;
sol[con].init();
con++;
} printf("Case #%d:\n",cas++); for(i=0; i<q; i++)
{
scanf("%lld %lld",&x,&y); ll res=1e18,tmp;
for(j=0; j<con; j++)
{ tmp=sol[j].query(x,y);
if(tmp!=-1)res=min(res,tmp);
}
if(res==1e18)res=-1;
printf("%I64d\n",res);
}
}
return 0;
}
Root(hdu5777+扩展欧几里得+原根)的更多相关文章
- Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7
Root Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Su ...
- 牛客练习赛52 C 烹饪(容斥+扩展欧几里得)
来源:https://ac.nowcoder.com/acm/contest/1084/D 思路来源:https://www.cnblogs.com/Morning-Glory/p/11521114. ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- UVA 12169 Disgruntled Judge 枚举+扩展欧几里得
题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
- 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】
Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- poj 2142 扩展欧几里得解ax+by=c
原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...
随机推荐
- Android.mk入门(一)
Android.mk是Android工程管理文件,其作用基本等同于Linux环境中的Makefile,在语法上,Android.mk和普通Makefile略有不同,主要区别是Android.mk包含一 ...
- VBO与VAO 【转】
我想大家都已经熟悉VBO了吧.在GL3.0时代的VBO大体还是处于最重要的地位,但是与此同时也出现了不少新的用法和辅助役,其中一个就是VAO.本文大致小记一下这两者的联系,帮助大家理解一下这个角色.— ...
- 转: 由socket的accept说开去
from: http://ticktick.blog.51cto.com/823160/779866 今天与同学争执一个话题:由于socket的accept函数在有客户端连接的时候产生了新的socke ...
- SSO单点登录系列4:cas-server登录页面自定义修改过程(jsp页面修改)
落雨 cas 单点登录 SSO单点登录系列4:cas-server登录页面自定义修改过程,全新DIY. 目标: 下面是正文: 打开cas的默认首页,映入眼帘的是满眼的中文and英文混杂体,作为一 ...
- Convert WebP to PNG using java
WebP是谷歌的图片格式,java 类库imageio 是不支持此种格式的.眼下除了在线转换以及工具以外,第三方类库转换webp格式大致有: 1.linux:Google libwebp 既是类库也能 ...
- SQLiteDatabase中query、insert、update、delete方法参数说明
1.SQLiteDataBase对象的query()接口: public Cursor query (String table, String[] columns, String selection, ...
- Android源代码下载
清华大学AOSP镜像: https://mirrors.tuna.tsinghua.edu.cn/help/AOSP/
- VS中c++文件调用c 函数 ,fatal error C1853 预编译头文件来自编译器的早期版本号,或者预编译头为 C++ 而在 C 中使用它(或相反)
出现错误:error C1853: "Debug\ConsoleApplication1.pch"预编译头文件来自编译器的早期版本号.或者预编译头为 C++ 而在 C 中使用它(或 ...
- iOS使用自己定义字体
1.加入相应的字体(.ttf或.odf)到project的resurce,比如my.ttf. 2.在info.plist中加入一项 Fonts provided by application (ite ...
- Jenkins安装火线fireline插件
原文请访问:http://magic.360.cn/zh/user.html 提示:如果您是第一次使用Jenkins,请先前往文章[Jenkins下载安装配置教程] 1. 点击左上角的`Jenkins ...