bzoj 2242 [SDOI2011]计算器——BSGS模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242
第一道BSGS!
咳咳,我到底改了些什么?……
感觉和自己的第一版写的差不多……可能是long long还有%C的位置的缘故?
不过挺欣赏这个板子的。把它记下来好了。
其讲解:https://blog.csdn.net/clove_unique/article/details/50740412
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<cmath>
#define ll long long
using namespace std;
ll T,type,A,B,C,x,y;
map<ll,ll> mp;
ll pw(ll x,ll k,ll mod)
{
ll ret=;while(k){if(k&)ret=ret*x%mod;x=x*x%mod;k>>=;}return ret;
}
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b){x=;y=;return;}
exgcd(b,a%b,y,x);y-=a/b*x;
}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll solve3()
{
A%=C;B%=C;
if(!A)
{
if(!B)return ;else return -;
}
mp.clear();ll m=ceil(sqrt(C)),t;
for(ll i=;i<=m;i++)
{
if(!i){t=B%C;mp[t]=i;continue;}
t=t*A%C;mp[t]=i;// if cover the previous one,it's correct
}
t=pw(A,m,C);ll ans=t;
for(ll i=;i<=m;i++)
{
if(mp[ans])return (i*m%C-mp[ans]%C+C)%C;//%C的位置
ans=ans*t%C;
}
return -;
}
int main()
{
scanf("%lld%lld",&T,&type);
while(T--)
{
scanf("%lld%lld%lld",&A,&B,&C);
if(type==)
{
B%=(C-);
printf("%lld\n",pw(A,B,C));
}
if(type==)
{
ll g=gcd(A,C);//ll g
if(B%g){printf("Orz, I cannot find x!\n");continue;}
A/=g;C/=g;B/=g;
exgcd(A,C,x,y);
printf("%lld\n",(x*B%C+C)%C);//多%一个C
}
if(type==)
{
ll ans=solve3();
if(ans==-)printf("Orz, I cannot find x!\n");
else printf("%lld\n",ans);
}
}
return ;
}
bzoj 2242 [SDOI2011]计算器——BSGS模板的更多相关文章
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- BZOJ 2242 [SDOI2011]计算器 BSGS+高速幂+EXGCD
题意:id=2242">链接 方法: BSGS+高速幂+EXGCD 解析: BSGS- 题解同上.. 代码: #include <cmath> #include <c ...
- BZOJ 2242 [SDOI2011]计算器 | BSGS
insert的时候忘了取模了-- #include <cstdio> #include <cmath> #include <cstring> #include &l ...
- bzoj 2242: [SDOI2011]计算器 & BSGS算法笔记
这题的主要难点在于第三问该如何解决 于是就要知道BSGS是怎样的一种方法了 首先BSGS是meet in the middle的一种(戳下面看) http://m.blog.csdn.net/blog ...
- BZOJ 2242: [SDOI2011]计算器( 快速幂 + 扩展欧几里德 + BSGS )
没什么好说的... --------------------------------------------------------------------- #include<cstdio&g ...
- BZOJ 2242: [SDOI2011]计算器 [快速幂 BSGS]
2242: [SDOI2011]计算器 题意:求\(a^b \mod p,\ ax \equiv b \mod p,\ a^x \equiv b \mod p\),p是质数 这种裸题我竟然WA了好多次 ...
- BZOJ.2242.[SDOI2011]计算器(扩展欧几里得 BSGS)
同余方程都不会写了..还一直爆int /* 2.关于同余方程ax ≡b(mod p),可以用Exgcd做,但注意到p为质数,y一定有逆元 首先a%p=0时 仅当b=0时有解:然后有x ≡b*a^-1( ...
- BZOJ 2242 [SDOI2011]计算器(快速幂+Exgcd+BSGS)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2242 [题目大意] 给出T和K 对于K=1,计算 Y^Z Mod P 的值 对于K=2 ...
- bzoj 2242 [SDOI2011]计算器 快速幂+扩展欧几里得+BSGS
1:快速幂 2:exgcd 3:exbsgs,题里说是素数,但我打的普通bsgs就wa,exbsgs就A了...... (map就是慢)..... #include<cstdio> # ...
随机推荐
- Android开发BUG及解决方法2
错误描述: 错误分析: 程序依赖的两个包冲突 解决方法: 在build.gradle文件中android节点下加packagingOptions节点
- 【leetcode刷题笔记】Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- MODBUS协议 一种问答方式的通信协议
源:MODBUS协议 一种问答方式的通信协议 ModBus通信系统协议
- awk的逻辑运算符
运算符 描述 赋值运算符 = += -= *= /= %= ^= **= 赋值语句 逻辑运算符 || 逻辑或 && 逻辑与 正则运算符 ~ ~! 匹配正则表达式和不匹配正则表达式 关系 ...
- 在unity 中,使用http请求,下载文件到可读可写路径
在这里我用了一个线程池,线程池参数接收一个带有object参数的,无返回值的委托 ,下载用到的核心代码,网上拷贝的,他的核心就是发起一个web请求,然后得到请求的响应,读取响应的流 剩下的都是常见的I ...
- Python httpServer服务器(初级)
使用原生的python开发的web服务器,入门级! #!/usr/bin/python # -*- coding: UTF-8 -*- import os #Python的标准库中的os模块包含普遍的 ...
- 自学Hadoop
一.Hadoop基础设施 起源于Google的三篇论文: 1. <The Google File System > 2003年 http://static.googleuserconten ...
- 解决Android7.1.1中无法打开/data目录的问题
声明:本技巧借鉴https://segmentfault.com/a/1190000008416511这个大神的文章 一.复制android Sdk安装目录的platform-tools路径: 二.w ...
- POJ2741 Colored Cubes
Description There are several colored cubes. All of them are of the same size but they may be colore ...
- Android国际化-图片国际化和文本字符国际化
注意: 1.是在res目录下面,新建文件夹 2.需要国际化的文本资源和图片资源名称是一样的 图片国际化 默认:drawable-xhdpi 中文简体:drawable-zh-rCN-xhdpi(或者不 ...