首页 > 其他分享 >Jzzhu and Sequences CF450B

Jzzhu and Sequences CF450B

时间:2023-02-26 15:45:43浏览次数:47  
标签:matrix int Jzzhu long Sequences CF450B include


其中 f(1)=x0,f(2)=y0, f( i )=f( i-1 )+f( i+1 ),求 f(n)

 

#include <iostream>
 #include <cmath>
 #include <algorithm>
 using namespace std; 
 #define N 2
  int mod=1e9+7;
 
 #define int long long
  struct matrix {
  	int a[N+2][N+2];
  };
  int n;
  matrix m1; 
  
  void init_e(matrix &x){
  	
  	int i,j;
  	for(i=1;i<=N;i++)
  	 for(j=1;j<=N;j++) {
  	 	 x.a[i][j]=0;
  	 	 if(i==j) x.a[i][j]=1;
  	 }
  }
  matrix mul(matrix &x,matrix &y){
 	int i,j,k;
 	matrix z;
 	
 	for(i=1;i<=N;i++)
 	 for(j=1;j<=N;j++){
 	 	z.a[i][j]=0;
 	 	for(k=1;k<=N;k++)
 	 	 z.a[i][j]+=x.a[i][k]*y.a[k][j], z.a[i][j]%=mod;
 	 }
 	
 	return z;
 }
   matrix ksm(matrix &x,int k){
 	matrix tmp=x, ans;
 	init_e(ans);
 	
 	while(k){
 		if(k&1) ans=mul(ans,tmp);
 		
 		tmp=mul(tmp,tmp); 
 		k/=2;
 	}
 	return ans;
 }
 
  signed main(){
 	std::ios::sync_with_stdio(0);
 	int x0,y0; 
 	cin>>x0>>y0>>n;
 	if(n==1){ cout<<(x0%mod+mod)%mod<<endl; return 0; }
 	
	 	m1.a[1][1]=1,m1.a[1][2]= -1;
	 	m1.a[2][1]=1,m1.a[2][2]= 0;
	 	
	 	matrix ans =ksm(m1,n-2);
	 	cout<< ((ans.a[1][1]*y0+ans.a[1][2]*x0)%mod+mod)%mod<<endl;
	 	cout<<endl;
  }	
 
 

 

标签:matrix,int,Jzzhu,long,Sequences,CF450B,include
From: https://www.cnblogs.com/towboa/p/17156802.html

相关文章

  • [LeetCode] 1332. Remove Palindromic Subsequences 删除回文子序列
    Youaregivenastring s consisting only ofletters 'a' and 'b'.Inasinglestepyoucanremoveone palindromicsubsequence from s.Return the mi......
  • Codeforces Round #661 (Div. 3) D. Binary String To Subsequences(贪心/思维)
    https://codeforces.com/contest/1399/problem/D题目大意:长度为n的字符串s只由0和1组成。我们要把s分成最小数量的子序列,使得每一个子序列看起来像“010101...”或......
  • CodeForces 1349F1 Slime and Sequences (Easy Version)
    洛谷传送门CF传送门发现样例中所有数的和为\(n!n\),于是猜想好的序列总数为\(n!\)。考虑将每一个排列\(p\)唯一对应一个好的序列\(a\)。可以这么构造:在\(p\)中顺......
  • CF1349F2 Slime and Sequences (Hard Version)
    题目描述定义一个正整数序列\(\texttt{p}\),称其是合法的当且仅当对于所有在\(\texttt{p}\)中出现过且\(>1\)的正整数\(k\),存在\(i<j\),满足\(p_i=k-1,p_j=k\)。定义\(f(k......
  • CF1349F1 Slime and Sequences (Easy Version)
    题目描述定义一个正整数序列\(\texttt{p}\),称其是合法的当且仅当对于所有在\(\texttt{p}\)中出现过且\(>1\)的正整数\(k\),存在\(i<j\),满足\(p_i=k-1,p_j=k\)。定义\(f(k......
  • [ABC248Ex] Beautiful Subsequences 题解
    [ABC248Ex]BeautifulSubsequencesSolution目录[ABC248Ex]BeautifulSubsequencesSolution更好的阅读体验戳此进入题面SolutionCodeCodeUPD更好的阅读体验戳此进入......
  • [数学记录][sosdp]CF449D Jzzhu and Numbers
    前几天做arc时连做两道高维前缀和,今天去看dp题单时发现这东西居然叫sosdp,来刷一下板子。粘一篇找到的blog,感觉引入那里非常自然!linktoCF|linktoLuogu给......
  • 题解 LGP7888【「MCOI-06」Distinct Subsequences】
    problem给定一个由小写字符构成的字符串\(S\)。令一个字符串的价值为该串的本质不同非空子序列个数,其中子序列可以为整体。求\(S\)所有子序列的价值和。答案对\(10^......
  • dp好题CF1183H Subsequences (hard version)
    CF1183HSubsequences(hardversion)考虑dp计算本质不同方案数dp[i][j]表示在前i个字符中,长度为j的本质不同的子串数跑pre[i]表示de字母出现的上一个位置pre数组我属......
  • [数学记录]abc276G Count Sequences
    题意:对满足以下条件的序列计数,膜\(998244353\):\(0\leqa_0\leqa_1\cdots\leqa_n\leqm\)$a_i\not\equiva_j\pmod3$这题的难点在于发现它是简单题。想了......