首页 > 其他分享 >题解 ABC270G【Sequence in mod P】

题解 ABC270G【Sequence in mod P】

时间:2022-11-12 14:55:36浏览次数:67  
标签:frac Sequence int 题解 LL SA include mod

posted on 2022-10-20 13:58:54 | under 题解 | source

有个地方写错了,改一下

problem

Soso 有一个无穷序列 \(\{X_i\}\) 定义如下:

\[X_i=\begin{cases} S,&(i=0)\\ (A\cdot X_{i-1}+B)\bmod P,&(i\geq 1) \end{cases}\]

其中 \(0\leq S,A,B<P\leq 10^9\) 均为常数,\(P\in\textrm{Prime}\)。现在求最小的 \(i\) 使得 \(X_i=G\)。多测 \(100\) 组数据。

solution

general

思路:将式子化为只与 \(k\) 有关的东西。

\[\begin{aligned} X_k&=SA^k+BA^{k-1}+BA^{k-2}+\cdots+BA^0\\ &=SA^k+B(A^{k-1}+A^{k-2}+\cdots+A^0)\\ &=SA^k+B\frac{A^k-1}{A-1}\\ &=SA^k+\frac{B}{A-1}A^k-\frac{B}{A-1}\\ &=(S+\frac{B}{A-1})A^k-\frac{B}{A-1}=G. \end{aligned}\]

令 \(C=\frac{B}{A-1}\),欲求 \(A^k\equiv \frac{G+C}{S+C}\pmod P\),因为 \(P\) 为质数,故使用 BSGS 求得最小的 \(k\)。

special judge

  • \(S=G\Rightarrow0\)。
  • \(A=0\):check if \(B=G\) or \(S=G\)。
  • \(A=1\):\(X_n=S+Bn\Rightarrow n=\frac{G-S}{B}\)。
  • \(S=B=0\):\(X_n=0\)。

code

#include <map>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
LL qpow(LL a,LL b,int p){LL r=1;for(;b;b>>=1,a=a*a%p) if(b&1) r=r*a%p;return r;}
LL inv(LL a,int p){return qpow(a,p-2,p);}
LL bsgs(LL x,LL y,int p){
//	fprintf(stderr,"bsgs(%lld,%lld,%d)\n",x,y,p);
	if(x%=p,y%=p,y==1||x==y) return y!=1;
	//x^(am-b)=(x^m)^a/x^b=y =>(x^m)^a=y*x^b
	int m=ceil(sqrt(p));
	map<int,int> mp;
	for(int i=m;i>=1;i--) mp[qpow(x,i*m,p)]=i*m;
	int res=1e9;
	for(int i=0;i<=m;i++){
		if(mp.count(y*qpow(x,i,p)%p)) res=min(res,mp[y*qpow(x,i,p)%p]-i);
	}
	return res==1e9?-1:res;
}
LL p,a,b,s,g;
int mian(){
	if(s==g) puts("0");
	else if(a==0) printf("%lld\n",b==g?1ll:-1ll);
	else if(a==1){
		if(!b) puts("-1");
		else printf("%lld\n",(g-s+p)%p*inv(b,p)%p);
	}else{
		LL cmb=b*inv(a-1,p)%p;
		printf("%lld\n",bsgs(a,(g+cmb)%p*inv(s+cmb,p)%p,p));
	}
	return 0;
}
int main(){
	for(scanf("%*d");~scanf("%lld%lld%lld%lld%lld",&p,&a,&b,&s,&g);mian());
	return 0;
}

标签:frac,Sequence,int,题解,LL,SA,include,mod
From: https://www.cnblogs.com/caijianhong/p/solution-ABC270G.html

相关文章

  • System.ComponentModel.Win32Exception: "指定的可执行文件不是该操作系统平台的有效
      加上这行代码就行了p.StartInfo.UseShellExecute=true; ......
  • 20221112 - Find Device closed unexpectedly 问题解决
    问题现象:小米手机屏幕下方每隔2秒弹出 FindDeviceclosedunexpectedly问题解决:备份数据;并恢复出厂设置(开机前,按住音量键上或下+开机键)。备注:也尝试了一些网上的说法......
  • [ABC271E] Subsequence Path
    洛谷链接原题链接题目描述某地区有\(N\)个城镇,编号为1到\(N\),并且由\(M\)条公路连接,编号1到\(M\)。每条公路都是有向的;而且编号为\(i(1\lei\leM)\)......
  • 「题解」Codeforces 1342F Make It Ascending
    只会\(\mathcal{O}(3^nn^2)\),打开题解一看怎么还真是这个玩意/jy首先集合之间形成一个sum和pos的二维偏序,那么思路就是对一维扫描线,然后另一维搞个什么东西。具体到......
  • ModuleNotFoundError: No module named 'XXX'
    先来一张表情包:   pycharm在小黑框使用pip安装某个包,在解释器没有找到某个包,所以运行程序的时候总是报错。我相信大家可能都遇到这样的问题。我下载有3.8、3.10......
  • OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src
    原先一直以为数据集路径错误,调了半天也没用,后来打印图片列表,发现一个隐藏文件在终端运行 ls-a也出现了这个隐藏文件  删除 rm-rf.ipynb_checkpoints之后成功......
  • 牛客小白月赛60 题解
    比赛通道:牛客小白月赛60前言第二次小白月赛没有AK,感觉自己可以原地退役了QAQ。这次F题理论上我能做出来,但是由于没有打表状态不佳,导致没有AK。A.小竹与妈妈思路这题......
  • 2022/11/11 集训题解
    今天是双11又是疯狂星期四,所以vivo50。比赛链接T2Description给出\(n\)个点\(m\)条边的图,问有多少种边的子集使得全图是个联通的仙人掌。答案对\(998244353\)取......
  • P3167 [CQOI2014]通配符匹配 题解
    想了两种做法,第一种拿到了10分的好成绩。而第二种做法不用前缀和,而且还跑的飞快。目前最优解第三尝试卡进最优解未果。不得不说这是一道好题,做完对KMP有了更深的理解......
  • 题解 [ABC227F] Treasure Hunting
    简单DP,当时赛时没做出来,怎么回事呢。在DP过程中并不好维护前\(k\)大都是什么,没有办法把它放到状态里,因此我们枚举第\(k\)大数的下标\(a_{x,y}\)。然后就好办了,设......