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. SSAS 内部错误:操作未能成功

    错误 -1056964601 : 内部错误: 操作未能成功,已终止. HY0008 是数据源的问题,设置数据源的虚拟连接就可以了

  2. [编译] 3、在Linux下搭建51单片机的开发烧写环境(makefile版)

    星期二, 10. 七月 2018 01:01上午 - beautifulzzzz 一.SDCC(Small Device C Compiler)编译环境搭建 SDCC是一个小型设备的C语言编译器,该编 ...

  3. 背水一战 Windows 10 (121) - 后台任务: 推送通知

    [源码下载] 背水一战 Windows 10 (121) - 后台任务: 推送通知 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 推送通知 示例演示如何接收推送通知/WebA ...

  4. 知乎十万级容器规模的Java分布式镜像仓库实践

    作者:知乎令孤无忌 前言 知乎在 2016 年已经完成了全量业务的容器化,并在自研容器平台上以原生镜像的方式部署和运行,并在后续陆续实施了 CI.Cron.Kafka.HAProxy.HBase.Tw ...

  5. 腾讯技术分享:微信小程序音视频技术背后的故事

    1.引言 微信小程序自2017年1月9日正式对外公布以来,越来越受到关注和重视,小程序上的各种技术体验也越来越丰富.而音视频作为高速移动网络时代下增长最快的应用形式之一,在微信小程序中也当然不能错过. ...

  6. 微信小程序的wx-charts插件-tab选项卡

    微信小程序的wx-charts插件-tab选项卡 效果: //index.js var wxCharts = require('../../utils/wxcharts-min.js'); const ...

  7. Python学习笔记【第五篇】:基础函数

    一.函数:函数定义关键字def  后跟函数名称 def 函数名(参数):             ...     函数体     ...     返回值 案例: # 定义函数 def say_hei( ...

  8. LabVIEW(五):DAQ同步

    1.在许多的测试测量应用当中,我们会需要在通过一个时间段内进行同步测量. 同步采集通常分为两类: (1).同时测量:即不同的任务在同一时刻开始.举例来说,我们会在一个模拟输入通道上采集数据,同时在一个 ...

  9. python基础-循环语句(5)

    一.循环语句介绍 一般情况下,需要多次重复执行的代码,都可以用循环的方式来完成 循环不是必须要使用的,但是为了提高代码的重复使用率,所以有经验的开发者都会采用循环 二.常见的循环形式 while循环 ...

  10. 课程五(Sequence Models),第二 周(Natural Language Processing & Word Embeddings) —— 2.Programming assignments:Emojify

    Emojify! Welcome to the second assignment of Week 2. You are going to use word vector representation ...