• 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 ",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
    ",ans);
            }
            else {
                b = b * a;
                ll ans = b.a[0][1] % MOD;
                printf("%lld
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    任意钝角三角形都可以分割为7个锐角三角形
    科场的胜者
    VBA按区导出电脑字库区位码
    A macro to get all interior colorindex has been used in thisworkbook
    和菜鸟一起学linux总线驱动之初识spi总线协议
    Android应用程序与SurfaceFlinger服务之间的共享UI元数据(SharedClient)的创建过程分析
    和菜鸟一起学android4.0.3源码之vibrator振动器移植心得
    opengl_NeNe 第九课,移动图像代码.vs 2010_express OPENGL 2.0
    和菜鸟一起学linux总线驱动之初识USB设备描述符
    和菜鸟一起学OK6410之熟悉内核源码
  • 原文地址:https://www.cnblogs.com/sweat123/p/5436647.html
Copyright © 2020-2023  润新知