zoukankan      html  css  js  c++  java
  • P1306 斐波那契公约数

    P1306 斐波那契公约数

    P1306 斐波那契公约数

    给定 (n,m)(Gcd(Fib_n,Fib_m)) 。((1leq n,mleq 10^9)

    结论(Gcd(Fib[n],Fib[m])=Fib[Gcd(n,m)])

    证明见题解..

    有了这个结论这个题就等价于求 (Fib[n]) 。((nleq 10^9)

    于是我们可以考虑矩阵快速幂。(好久没打了,都忘了。)

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    template <typename T>
    inline void read(T &x){
    	x=0;char ch=getchar();bool f=false;
    	while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();}
    	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    	x=f?-x:x;
    	return ;
    }
    template <typename T>
    inline void write(T x){
    	if(x<0) putchar('-'),x=-x;
    	if(x>9) write(x/10);
    	putchar(x%10^48);
    	return ;
    }
    const int N=3e5+5,mod=1e8;
    #define int long long
    int n,m;
    int gcd(int x,int y){return !y?x:gcd(y,x%y);}
    inline int inc(int x,int y){return x+y>=mod?x+y-mod:x+y;}
    struct Matrix{
    	int a[2][2];
    	inline Matrix operator * (const Matrix B) const{
    		Matrix c;
    		memset(c.a,0,sizeof(c.a));
    		for(int i=0;i<2;i++){
    			for(int j=0;j<2;j++){
    				for(int k=0;k<2;k++){
    					c.a[i][j]=inc(c.a[i][j],a[i][k]*B.a[k][j])%mod;
    				}
    			}
    		}
    		return c;
    	}
    }q,p;
    Matrix QuickPow(Matrix x,int y){
    	Matrix res;
    	memset(res.a,0,sizeof(res.a));
    	res.a[0][0]=res.a[1][1]=1;
    	if(!y) return res;
    	while(y){
    		if(y&1) res=res*x;
    		x=x*x;
    		y>>=1;
    	}
    	return res;
    }
    signed main(){
    	read(n),read(m);
    	int Gcd=gcd(n,m);
    	q.a[0][0]=q.a[0][1]=1;p.a[0][1]=p.a[1][1]=p.a[1][0]=1;
    	q=q*QuickPow(p,Gcd-1);
    	write(q.a[0][0]%mod);
    	return 0;
    }
    
  • 相关阅读:
    1. Hello UWP
    ASP.NET MVC SignalR(1):背景
    ASP.NET MVC SignalR
    nekohtml转换html时标签变大写的问题
    nohup启动java命令导致dubbo无法注册
    SOA架构改造简单记录
    [转]BloomFilter——大规模数据处理利器
    IOS行货自动打包
    Kruskal算法java版
    prim算法java版
  • 原文地址:https://www.cnblogs.com/Akmaey/p/14649854.html
Copyright © 2011-2022 走看看