当前位置:网站首页>CF1634 F. Fibonacci Additions
CF1634 F. Fibonacci Additions
2022-07-05 05:31:00 【solemntee】
The question : q q q operations , Every time you give an array A A A perhaps B B B Of [ l , r ] [l,r] [l,r] Position plus a Fibonacci sequence , Ask after each operation A B AB AB Whether the arrays are the same
Consider Fibonacci's generating function
f ( x ) = 1 + 1 x + 2 x 2 + . . . + f i b i − 1 x i + . . . = ∑ i = 0 f i b i + 1 x i x f ( x ) = ∑ i = 1 f i b i x i , x 2 f ( x ) = ∑ i = 2 f i b i − 1 x i f ( x ) − x f ( x ) − x 2 f ( x ) = 1 f ( x ) = 1 1 − x − x 2 f(x)=1+1x+2x^2+...+fib_{i-1}x^i+...=\sum_{i=0}fib_{i+1}x^i\\ xf(x)=\sum_{i=1}fib_{i}x^i,x^2f(x)=\sum_{i=2}fib_{i-1}x^i\\ f(x)-xf(x)-x^2f(x)=1\\ f(x)=\frac 1 {1-x-x^2} f(x)=1+1x+2x2+...+fibi−1xi+...=i=0∑fibi+1xixf(x)=i=1∑fibixi,x2f(x)=i=2∑fibi−1xif(x)−xf(x)−x2f(x)=1f(x)=1−x−x21
If you put A , B A,B A,B As two polynomials
A ( x ) = ∑ i = 1 n a i x i B ( x ) = ∑ i = 1 n b i x i A(x)=\sum_{i=1}^{n}a_ix^i\\ B(x)=\sum_{i=1}^{n}b_ix^i A(x)=i=1∑naixiB(x)=i=1∑nbixi
So in A A A Array [ l , r ] [l,r] [l,r] Position plus a Fibonacci sequence is equivalent to
A ( x ) + = x l f ( x ) − f i b r − l + 2 x r f ( x ) − f i b r − l + 1 x r + 1 f ( x ) * A ( x ) + = x l 1 1 − x − x 2 − f i b r − l + 2 x r 1 1 − x − x 2 − f i b r − l + 1 x r + 1 1 1 − x − x 2 A(x)+=x^lf(x)-fib_{r-l+2}x^rf(x)-fib_{r-l+1}x^{r+1}f(x)\\ \iff A(x)+=x^l\frac 1 {1-x-x^2}-fib_{r-l+2}x^r\frac 1 {1-x-x^2}-fib_{r-l+1}x^{r+1}\frac 1 {1-x-x^2} A(x)+=xlf(x)−fibr−l+2xrf(x)−fibr−l+1xr+1f(x)*A(x)+=xl1−x−x21−fibr−l+2xr1−x−x21−fibr−l+1xr+11−x−x21
Ride on both sides ( 1 − x − x 2 ) (1-x-x^2) (1−x−x2)
* ( 1 − x − x 2 ) A ( x ) + = x l − f i b r − l + 2 x r − f i b r − l + 1 x r + 1 \iff (1-x-x^2)A(x)+=x^l-fib_{r-l+2}x^r-fib_{r-l+1}x^{r+1} *(1−x−x2)A(x)+=xl−fibr−l+2xr−fibr−l+1xr+1
The complexity of each modification is reduced to O ( 1 ) O(1) O(1)
Then consider ( 1 − x − x 2 ) A ( x ) (1-x-x^2)A(x) (1−x−x2)A(x) The practical significance of , That is, construct an auxiliary array D D D
D i = A i − A i − 1 − A i − 2 D_i=A_i-A_{i-1}-A_{i-2} Di=Ai−Ai−1−Ai−2
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int n,q,mod;
scanf("%d%d%d",&n,&q,&mod);
vector<int>a(n+1),b(n+1);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
for(int i=n;i>=2;i--)
{
a[i]-=a[i-1]+a[i-2];
a[i]=(a[i]%mod+mod)%mod;
}
for(int i=n;i>=2;i--)
{
b[i]-=b[i-1]+b[i-2];
b[i]=(b[i]%mod+mod)%mod;
}
int cnt=0;
for(int i=1;i<=n;i++)cnt+=(a[i]!=b[i]);
vector<int>fib(n+5);
fib[1]=1%mod;
fib[2]=1%mod;
for(int i=3;i<=n;i++)fib[i]=(fib[i-1]+fib[i-2])%mod;
// printf("cnt=%d\n",cnt);
// for(int j=1;j<=n;j++)printf("%d%c",a[j]," \n"[j==n]);
// for(int j=1;j<=n;j++)printf("%d%c",b[j]," \n"[j==n]);
for(int i=1;i<=q;i++)
{
char c[2];
scanf("%s",c);
int l,r;
scanf("%d%d",&l,&r);
auto add=[&](int id,int pos,int add)
{
// printf("id=%d pos=%d add=%d\n",id,pos,add);
if(pos>n)return;
if(id==1)
{
cnt-=(a[pos]!=b[pos]);
a[pos]+=add;
a[pos]=(a[pos]%mod+mod)%mod;
cnt+=(a[pos]!=b[pos]);
}
else
{
cnt-=(a[pos]!=b[pos]);
b[pos]+=add;
b[pos]=(b[pos]%mod+mod)%mod;
cnt+=(a[pos]!=b[pos]);
}
};
if(c[0]=='A')
{
add(1,l,1);
add(1,r+1,-fib[r-l+2]);
add(1,r+2,-fib[r-l+1]);
}
else
{
add(2,l,1);
add(2,r+1,-fib[r-l+2]);
add(2,r+2,-fib[r-l+1]);
}
// printf("q=%d cnt=%d\n",i,cnt);
// for(int j=1;j<=n;j++)printf("%d%c",a[j]," \n"[j==n]);
// for(int j=1;j<=n;j++)printf("%d%c",b[j]," \n"[j==n]);
if(cnt==0)printf("YES\n");
else printf("NO\n");
}
return 0;
}
边栏推荐
- 软件测试 -- 0 序
- Little known skills of Task Manager
- 读者写者模型
- 挂起等待锁 vs 自旋锁(两者的使用场合)
- 数仓项目的集群脚本
- National teacher qualification examination in the first half of 2022
- Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
- Solution to the palindrome string (Luogu p5041 haoi2009)
- Drawing dynamic 3D circle with pure C language
- Haut OJ 2021 freshmen week II reflection summary
猜你喜欢
Sword finger offer 53 - I. find the number I in the sorted array
剑指 Offer 58 - II. 左旋转字符串
剑指 Offer 05. 替换空格
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Sword finger offer 05 Replace spaces
对象的序列化
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
Web APIs DOM node
浅谈JVM(面试常考)
Sword finger offer 53 - ii Missing numbers from 0 to n-1
随机推荐
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
剑指 Offer 58 - II. 左旋转字符串
Download xftp7 and xshell7 (official website)
Acwing 4301. Truncated sequence
Pointnet++学习
SDEI初探-透过事务看本质
A misunderstanding about the console window
Haut OJ 1321: mode problem of choice sister
剑指 Offer 53 - II. 0~n-1中缺失的数字
Find a good teaching video for Solon framework test (Solon, lightweight application development framework)
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
二十六、文件系统API(设备在应用间的共享;目录和文件API)
挂起等待锁 vs 自旋锁(两者的使用场合)
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
远程升级怕截胡?详解FOTA安全升级
Pointnet++ learning
SSH password free login settings and use scripts to SSH login and execute instructions
Web APIs DOM node
YOLOv5添加注意力機制
Haut OJ 1241: League activities of class XXX