有趣的数学题
http://csustacm.com:4803/problem/1106
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson l,mid,rt<<1 4 #define rson mid+1,r,rt<<1|1 5 #define IT set<ll>::iterator 6 #define pb push_back 7 #define eb emplace_back 8 #define maxn 200005 9 #define eps 1e-6 10 #define PI acos(-1.0) 11 #define rep(k,i,j) for(int k=i;k<j;k++) 12 typedef long long ll; 13 typedef pair<int,int> pii; 14 typedef pair<ll,ll>pll; 15 typedef pair<ll,int> pli; 16 typedef pair<pair<int,string>,pii> ppp; 17 typedef unsigned long long ull; 18 const long long MOD=1000000007; 19 const double oula=0.57721566490153286060651209; 20 using namespace std; 21 22 inline char nc() 23 { 24 static char buf[10000000], *p1 = buf, *p2 = buf; 25 return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 10000000, stdin), p1 == p2) ? EOF : *p1++; 26 } 27 inline int read() 28 { 29 int res = 0; 30 char ch; 31 do ch = nc(); while (ch < 48 || ch > 57); 32 do res = res * 10 + ch - 48, ch = nc(); while (ch >= 48 && ch <= 57); 33 return res; 34 } 35 36 typedef vector<ll> vec; 37 typedef vector<vec> mat; 38 39 mat mul(mat &a,mat &b,ll mod){ 40 mat c(a.size(),vec(b[0].size())); 41 for(int i=0;i<a.size();i++){ 42 for(int k=0;k<b.size();k++){ 43 if(a[i][k]){ 44 for(int j=0;j<b[0].size();j++){ 45 c[i][j]=(c[i][j]+a[i][k]*b[k][j]%mod+mod)%mod; 46 } 47 } 48 } 49 } 50 return c; 51 } 52 53 mat ksm(mat a,ll n,ll mod){ 54 mat b(a.size(),vec(a.size())); 55 b[0][0]=2; 56 b[1][0]=b[2][0]=1; 57 while(n){ 58 if(n&1){ 59 b=mul(a,b,mod); 60 } 61 a=mul(a,a,mod); 62 n>>=1; 63 } 64 return b; 65 } 66 67 int main(){ 68 std::ios::sync_with_stdio(false); 69 ll n,m,x,y; 70 cin>>n>>m>>x>>y; 71 ll gcd=__gcd(n+m,x+y); 72 if(gcd==1||gcd==2) cout<<1<<endl; 73 else{ 74 mat a(3,vec(3)); 75 a[0][0]=a[0][1]=a[1][0]=a[2][1]=1; 76 mat ans=ksm(a,gcd-3,1e9+7); 77 cout<<ans[0][0]<<endl; 78 } 79 }
posted on 2019-04-16 16:45 Fighting_sh 阅读(234) 评论(0) 编辑 收藏 举报