hdu4990 矩阵

C - Reading comprehension
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

Read the program below carefully then answer the question. 
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include <cstdio> 
#include<iostream> 
#include <cstring> 
#include <cmath> 
#include <algorithm> 
#include<vector> 

const int MAX=100000*2; 
const int INF=1e9; 

int main() 

  int n,m,ans,i; 
  while(scanf("%d%d",&n,&m)!=EOF) 
  { 
    ans=0; 
    for(i=1;i<=n;i++) 
    { 
      if(i&1)ans=(ans*2+1)%m; 
      else ans=ans*2%m; 
    } 
    printf("%d\n",ans); 
  } 
  return 0; 
}
 

Input

Multi test cases,each line will contain two integers n and m. Process to end of file. 
[Technical Specification]
1<=n, m <= 1000000000
 

Output

For each case,output an integer,represents the output of above program.
 

Sample Input

1 10 3 100
 

Sample Output

1 5

 

思路:
if(i & 1) f[i] = f[i-1] * 2 + 1;
else f[i] = f[i-1] * 2;
 
所以这里有2个矩阵,关键的是什么,矩阵不满足交换律,所以先求出矩阵a和b的乘结果,然后在求a×b的n/2次方,如果n为奇数,补乘矩阵a即可。
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = 100010;
struct Mat
{
    ll a[2][2];
};
ll MOD;
Mat operator *(Mat a,Mat b)
{
    Mat 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] += (a.a[i][k] * b.a[k][j])%MOD;
            }
        }
    }
    return c;
}
Mat power(Mat b,ll n)
{
    Mat c;
    c.a[0][0] = c.a[1][1] = 1;
    c.a[0][1] = c.a[1][0] = 0;
    while(n){
        if(n & 1){
            c = c * b;
        }
        b = b * b;
        n >>= 1;
    }
    return c;
}
ll mod_pow(ll x,ll n)
{
    ll res = 1;
    while(n){
        if(n & 1) res = res * x % MOD;
        x = x * x % MOD;
        n >>= 1;
    }
    return res;
}
ll mod_mul(ll a,ll b)
{
    ll res = 0;
    while(b){
        if(b & 1){
            res = (res + a) % MOD;
        }
        b >>= 1;
        a = (a + a) % MOD;
    }
    return res;
}
ll n;
int main()
{
    while(~scanf("%lld%lld",&n,&MOD)){
        if(n == 0){
            cout<<0<<endl;
            continue;
        }
        else if(n == 1){
            cout<<1%MOD<<endl;
            continue;
        }
        Mat a,b;
        a.a[0][0] = 4;
        a.a[0][1] = 2;
        a.a[1][0] = 0;
        a.a[1][1] = 1;
        b.a[0][0] = 2;
        b.a[0][1] = 1;
        b.a[1][1] = 1;
        b.a[1][0] = 0;
        a = power(a,n/2);
        if(n % 2 == 0){
            ll ans = a.a[0][1] % MOD;
            printf("%lld\n",ans);
        }
        else {
            b = b * a;
            ll ans = b.a[0][1] % MOD;
            printf("%lld\n",ans);
        }
    }
    return 0;
}

 

posted @ 2016-04-26 21:08  sweat123  阅读(116)  评论(0编辑  收藏  举报