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. windows2008R2下iis7.5中的url重写(urlrewrite)

    以前在windows2003里,使用的是iis6.0,那时常使用的URL重写组件是iisrewrite,当服务器升级到windows2008R2时,IIS成了64位的7.5,结果iisreite组件是 ...

  2. C#图片水印类

    这个是学习用的呃,主要看一下水印在修改图片中距左边的宽度和高度是杂弄的就哦客了. using System; using System.Collections.Generic; using Syste ...

  3. 【计算机视觉】基于Shading Model(对光照变化一定不变性)的运动目标检测算法

    光照模型(Shading Model)在很多论文中得到了广泛的应用,如robust and illumination invariant change detection based on linea ...

  4. [转帖]上云测试,这些关键点你get 到没有

    上云测试,这些关键点你get 到没有 https://www.cnblogs.com/mypm/p/10852656.html?tdsourcetag=s_pcqq_aiomsg sticky 还有s ...

  5. (二)linux 学习 -- 探究操作系统

    The Linux Command Line 读书笔记 - 部分内容来自 http://billie66.github.io/TLCL/book/chap04.html 文章目录 ls 命令进阶 `l ...

  6. Pythn基础课程笔记day03_学习内容概要及作业讲解

    第三天_学习内容概要 今日内容概要 1.整形 2.布尔类型 3.字符串 内容回顾和补充 内容回顾 利用思维导图,罗列复习自己学习的内容,巩固知识点. xmind 软件 processon 网站 补充 ...

  7. PAT(B) 1068 万绿丛中一点红(C)

    题目链接:1068 万绿丛中一点红 (20 point(s)) 参考博客:1068. 万绿丛中一点红(20) i逆天耗子丶 题目描述 对于计算机而言,颜色不过是像素点对应的一个 24 位的数值.现给定 ...

  8. Java正则表达式获取中括号之间的内容

    参考: 求一个正则表达式提取中括号里的内容 [问题点数:80分]CSDN论坛 > Java > Web 开发 正则表达式 - 菜鸟教程 不包含中括号 正则表达式如下: \\[(.*?)] ...

  9. 【HC89S003F4开发板】 1环境搭建

    HC89S003F4开发板环境搭建 一.概述 芯圣电子做活动,一个开发板只用一块钱,买过来玩玩.︿( ̄︶ ̄)︿ 全套资料可以在论坛或qq群里下载.总之先安装个环境先. 二.安装Keil C51 作为增 ...

  10. Filter讲解4

    想要 浏览更多Fiddler内容:请点击进入Fiddler官方文档 阅读目录: 一.使用.NET代码扩展Fiddler 二.实现Fiddler接口 三.创建Fiddler扩展项目 四.在扩展程序选项卡 ...