2019-ACM-ICPC-南昌区网络赛-H. The Nth Item-特征根法求通项公式+二次剩余+欧拉降幂


【Problem Description】

​ 已知\(f(n)=3\cdot f(n-1)+2\cdot f(n-2),(n\ge 2)\),求\(f(n)\pmod {998244353}\)。

【Solution】

​ 利用特征根法求得通项公式为\(a_n=\frac{\sqrt{17}}{17}\cdot\Bigg(\Big(\frac{3+\sqrt{17}}{2} \Big)^n-\Big(\frac{3-\sqrt{17}}{2} \Big)^n\Bigg)\)。\(\sqrt{17}\pmod {998244353}\)可以用二次剩余求得。然后就可以用快速幂在\(O(log(n))\)的时间复杂度内求得\(a_n\)。但是因为\(T\le 10^7\),所以还需优化。

​ \(n\le 10^{18}\),进行欧拉降幂后\(n\le 10^9\),令\(k=\lfloor \sqrt{n}\rfloor\),则:

\[n=k\cdot t+r\\
x^n=x^{k\cdot t+r}\Leftrightarrow x^n=x^{k\cdot t}+x^r(t,r\le k)
\]

然后预处理出\(x^r,r\le k\)以及\(x^{k\cdot t},t\le k\)。则\(x^n\)就可以\(O(1)\)查询了。


【Code】

/*
* @Author: Simon
* @Date: 2019-09-08 13:13:29
* @Last Modified by: Simon
* @Last Modified time: 2019-09-17 17:44:39
*/
#include<bits/stdc++.h>
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 100005
const int mod=998244353;
int inv17;
/*
* Author: Simon
* 功能: 求解x^2=n(mod p),即x=sqrt(n)(mod p)
* 复杂度: O(sqrt(p))
*/ /*类似复数 单位元为w(复数的单位元为-1)*/
struct Complex {
int x, y, w;
Complex() {}
Complex(int x, int y, int w) : x(x), y(y), w(w) {}
};
/*类复数乘法 */
Complex mul(Complex a, Complex b, int p) {
Complex ans;
ans.x = (a.x * b.x % p + a.y * b.y % p * a.w % p) % p;
ans.y = (a.x * b.y % p + a.y * b.x % p) % p;
ans.w = a.w;
return ans;
}
/*类复数快速幂 */
Complex Complexfpow(Complex a, int b, int mod) {
Complex ans = Complex(1, 0, a.w);
while (b) {
if (b & 1) ans = mul(ans, a, mod);
a = mul(a, a, mod);
b >>= 1;
}
return ans;
}
int fpow(int a, int b, int mod) {
int ans = 1;
a %= mod;
while (b) {
if (b & 1) (ans *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return ans;
}
/*求解x^2=n(mod p) */
int solve(int n, int p) {
n %= p;
if (n == 0) return 0;
if (p == 2) return n;
if (fpow(n, (p - 1) / 2, p) == p - 1) return -1; /*勒让德定理判断n不是p的二次剩余 */
mt19937 rnd(time(0));
int a, t, w;
do {
a = rnd() % p;
t = a * a - n;
w = (t % p + p) % p; /*构造w=a^2-n */
} while (fpow(w, (p - 1) / 2, p) != p - 1); /*找到一个w不是p的二次剩余 */
Complex ans = Complex(a, 1, w);
ans = Complexfpow(ans, (p + 1) / 2, p); /*答案为(a+w)^{(p+1)/2} */
return ans.x;
}
pair<int,int> bit1[maxn],bit2[maxn];
Int main(){
#ifndef ONLINE_JUDGE
//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);inv17=fpow(17,mod-2,mod);
int x=solve(17,mod); //二次剩余
int lim=ceil(sqrt(1e9));
int s1=(3+x)%mod*fpow(2,mod-2,mod)%mod,
s2=(3-x)%mod*fpow(2,mod-2,mod)%mod;
bit1[0].first=bit1[0].second=bit2[0].first=bit2[0].second=1;
for(int i=1;i<=lim;i++){ //预处理
bit1[i].first=bit1[i-1].first*s1%mod;
bit1[i].second=bit1[i-1].second*s2%mod;
bit2[i].first=fpow(s1,i*lim,mod);
bit2[i].second=fpow(s2,i*lim,mod);
}
int q,n;cin>>q>>n;
int ans=0;
while(q--){
int tmp=0;
if(n==0) tmp=0;
else if(n==1) tmp=1;
else{
int t=n%(mod-1);
int t2=t/lim,t1=t%lim;
tmp=(bit1[t1].first*bit2[t2].first%mod-bit1[t1].second*bit2[t2].second%mod)%mod*x%mod*inv17%mod;
}
tmp=(tmp+mod)%mod;
n=tmp*tmp^n; ans^=tmp;
// cout<<n<<endl;
}
cout<<(ans+mod)%mod<<endl;
#ifndef ONLINE_JUDGE
cout<<endl;system("pause");
#endif
return 0;
}

2019-ACM-ICPC-南昌区网络赛-H. The Nth Item-特征根法求通项公式+二次剩余+欧拉降幂的更多相关文章

  1. 2019 南昌ICPC网络赛H The Nth Item

    The Nth Iteam 题意:F(0)=1,F(1)=1,F(n)=3*F(n-1)+2*F(n-2) (n>=2) ,F(n) mod 998244353.给出Q跟N1,Ni=Ni-1^( ...

  2. 南昌网络赛 H The Nth Item

    南昌网络赛The Nth Item 暴力快速幂+unordered_map记忆化 注意:记忆化不能写到快速幂求解函数里,不断调用函数会造成很大的时间浪费 #include<bits/stdc++ ...

  3. 2017 ACM/ICPC 南宁区 网络赛 Overlapping Rectangles

    2017-09-24 20:11:21 writer:pprp 找到的大神的代码,直接过了 采用了扫描线+线段树的算法,先码了,作为模板也不错啊 题目链接:https://nanti.jisuanke ...

  4. 2019南昌网络赛H The Nth Item(打表找询问循环节 or 分段打表)

    https://nanti.jisuanke.com/t/41355 思路 从fib循环节入手,\(O(1e7log(1e9))\),tle 因为只需要输出所有询问亦或后的结果,所以考虑答案的循环节, ...

  5. 2019南昌网络赛H The Nth Item(二阶线性数列递推 + 广义斐波那契循环节 + 分段打表)题解

    题意: 传送门 已知\(F(n)=3F(n-1)+2F(n-2) \mod 998244353,F(0)=0,F(1)=1\),给出初始的\(n_1\)和询问次数\(q\),设每一次的答案\(a_i= ...

  6. 2019 ICPC南昌邀请赛网络赛比赛过程及题解

    解题过程 中午吃饭比较晚,到机房lfw开始发各队的账号密码,byf开始读D题,shl电脑卡的要死,启动中...然后听到谁说A题过了好多,然后shl让blf读A题,A题blf一下就A了.然后lfw读完M ...

  7. 2019 ICPC南昌邀请赛 网络赛 K. MORE XOR

    说明 \(\oplus x​\)为累异或 $ x^{\oplus(a)}​$为异或幂 题意&解法 题库链接 $ f(l,r)=\oplus_{i=l}^{r} a[i]$ $ g(l,r)=\ ...

  8. 2019 ICPC南京站网络赛 H题 Holy Grail(BF算法最短路)

    计蒜客题目链接:https://nanti.jisuanke.com/t/41305 给定的起点是S,终点是T,反向跑一下就可以了,注意判负环以及每次查询需要添加边 AC代码: #include< ...

  9. icpc 南昌邀请赛网络赛 Max answer

    就是求区间和与区间最小值的积的最大值 但是a[i]可能是负的 这就很坑 赛后看了好多dalao的博客 终于a了 这个问题我感觉可以分为两个步骤 第一步是对于每个元素 以它为最小值的最大区间是什么 第二 ...

随机推荐

  1. Union All/Union/Intersect操作

    Union All/Union/Intersect操作 适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1. ...

  2. windows环境下安装: VMware 15 + centos 7

    第一步: 下载 centos7  http://isoredirect.centos.org/centos/7/isos/x86_64/ 选择阿里云镜像下载,速度最快 注意: 尽量使用下载工具下载, ...

  3. Beta冲刺(4/4)

    队名:秃头小队 组长博客 作业博客 组长徐俊杰 过去两天完成的任务:学习了很多东西 Github签入记录 接下来的计划:继续学习 还剩下哪些任务:细节处理 燃尽图 遇到的困难:自己太菜了 收获和疑问: ...

  4. eclipse的maven中需要把jar的包文件登入到自己的仓库里面的操作

    问题的描述 从别人那拿到了Java maven的工程,导入自己的eclipse中之后编译的时候出现包文件找不到,之后把工程进行maven的update project之后,pom.xml文件出现错误, ...

  5. ubuntu 安装 typora

    # or run: # sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BA300B7755AFCFAE wget -qO ...

  6. PHP中奖概率写法

    PHP中奖概率写法 <pre><?phpheader("Content-type: text/html; charset=utf-8");/* * 经典的概率算法 ...

  7. juc-locks包

    1. 简介 java.util.concurrent.locks 包含常用的锁实现,重点研究AbstractQueuedSynchronizer.ReentrantLock.ReentrantRead ...

  8. github.com连接超时

     https://blog.csdn.net/hanchao5272/article/details/79393393 1.错误信息 之前github都能用,但是今天git clone的时候居然连不上 ...

  9. 查看php和apache配置成功的方法

    PHP配置文件是php.ini  检查php是否配置成功,在wamp/www根目录写一个phpinfo.php文件,内容为 <?php phpinfo(); ?>  然后可以打开网页输入l ...

  10. IDEA/WebStorm使用笔记

    1.使用powershell作为默认终端 #改变powershell策略 Set-ExecutionPolicy Unrestricted -Scope CurrentUser 找到系统的powers ...