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 =5^2∗7

2^0 mod 5 = 1

2^3 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+;
const int maxv=1e5+;
bool isnp[maxv];
int prime[maxv],pnum;//素数数组,素数个数
int cas;
void get_prime()//素数打表
{
pnum=;
int i,j;
memset(isnp,,sizeof(isnp));
isnp[]=isnp[]=true;
for(i=; i<maxv; i++)
{
if(!isnp[i])prime[pnum++]=i;
for(j=; j<pnum&&prime[j]*i<maxv; j++)
{
isnp[i*prime[j]]=true;
if(i%prime[j]==)break;
}
}
}
ll qukpow(ll k,ll base,ll p)
{
ll res=;
for(; k; k>>=)
{
if(k&)res=(res*base)%p;
base=(base*base)%p;
}
return res;
}
ll ppow(ll a,ll b,ll mod)
{
ll c=;
while(b)
{
if(b&) c=c*a%mod;
b>>=;
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=,num=p-,res;
int i;
if(p==)return ;
for(i=; i<pnum && prime[i]*prime[i]<=num && num> ; i++)
{
if(num%prime[i]==)//
{
fpr[cnt++]=prime[i];
while(num%prime[i]==)num/=prime[i];
}
}
if(num>)fpr[cnt++]=num;//fpr[]存的是p-1的因子
for(res=; res<=p-; res++)//暴力
{
for(i=; i<cnt; i++)
if(ppow(res,p/prime[i],p)==)break;
if(i>=cnt)return res;
}
return -;
}; const int mod=1e6+; struct solve
{
struct HashTable
{
int top,head[mod];
struct Node
{
int x,y,next;
} node[mod];
void init()
{
top=;
memset(head,,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 -;
}
} mp; ll p;
ll discretelog(ll prt,ll a) //取对数
{
ll res,am=ppow(prt,maxn-,p),inv=ppow(a,p-,p),x=;
for(ll i=maxn-;; i+=(maxn-))
{
if((res=mp.query((x=x*am%p)*inv%p))!=-)
{ return i-res;
}
if(i>p)break;
}
return -;
}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)//扩展欧几里得 x为最后需要的k
{
if(!b)
{
d=a;
x=;
y=;
}
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=,tmp=; i<maxn-; 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==)return ;
else if(x==)
{
if(y==)return ;
else return -;
}
else if(y==)return -;
else
{
ll s=discretelog(proot,x); ll t=discretelog(proot,y); ex_gcd(s,p-,d,x,y);
if(t%d)return -;
else
{
ll dx=(p-)/d;
x=(x%dx+dx)%dx;
x*=(t/d);
x=(x%dx+dx)%dx;
return x;
}
}
}
} sol[];
int main()
{
int i,j,q,con,T;
ll sum,x,y;
scanf("%d",&T);
get_prime();
cas=;
while(cas<=T)
{
con=;
scanf("%I64d %d",&sum,&q); for(i=; i<pnum&&prime[i]*prime[i]<=sum&&sum!=; i++)
{
if(sum%prime[i]==)//素数存起来
{
sol[con].p=prime[i];
sol[con].init();
con++;
while(sum%prime[i]==)sum/=prime[i];
}
}
if(sum>)
{
sol[con].p=sum;
sol[con].init();
con++;
} printf("Case #%d:\n",cas++); for(i=; i<q; i++)
{
scanf("%lld %lld",&x,&y); ll res=1e18,tmp;
for(j=; j<con; j++)
{ tmp=sol[j].query(x,y);
if(tmp!=-)res=min(res,tmp);
}
if(res==1e18)res=-;
printf("%I64d\n",res);
}
}
return ;
}
 

Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7的更多相关文章

  1. Root(hdu5777+扩展欧几里得+原根)

    Root                                                                          Time Limit: 30000/1500 ...

  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. Openvswitch手册(2): OpenFlow Controller

         我们这一节主要来看Controller Controller有两种: Primary Controller: 真正控制vswitch的flow table,vswitch会保持和contro ...

  2. vs链接错误解决方法

    常见引起链接错误的主要原因是由于项目不能找到所需的动态库的路径: 这里介绍一下引用第三方动态库的配置方法: 方法一: vs加载动态库需要先把动态库拷贝到exe所在文件夹,再修改项目属性: 链接器-&g ...

  3. 《JavaScript》高级程序设计第21章:Ajax和Comet,jsonp

    一.创建XMLHttpRequest对象 二.XHR的用法 五.跨域资源共享 六.其他跨域技术七.安全七.安全 1. 图像Ping 2. JSONP(JSON with padding,填充式JSON ...

  4. JAVAEE企业级应用开发浅谈之MVC 中的V-VIEW视图

    Step1.情景概要 Hello,小伙伴们,好久不见,之前跟大家分享了三层架构与MVC思想,相信大家对于这两块内容有了相对清晰的个人认识了,既然我们讲到了MVC,这里我们接着这块内容继续往下深入,今天 ...

  5. Python的串口通信(pyserial)

    串口通信是指外设和计算机间,通过数据信号线 .地线.控制线等,按位进行传输数据的一种通讯方式.这种通信方式使用的数据线少,在远距离通信中可以节约通信成本,但其传输速度比并行传输低.串口是计算机上一种非 ...

  6. tensorflow笔记4:函数:tf.assign()、tf.assign_add()、tf.identity()、tf.control_dependencies()

    函数原型: tf.assign(ref, value, validate_shape=None, use_locking=None, name=None)   Defined in tensorflo ...

  7. React 实现一个时钟

    最终效果 其实主要难点在于最左边的小时钟 指针的实现方式很简单,就是通过绝对定位将指针移到中间,然后以下边中间的位置为圆心旋转即可.代码如下: <!DOCTYPE html> <ht ...

  8. [CERC2014] Virus synthesis

    设f[i]为形成极长回文串i的最小操作数.答案为min f[i]+n-len[i]. 在不形成偶回文的情况下形成奇回文的最小操作数为该串长度.可以不考虑(但ans赋为len). 正确性基于: 1)奇. ...

  9. CentOS 7配置MariaDB允许指定IP远程连接数据库

    防火墙 CentOS7 之前的防火墙是不一样的,比如你要添加3306端口: ## 全部 iptables -A INPUT -p tcp -m tcp --dport 3306 -j ACCEPT # ...

  10. centos 7 linux 安装与卸载 tomcat 7

    一.声明 本文采用操作系统版本: Centos 7 Linux系统 版本源:CentOS-7-x86_64-DVD-1708.iso 官网下载地址:http://isoredirect.centos. ...