首页 > 代码库 > hdu 4291
hdu 4291
暴力找循环节,矩阵快速幂
#include <iostream>#include <cstring>#include <cstdio>#include <cstdlib>#include <cmath>#include <string>#include <vector>#include <list>#include <map>#include <queue>#include <stack>#include <bitset>#include <algorithm>#include <numeric>#include <functional>using namespace std;#define LL __int64#define N 100005#define MOD 1000000007struct tt{ LL ma[2][2];};tt ksm(tt a,tt b,LL mod){ tt temp; int i,j,k; memset(temp.ma,0,sizeof(temp.ma)); for(i = 0 ; i < 2 ; i++) for(j = 0 ; j < 2 ; j++) for(k =0 ; k < 2 ; k++) temp.ma[i][j] = (temp.ma[i][j]+(a.ma[i][k]*b.ma[k][j])%mod)%mod; return temp;}LL solve(LL n,LL mod){ if(n == 0) return 0; tt p,q; p.ma[0][0] = 0,p.ma[0][1] = 1,p.ma[1][0] = 0,p.ma[1][1] = 1; q.ma[0][0] = 3,q.ma[0][1] = 1,q.ma[1][0] = 1,q.ma[1][1] = 0; while(n) { if(n&1) p = ksm(p,q,mod); q = ksm(q,q,mod); n/=2; } return p.ma[1][0]%mod;}int main(){ LL n,g1 = 183120,g2 = 222222224; while(~scanf("%I64d",&n)) { printf("%I64d\n",solve(solve(solve(n,g1),g2),MOD)); } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。