#include<cstdio>
#include<iostream>
#include<map>
#include<cmath>
#define ll long long
using namespace std;
ll T,k,y,z,p;
map<int,int> mp;
void exgcd(int a1,int a2,ll &x,ll &y)
{
if(!a2)
{
x=;
y=;
return;
}
exgcd(a2,a1%a2,x,y);
int t=x;
x=y;
y=t-a1/a2*y;
}
ll gcd(ll a1,ll a2)
{
for(;a2;)
{
ll a3=a1%a2;
a1=a2;
a2=a3;
}
return a1;
}
ll kuai(ll y,ll z)
{
ll ans=;
for(;z;)
{
if(z%)
ans=(ans*y)%p;
y=(y*y)%p;
z=z/;
}
return ans;
}
bool pan(ll y,ll z,ll p)
{
mp.clear();
ll m=ceil(sqrt(p)),t=;
mp[]=m+;
for(ll i=;i<m;i++)
{
t=t*y%p;
mp[t]=i;
}
ll T=kuai(y,p-m-),ine=;
for(int k=;k<=m;k++)
{
int i=mp[z*ine%p];
if(i)
{
if(i==m+)
i=;
printf("%lld\n",k*m+i);
return ;
}
ine=ine*T%p;
}
return ;
}
int main()
{
scanf("%lld%lld",&T,&k);
for(int i=;i<=T;i++)
{
scanf("%lld%lld%lld",&y,&z,&p);
if(k==)
printf("%lld\n",kuai(y,z));
if(k==)
{
ll x1,y1;
ll t=gcd(y,p);
if(z%t)
{
printf("Orz, I cannot find x!\n");
continue;
}
y/=t;
p/=t;
z/=t;
exgcd(y,p,x1,y1);
x1=(x1*z)%p;
for(;x1<;)
x1+=p;
printf("%lld\n",x1);
}
if(k==)
{
y%=p;
z%=p;
if(!y&&!z)
{
printf("1\n");
continue;
}
if(!y)
{
printf("Orz, I cannot find x!\n");
continue;
}
if(!pan(y,z,p))
printf("Orz, I cannot find x!\n");
}
}
return ;
}

第一种情况 快速幂  第二种情况 exgcd 第三种情况 大步小步法 把x变成k*m+i,把所有的0-m次方用map存下来,然后依次累加k,直到找到一个可行的。

bzoj 2242: [SDOI2011]计算器的更多相关文章

  1. bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德

    2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...

  2. BZOJ 2242: [SDOI2011]计算器( 快速幂 + 扩展欧几里德 + BSGS )

    没什么好说的... --------------------------------------------------------------------- #include<cstdio&g ...

  3. BZOJ 2242: [SDOI2011]计算器 [快速幂 BSGS]

    2242: [SDOI2011]计算器 题意:求\(a^b \mod p,\ ax \equiv b \mod p,\ a^x \equiv b \mod p\),p是质数 这种裸题我竟然WA了好多次 ...

  4. bzoj 2242 [SDOI2011]计算器(数论知识)

    Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...

  5. BZOJ.2242.[SDOI2011]计算器(扩展欧几里得 BSGS)

    同余方程都不会写了..还一直爆int /* 2.关于同余方程ax ≡b(mod p),可以用Exgcd做,但注意到p为质数,y一定有逆元 首先a%p=0时 仅当b=0时有解:然后有x ≡b*a^-1( ...

  6. BZOJ 2242 [SDOI2011]计算器(快速幂+Exgcd+BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2242 [题目大意] 给出T和K 对于K=1,计算 Y^Z Mod P 的值 对于K=2 ...

  7. bzoj 2242 [SDOI2011]计算器——BSGS模板

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...

  8. BZOJ 2242 [SDOI2011]计算器 BSGS+高速幂+EXGCD

    题意:id=2242">链接 方法: BSGS+高速幂+EXGCD 解析: BSGS- 题解同上.. 代码: #include <cmath> #include <c ...

  9. [原博客] BZOJ 2242 [SDOI2011] 计算器

    题目链接 noip级数论模版题了吧.让求三个东西: 给定y,z,p,计算`Y^Z Mod P` 的值. 给定y,z,p,计算满足`xy≡ Z ( mod P )`的最小非负整数. 给定y,z,p,计算 ...

随机推荐

  1. 百度之星复赛Astar Round3

    拍照 树状数组(SB了).求出静止状态下,每个点能看到多少个向右开的船c1[i],多少个向左开的船c2[i]. max{c1[i] + c2[j], (满足i <= j)  }即为答案.从后往前 ...

  2. c#获取url请求的返回值(转)

    有两种方式获取. 方法一: /// <summary> /// 获取url的返回值 /// </summary> /// <param name="url&qu ...

  3. JavaWeb学习总结(十四)--Apache的DBUtils

    一.commons-dbutils简介 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化 ...

  4. C++中的虚函数与纯虚函数

    这个吧,我也不怎么知道,所以,大家来看这两篇文章哦: http://blog.csdn.net/hackbuteer1/article/details/7558868 http://blog.csdn ...

  5. Jump Game II

    Description: Given an array of non-negative integers, you are initially positioned at the first inde ...

  6. spring配置详解

    1.前言 公司老项目的后台,均是基于spring框架搭建,其中还用到了log4j.jar等开源架包.在新项目中,则是spring和hibernate框架均有使用,利用了hibernate框架,来实现持 ...

  7. android下调用C,JNI调用

    1.eclipse中声明调用的C函数  com.example.test.MainActivity public native long RucMyfunction(int w,int h,int[] ...

  8. iOS开发 返回字符串的宽高

    - (CGFloat)achiveWidthWithHeight:(CGFloat)height Font:(UIFont *)font { CGSize size = [self boundingR ...

  9. Hbase之测试数据

    info ship user name age height phone addr email dept salary create 'user','info','ship'; put 'user', ...

  10. spring3-hibernate3整合

    Spring与Hibernate整合关键点: 1) Hibernate的SessionFactory对象交给Spring创建: 2) hibernate事务交给spring的声明式事务管理. SH整合 ...