Root

                                                                         Time Limit: 30000/15000 MS (Java/Others)    Memory
Limit: 262144/262144 K (Java/Others)

                                                                                                           Total Submission(s): 34    Accepted Submission(s): 6

Problem Description
Given a number sum(1≤sum≤100000000),we
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)
 
Input
The first line contains a number T, indicating the number of test cases. 



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)
 
Output
For each test case,output "Case #X:"
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.
 
Sample Input
1
175 2
2 1
2 3
 
Sample Output
Case #1:
0
3
Hint
175 =52∗7 20 mod 5 = 1 23 mod 7 = 1 So the answer to (2,1) is 0
 
Source

比較经典一道扩展欧几里得

如今。我们首先来解决下原根的问题:简单的解释能够參考:>>原根<<

资源下载: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+扩展欧几里得+原根)的更多相关文章

  1. 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 ...

  2. 牛客练习赛52 C 烹饪(容斥+扩展欧几里得)

    来源:https://ac.nowcoder.com/acm/contest/1084/D 思路来源:https://www.cnblogs.com/Morning-Glory/p/11521114. ...

  3. 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个点,求射 ...

  4. 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- ...

  5. UVA 10090 Marbles 扩展欧几里得

    来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...

  6. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

  7. 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】

    Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...

  8. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

  9. poj 2142 扩展欧几里得解ax+by=c

    原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...

随机推荐

  1. 上机题目(0基础)- Java网络操作-Socket实现client和server端通信二(Java)

    上一节实现了client像server端发送请求.本节将实现server端向client回传信息.实现原理非常easy,在原来的基础上.在server端实现输出流,在client实现输入流就可以,详细 ...

  2. django自定义过滤器及模板标签

    创建一个模板库 不管是写自定义标签还是过滤器,第一件要做的事是创建模板库(Django能够导入的基本结构). 创建一个模板库分两步走: 第一,决定模板库应该放在哪个Django应用下. 如果你通过 m ...

  3. Shell--变量的显示与设置、环境变量、语系变量

    1.变量的显示与设置:echo,unsetecho:显示一段文字,也可以读出变量内容并打印出来  格式echo $变量或者echo ${变量}语 法:echo [-neE][字符串]或 echo [- ...

  4. ios开发中APP底部上滑不能调出如WiFi、蓝牙、播放等的设置页面的解决的方法

    在开发的APP中我们通常通过手动底部上滑来调出WiFi.蓝牙.飞行模式等的设置页面.有时我们开发的APP无法调出. 解决的方法: 进入iPhone "设置" --> &quo ...

  5. 计算机速度GHz等于每秒多少次

    这个是没有对比关系的.需要看处理器的微指令.计算机的GHz指的是CPU的时钟频率,不同的指令需要的时钟个数是不同的,列如,从内存读一个数据,一般需要4个时钟周期,而进行一次8位数的加法运算,一般需要1 ...

  6. java根据身份证号和获取用户年龄和性别的工具类

    import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util ...

  7. 一步一步学习Unity3d学习笔记系1.3 英雄联盟服务器集群架构猜想

    说到了网游那就涉及到服务器了,时下最火的属英雄联盟了,我也是它的粉丝,每周必撸一把,都说小撸怡情,大撸伤身,强撸灰飞烟灭,也告诫一下同仁们,注意身体,那么他的服务器架构是什么呢,给大家分享一下, 具体 ...

  8. jquery 获取table当前行值

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  9. 用键盘控制DIV && Div闪烁

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. javascript - 活动倒计时(天、时、分、秒)

    计数时: 结束时: 示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...