看了Po神的题解一下子就懂了A了!

不过Po神的代码出锅了…solve中"d-temp"并没有什么用QwQQwQQwQ…应该把模数除以p^temp次方才行.

来自BZOJ讨论板的hack数据

hack data

1 5 3125 7812

正确输出应该是625, 但是很多人输出3125…

CODE

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 1e15;
inline LL qpow(LL a, LL b, LL c) {
LL re = 1;
while(b) {
if(b&1) re = re * a % c;
a = a * a % c; b >>= 1;
}
return re;
}
LL gcd(LL a, LL b) { return b ? gcd(b, a%b) : a; }
void exgcd(LL a, LL b, LL &x, LL &y) {
if(!b) { x = 1, y = 0; return; }
exgcd(b, a%b, y, x); y -= x*(a/b);
}
int prime[100], cnt;
inline void Factor(int N) {
cnt = 0;
for(int i = 2; i*i <= N; ++i)
if(N % i == 0) {
prime[++cnt] = i;
while(N % i == 0) N /= i;
}
if(N > 1) prime[++cnt] = N;
}
inline int Get_g(int p, int phi) { //找原根
Factor(phi);
for(int g = 2; ; ++g) {
bool flg = true;
for(int i = 1; i <= cnt; ++i)
if(qpow(g, phi/prime[i], p) == 1)
{ flg = 0; break; }
if(flg) return g;
}
}
map<int, int>myhash;
inline LL Baby_Step_Giant_Step(LL a, LL b, LL p) {
myhash.clear(); int m = int(sqrt(p) + 1);
LL base = b;
for(int i = 0; i < m; ++i) {
myhash[base] = i;
base = base * a % p;
}
base = qpow(a, m, p); LL tmp = 1;
for(int i = 1; i <= m+1; ++i) {
tmp = tmp * base % p;
if(myhash.count(tmp))
return i*m - myhash[tmp];
}
return -1;
}
inline LL solve(LL a, LL b, LL p, LL d, LL p_d) {
b %= p_d;
if(!b) return qpow(p, d-((d-1)/a+1), INF);
LL temp = 0;
while(b % p == 0) b /= p, ++temp, p_d /= p;
if(temp % a) return 0;
LL phi = p_d - p_d/p, g = Get_g(p_d, phi);
LL ind = Baby_Step_Giant_Step(g, b, p_d);
LL re = gcd(a, phi);
if(ind % re) return 0;
return re * qpow(p, temp-temp/a, INF);
}
int main() {
int T, a, b, k;
for(scanf("%d", &T); T; T--) {
scanf("%d%d%d", &a, &b, &k); k = k<<1|1;
LL ans = 1;
for(int i = 2; i*i <= k && ans; ++i)
if(k % i == 0) {
int d = 0, p_d = 1;
while(k % i == 0) k /= i, ++d, p_d *= i;
ans *= solve(a, b, i, d, p_d);
}
if(k > 1 && ans) ans *= solve(a, b, k, 1, k);
printf("%lld\n", ans);
}
}

最新文章

  1. Spring学习记录(三)---bean自动装配autowire
  2. Spark基本工作流程及YARN cluster模式原理(读书笔记)
  3. Linux串口中的超时设置
  4. SAP 传感器辅助定位
  5. 网络流 poj 3308 最小割
  6. C# 条形码 生成函数 (Code 128 标准
  7. 【COCOS2D-HTML5 开发之三】演示样例项目附源代码及执行的GIF效果图
  8. Android实现图片宽度100%ImageView宽度且高度按比例自动伸缩
  9. 猪圈密码python脚本实现
  10. java接口多实现和多继承
  11. django系列8:优化vote页面,使用通用视图降低代码冗余
  12. vue组件,通过props父组件给子组件传值,WTF, 子组件报错undefined???
  13. ELK+Filebeat 集中式日志解决方案详解
  14. Jenkins删除或替换All view
  15. Ethereum Dapp Tutorial — Part 1
  16. Python笔记(三):构建发布模块
  17. ELK安装过程
  18. CCCC L2-005. 集合相似度
  19. DataGridView使用技巧十一:DataGridView用户输入时,单元格输入值的设定
  20. 为什么要同时重写equals和hashcode

热门文章

  1. (模板)hdoj5977 Garden of Eden(点分治)
  2. input框改变默认样式
  3. Centos7 + nginx 托管 Django 项目
  4. navicat 使用 pymysql模块
  5. iView组件Tabs嵌套使用
  6. UDP通信简单 小结
  7. js修改当前页面地址栏参数
  8. BZOJ4241历史研究题解--回滚莫队
  9. python中获取当前位置所在的行号和函数名(转)
  10. python 中英文时间转换