当前位置:网站首页>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;
}
边栏推荐
- YOLOv5-Shufflenetv2
- Reflection summary of Haut OJ freshmen on Wednesday
- sync. Interpretation of mutex source code
- Haut OJ 1241: League activities of class XXX
- Sword finger offer 58 - ii Rotate string left
- Animation scoring data analysis and visualization and it industry recruitment data analysis and visualization
- Download xftp7 and xshell7 (official website)
- SAP-修改系统表数据的方法
- Introduction to memory layout of FVP and Juno platforms
- Solution to the palindrome string (Luogu p5041 haoi2009)
猜你喜欢
Yolov5 adds attention mechanism
用STM32点个灯
Yolov5 ajouter un mécanisme d'attention
The present is a gift from heaven -- a film review of the journey of the soul
YOLOv5添加注意力機制
Sword finger offer 53 - ii Missing numbers from 0 to n-1
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
YOLOv5-Shufflenetv2
Sword finger offer 58 - ii Rotate string left
随机推荐
Annotation and reflection
A misunderstanding about the console window
[es practice] use the native realm security mode on es
Daily question - longest substring without repeated characters
二十六、文件系统API(设备在应用间的共享;目录和文件API)
[turn]: Apache Felix framework configuration properties
What is the agile proportion of PMP Exam? Dispel doubts
挂起等待锁 vs 自旋锁(两者的使用场合)
Fragment addition failed error lookup
剑指 Offer 05. 替换空格
Yolov5 adds attention mechanism
Daily question - Search two-dimensional matrix PS two-dimensional array search
卷积神经网络简介
Summary of Haut OJ 2021 freshman week
[turn to] MySQL operation practice (III): table connection
MySQL数据库(一)
Graduation project of game mall
Control Unit 控制部件
Sword finger offer 04 Search in two-dimensional array
SAP-修改系统表数据的方法