UVA 11916

  BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了。

代码如下:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <map> using namespace std; template<class T> T gcd(T a, T b) { return b ? a : gcd(b, a % b);}
typedef long long LL;
void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (b) { gcd(b, a % b, d, y, x); y -= a / b * x;}
else d = a, x = , y = ;
}
const LL MOD = ;
const int N = ; LL multi(LL a, LL p) {
LL ret = ;
a %= MOD;
while (p > ) {
if (p & ) ret *= a, ret %= MOD;
a *= a, a %= MOD, p >>= ;
}
return ret;
}
map<LL, int> id;
vector<LL> rec[N];
LL mincol; void bs(LL b, LL x, LL rt) {
id.clear();
for (int i = ; i <= rt; i++) {
if (id.find(b) != id.end()) break;
id[b] = i;
b *= x, b %= MOD;
}
} LL gs(LL b, LL x, LL r) {
LL cur = b;
//for (int i = 0; i < 50; i++) {
//if (cur == r) return mincol + i + 1;
//cur *= x, cur %= MOD;
//}
int rt = (int) ceil(sqrt((double) MOD)) + ;
bs(b, x, rt);
LL st = multi(x, rt), p, q, d;
cur = ;
for (int i = ; i <= rt; i++) {
gcd(cur, MOD, d, p, q);
p %= MOD, p += MOD, p %= MOD, p *= r / d, p %= MOD;
if (id.find(p) != id.end()) return (LL) i * rt + id[p] + mincol + ;
cur *= st, cur %= MOD;
}
return -;
} LL bf(LL b, LL x, LL r) {
LL cur = b;
for (int i = ; i < MOD; i++) {
if (cur == r) return mincol + i + ;
cur *= x, cur %= MOD;
}
return -;
}
int main() {
//freopen("in", "r", stdin);
LL n, k, b, r, x, y, bres;
int T, cas = ;
cin >> T;
while (T-- && cin >> n >> k >> b >> r) {
for (int i = ; i < N; i++) rec[i].clear();
id.clear();
mincol = ;
for (int i = ; i < b; i++) {
cin >> x >> y;
if (id.find(y) == id.end()) id[y] = id.size() - ;
rec[id[y]].push_back(x);
mincol = max(mincol, x);
}
bres = ;
for (int i = , sz = id.size(); i < sz; i++) {
rec[i].push_back();
rec[i].push_back(mincol + );
sort(rec[i].begin(), rec[i].end());
for (int j = , szj = rec[i].size(); j < szj; j++) {
if (rec[i][j] - rec[i][j - ] - >= ) bres *= k * multi(k - , (LL) rec[i][j] - rec[i][j - ] - ) % MOD, bres %= MOD;
}
}
LL tmp = k * multi(k - , mincol - );
bres *= multi(tmp, n - id.size());
bres %= MOD;
cout << "Case " << cas++ << ": ";
if (bres == r) { cout << mincol << endl; continue; }
int cnt = ;
for (int i = , sz = id.size(); i < sz; i++) {
rec[i].pop_back();
if (rec[i][rec[i].size() - ] == mincol) bres *= k, bres %= MOD, cnt++;
}
bres *= multi(k - , n - cnt);
bres %= MOD;
k = multi(k - , n);
cout << gs(bres, k, r) << endl;
//cout << bf(bres, k, r) << endl;
}
return ;
}

——written by Lyon

最新文章

  1. Winform快速开发组件的实现(一)
  2. 【NOIP2015】提高day2解题报告
  3. 标准模板库(STL)学习探究之vector容器
  4. 小课堂Week8 例外处理设计的逆袭Part1
  5. 斐波那契fib
  6. VM虚拟机安装苹果雪豹操作系统
  7. 开放Nginx在文件夹列表功能
  8. AngularJS html5Mode 使用 SVG Marker失效
  9. SharePoint 2010 加入项目到用户/欢迎菜单
  10. iOS 开发 旧版 framework
  11. sudo 取消密码
  12. stylus入门教程,在webstorm中配置stylus
  13. 常见Web应用程序漏洞
  14. Ubuntu16.04安装mac主题之图标居中(百度经验)
  15. PHP文件上传,下载,Sql工具类!
  16. 学习python 第一章
  17. jmeter 的java请求代码在main方法里面执行
  18. from __future__ import division
  19. 51. N-Queens (Array; Back-Track, Bit)
  20. nginx 实现mysql的负载均衡【转】

热门文章

  1. [jnhs]使用netbeans生成的webapp发布到tomcat是需要改名字的,不然就是404Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
  2. webserver的性能问题,一语道破真谛
  3. loadrunner分析之-网页、网络、资源分析
  4. AnalyticDB for MySQL 3.0 技术架构解析
  5. 【JZOJ4922】【NOIP2017提高组模拟12.17】环
  6. 遗传算法MATLAB实现(2):一元函数优化举例
  7. MSSQL → 04:表的创建与维护
  8. 【风马一族_php】常用的语句
  9. @spoj - ADAMOLD@ Ada and Mold
  10. Codeforces 425A