Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7
Root
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 34 Accepted Submission(s): 6
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)
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.
175 =5^2∗7
2^0 mod 5 = 1
2^3 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
转载请注明出处:寻找&星空の孩子
#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的更多相关文章
- Root(hdu5777+扩展欧几里得+原根)
Root Time Limit: 30000/1500 ...
- 牛客练习赛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) 先用扩展欧几里 ...
随机推荐
- git的认识2
我的GitHub 地址:https://github.com/lvjiaze130 唯一的感觉就是厉害了,还有看了一些个教学视频发现他们在git bush上打的东西我竟然都看不懂,是英文吗?太不好懂了 ...
- 学习C++后感
c++是一门系统级语言,记得大一是要学习这门课时还上网找过学习方法.但网上很多学习方法看的我都头晕,都说学习C++很难,有的说学C++前最好先学C语言,有的说学C++最好不要学C语言,当翻了C++课本 ...
- php安全编程&python测试实例编写
前言 本文首发i春秋论坛. 本篇文章主要分享一个python暴破脚本,该脚本采用optparse模块,支持自定义字典位置:用了多线程(虽然我感觉和单线程速度差不多..是我的错觉还是线程写的不对..求表 ...
- Kubernetes---DaemonSet
DaemonSet用于管理在集群中每个Node上仅运行一份Pod的副本实例. kind: DaemonSet
- js控制全屏及退出全屏
js控制全屏及退出全屏,网上很多代码例子,我这里需求和标准的有点出入: 1.当用户点击某按钮,触发iframe下的页面全屏. 2.不允许用户退出全屏. 解决第一点,触发全屏可以按照网上的例子,代码如下 ...
- SpringBoot中集成redis
转载:https://www.cnblogs.com/zeng1994/p/03303c805731afc9aa9c60dbbd32a323.html 不是使用注解而是代码调用 需要在springbo ...
- shell脚本中if的“-e,-d,-f”
文件表达式-e filename 如果 filename存在,则为真-d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真-L ...
- Linux的 Shell 理解和使用
Shell为命令解释器,把用户命令解释为内核可识别的机器语言,然后翻译命令结果给用户,其也是一个强大的编程语言,是解释执行的脚本语言,无编译,与python机制基本相同. Shell内部可以直接调用L ...
- [干货,阅后进BAT不是梦]面试心得与总结---阿里、小米、腾讯
之前实习的时候就想着写一篇面经,后来忙就给忘了,现在找完工作了,也是该静下心总结一下走过的路程了,我全盘托出,奉上这篇诚意之作,希望能给未来找工作的人一点指引和总结, 也希望能使大家少走点弯路 , 如 ...
- 项目总结一:情感分类项目(emojify)
一.Emojifier-V1 模型 1. 模型 (1)前向传播过程: (2)损失函数:计算the cross-entropy cost (3)反向传播过程:计算dW,db dz = a - Y_oh[ ...