H - Hawawshi Decryption

对于一个给定的生成数列

R[ 0 ] 已知, (R[ i - 1 ] * a + b) % p = R[ i ] (p 是 质数), 求最小的 x 使得 R[ x ] = t

我们假设存在这样一个数列 S[ i ] = R[ i ] - v, 并且S[ i - 1] * a = S[ i ], 那么将S[ i ] = R[ i ] - v带入可得

v = b / (1-a) 那么我们能得到 R[ i ] = (R[ 0 ] - v) * a ^ n + v, 然后就是解一个高次剩余方程,

注意 a == 1 和 R[ 0 ] == v的情况需要特殊考虑。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = 1e4 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, x, L, R, a, b, p, T, y; struct hashTable {
int head[N+], tot;
struct node {
int val, id, nx;
} a[N+];
void init() {
memset(head, -, sizeof(head));
tot = ;
}
void Insert(int val, int id) {
int p = val % N;
a[tot].val = val;
a[tot].id = id;
a[tot].nx = head[p];
head[p] = tot++;
}
int Find(int val) {
int p = val % N;
for(int i = head[p]; ~i; i = a[i].nx)
if(a[i].val == val) return a[i].id;
return -;
}
} mp; int fastPow(int a, int b) {
int ans = ;
while(b) {
if(b & ) ans = 1ll*ans*a%p;
a = 1ll*a*a%p; b >>= ;
}
return ans;
} int BSGS(int a,int b,int p) {
if(b == ) return ;
if(a == b) return ;
if(!b) return !a ? : -;
mp.init();
int m = ceil(sqrt(p)), x = , y, z;
for(int i = ; i <= m; i++) {
x = 1ll * x * a % p;
if(mp.Find(x) == -) mp.Insert(x, i);
}
x = , y = fastPow(a, p-m-);
for(int i = ; i < m; ++i) {
z = mp.Find(1ll*x*b%p);
if(~z) return i * m + z;
x = 1ll * x * y % p;
}
return -;
} int main() {
// freopen("hawawshi.in", "r", stdin);
scanf("%d", &T);
while(T--) {
scanf("%d%d%d%d%d%d%d", &n, &x, &L, &R, &a, &b, &p);
int q = , r = R-L+;
if(a == ) {
for(int R0 = L; R0 <= R; R0++) {
int pos = 1ll*(x-R0+p)%p*fastPow(b, p-)%p;
if(pos < n) q++;
}
} else {
int v = 1ll * b * fastPow(-a+p, p-) % p;
for(int R0 = L; R0 <= R; R0++) {
if(R0 == v) {
if(R0 == x) q++;
} else {
int pos = BSGS(a, 1ll*(x-v+p)%p*fastPow((R0-v+p)%p, p-)%p, p);
if(~pos && pos < n) q++;
}
}
}
int gcd = __gcd(q, r);
printf("%d/%d\n", q/gcd, r/gcd);
}
return ;
} /*
*/

2018 Arab Collegiate Programming Contest (ACPC 2018) H - Hawawshi Decryption 数学 + BSGS的更多相关文章

  1. 2018 Arab Collegiate Programming Contest (ACPC 2018) E - Exciting Menus AC自动机

    E - Exciting Menus 建个AC自动机求个fail指针就好啦. #include<bits/stdc++.h> #define LL long long #define fi ...

  2. 2018 Arab Collegiate Programming Contest (ACPC 2018) G. Greatest Chicken Dish (线段树+GCD)

    题目链接:https://codeforces.com/gym/101991/problem/G 题意:给出 n 个数,q 次询问区间[ li,ri ]之间有多少个 GCD = di 的连续子区间. ...

  3. 2018 German Collegiate Programming Contest (GCPC 18)

    2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...

  4. (寒假GYM开黑)2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)

    layout: post title: 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) author: &qu ...

  5. (寒假GYM开黑)2018 German Collegiate Programming Contest (GCPC 18)

    layout: post title: 2018 German Collegiate Programming Contest (GCPC 18) author: "luowentaoaa&q ...

  6. 2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定理

    2018 China Collegiate Programming Contest Final (CCPC-Final 2018)-K - Mr. Panda and Kakin-中国剩余定理+同余定 ...

  7. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp

    2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)-E. Explosion Exploit-概率+状压dp [P ...

  8. 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举

    2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018)- D. Delivery Delays -二分+最短路+枚举 ...

  9. The Ninth Hunan Collegiate Programming Contest (2013) Problem H

    Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...

随机推荐

  1. MT【194】又见和式变换

    (2007浙江省赛B卷最后一题)设$\sum\limits_{i=1}^{n}{x_i}=1,x_i>0,$求证:$n\sum\limits_{i=1}^n{x_i^2}-\sum\limits ...

  2. 【题解】 bzoj4033: [HAOI2015]树上染色* (动态规划)

    bzoj4033,懒得复制,戳我戳我 Solution: 定义状态\(dp[i][j]\)表示\(i\)号节点为根节点的子树里面有\(j\)个黑色节点时最大的贡献值 然后我们要知道的就是子节点到根节点 ...

  3. 基于Maven构建的Spring+Mybatis项目

    项目的目录结构: 1.基于Maven构建Web项目 参考:基于Maven构建Web项目 2.导入项目依赖 Spring 核心容器(Beans.Core.Context.Context support. ...

  4. ASP.NET MVC验证框架中关于属性标记的通用扩展方法

    http://www.cnblogs.com/wlb/archive/2009/12/01/1614209.html 之前写过一篇文章<ASP.NET MVC中的验证>,唯一的遗憾就是在使 ...

  5. CodeForces 获得数据

    针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. ;i<=q;i++) { scanf("%ld%ld% ...

  6. np.clip截取函数

    np.clip截取函数 觉得有用的话,欢迎一起讨论相互学习~Follow Me 将范围外的数强制转化为范围内的数 def clip(a, a_min, a_max, out=None): 将数组a中的 ...

  7. CF876 F 思维 枚举

    给你n个数,问有几个区间满足,区间内或操作大于区间内的任意数. 首先可以知道,两数或操作的结果必定不会小于两者间的最大值,也就是说对于一个区间中,不合法的状态只有两值或相等.那么我们可以考虑枚举每个数 ...

  8. Python学习笔记5-时间模块time/datetime

    import time time.sleep(2) #等待几秒 # 1.格式化好的时间 2018-1-14 16:42 # 2.时间戳 是从unix元年到现在所有的秒数 # 3.时间元组 #想时间戳和 ...

  9. htm、html、shtml网页区别

    htm.html.shtml网页区别 html或者htm是一种静态的页面格式,也就是说不需要服务器解析其中的脚本,或者说里面没有服务器端执行的脚本,而shtml或者shtm由于它基于SSI技术,当有服 ...

  10. php-url地址加密

    下面是可以将我们的url地址加密.确保我们提交的数据安全 以下是代码片段: <?php function keyED($txt,$encrypt_key) //定义一个keyED { $encr ...