zoukankan      html  css  js  c++  java
  • hdu5451_求循环节加矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5451

    题目描述:

      对于技术分享,给出x和mod,求y向下取整后取余mod的值为多少?

    找循环节解析链接:http://blog.csdn.net/ACdreamers/article/details/25616461

    裸题链接:http://blog.csdn.net/chenzhenyu123456/article/details/48529039

     1 #include <algorithm>
     2 #include <iostream>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <cstdio>
     6 #include <vector>
     7 #include <ctime>
     8 #include <queue>
     9 #include <list>
    10 #include <set>
    11 #include <map>
    12 using namespace std;
    13 #define INF 0x3f3f3f3f
    14 typedef long long LL;
    15 
    16 LL mod;
    17 struct Mat
    18 {
    19     LL a[3][3];
    20 };
    21 LL pow(LL base, LL n, LL mod)
    22 {
    23     LL res = 1;
    24     while(n)
    25     {
    26         if(n % 2)
    27             res = (res * base) % mod;
    28         base = (base * base) % mod;
    29         n >>= 1;
    30     }
    31     return res;
    32 }
    33 Mat mul(Mat x, Mat y)
    34 {
    35     Mat z;
    36     memset(z.a, 0, sizeof(z.a));
    37     for(int i = 0; i < 2; i++)
    38     {
    39         for(int k = 0; k < 2; k++)
    40         {
    41             for(int j = 0; j < 2; j++)
    42             {
    43                 z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]) % mod) % mod;
    44             }
    45         }
    46     }
    47     return z;
    48 }
    49 void solve(LL n)
    50 {
    51     Mat res, base;
    52     memset(res.a, 0, sizeof(res.a));
    53     res.a[0][0] = 1, res.a[1][1] = 1;
    54     base.a[0][0] = 5, base.a[0][1] = 2;
    55     base.a[1][0] = 12, base.a[1][1] = 5;
    56     while(n)
    57     {
    58         if(n % 2)
    59             res = mul(res, base);
    60         base = mul(base, base);
    61         n >>= 1;
    62     }
    63     LL ans = 0;
    64     ans = (ans + (res.a[0][0] * 5) % mod) % mod;
    65     ans = (ans + (res.a[1][0] * 2) % mod) % mod;
    66     ans = (ans * 2 - 1) % mod;
    67     printf("%lld
    ", ans);
    68 }
    69 
    70 int main()
    71 {
    72     int t;
    73     LL x;
    74     scanf("%d", &t);
    75     for(int i = 1; i <= t; i++)
    76     {
    77         scanf("%lld %lld", &x, &mod);
    78         LL y = (mod + 1) * (mod - 1);
    79         LL N = pow(2, x, y);
    80         printf("Case #%d: ", i);
    81         solve(N);
    82     }
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    DynamicObject
    ABP文档
    ABP文档
    ABP文档
    ABP文档
    ABP文档
    ABP文档
    第1张 Maven简介 学习笔记
    lambda表达式10个示例——学习笔记
    对象在内存中初始化的过程?
  • 原文地址:https://www.cnblogs.com/luomi/p/5751031.html
Copyright © 2011-2022 走看看