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

题意 : 优化按照已给的程序

思路 : 用已有的程序跑出前几个答案,找规律, fn = f n-1 + 2 * f n-2 + 1 , 重点还是构造矩阵,带常数项如何构造出矩阵

代码示例 :

struct mat
{
ll a[3][3];
}; ll m; mat mul(mat a, mat b){
mat r;
memset(r.a, 0, sizeof(r.a)); for(int i = 0; i < 3; i++){
for(int k = 0; k < 3; k++){
if (a.a[i][k]){
for(int j = 0; j < 3; j++){
if (b.a[k][j]){
r.a[i][j] += (a.a[i][k]*b.a[k][j])%m;
r.a[i][j] %= m;
}
}
}
}
}
return r;
} mat pow(mat a, ll n){
mat b;
memset(b.a, 0, sizeof(b.a));
b.a[0][0] = b.a[1][1] = b.a[2][2] = 1; while(n){
if (n&1) b = mul(a, b); //
a = mul(a, a);
n >>= 1;
}
return b;
} int main() {
ll n; while(~scanf("%lld%lld", &n, &m)){
mat a;
memset(a.a, 0, sizeof(a.a));
a.a[0][0] = a.a[1][0] = a.a[0][2] = a.a[2][2] = 1;
a.a[0][1] = 2;
if (n == 1) {
printf("%lld\n", 1%m);
}
else if (n == 2){
printf("%lld\n", 2%m);
}
else {
a = pow(a, n-2);
printf("%lld\n", (a.a[0][0]*2%m+a.a[0][1]%m+a.a[0][2]%m)%m);
} } return 0;
}

hdu - 4990的更多相关文章

  1. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  2. hdu 4990 Reading comprehension(等比数列法)

    题目链接:pid=4990" style="color:rgb(255,153,0); text-decoration:none; font-family:Arial; line- ...

  3. hdu 4990 Reading comprehension 二分 + 快速幂

    Description Read the program below carefully then answer the question. #pragma comment(linker, " ...

  4. HDU 4990 Ordered Subsequence --数据结构优化DP

    题意:给一串数字,问长度为m的严格上升子序列有多少个 解法:首先可以离散化为10000以内,再进行dp,令dp[i][j]为以第i个元素结尾的长度为j的上升子序列的个数, 则有dp[i][j] = S ...

  5. HDU 4990 Reading comprehension

    快速幂 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #i ...

  6. Reading comprehension HDU - 4990

    Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024 ...

  7. HDU 4990 Reading comprehension(矩阵快速幂)题解

    思路: 如图找到推导公式,然后一通乱搞就好了 要开long long,否则红橙作伴 代码: #include<set> #include<cstring> #include&l ...

  8. HDU 4990 Reading comprehension 简单矩阵快速幂

    Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...

  9. Reading comprehension HDU - 4990 (矩阵快速幂 or 快速幂+等比数列)

    ;i<=n;i++) { )ans=(ans*+)%m; %m; } 给定n,m.让你用O(log(n))以下时间算出ans. 打表,推出 ans[i] = 2^(i-1) + f[i-2] 故 ...

  10. hdu 4990(数学,等比数列求和)

    Reading comprehension Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. js 制作分页

    如图所示 在html中调用方法 getpage(7, 1, 1, 'URL') 1.page.js文件 代码 function getpage(count, countPage, pageIndex, ...

  2. ASP.NET一般登陆逻辑分享(01)

  3. 51nod 1380"夹克老爷的逢三抽一"(贪心+set)

    传送门 •参考资料 [1]:51Nod-1380-夹克老爷的逢三抽一 •题意 从长度为 n 的数组中抽取 $\frac{n}{3}$ 个不相邻的值使得加和最大(首尾也不能同时取) •题解 贪心选择当前 ...

  4. PC端页面如何调用QQ进行在线聊天?

    pc端如何实现QQ在线咨询? html代码如下: <a href="tencent://message/?uin=1234567&Site=Sambow&Menu=ye ...

  5. 在后台管理系统中引入富文本编辑器 (vue-quill-editor)

    在admin系统中引入富文本编辑器 (vue-quill-editor) 由于公司项目的需求,内容需要更新,那么自然需要admin后台来上传内容,在苦苦寻觅了N个编辑器之后,终于找到了一个比较容易使用 ...

  6. js基础——基本包装类型

    1.基本包装类型String   var bz = new String("Li.Linda"); //引用类型(object)         bz.name= bz.subst ...

  7. linux 在 1 MB 之下的 ISA 内存

    一个最著名的 I/O 内存区是在个人计算机上的 ISA 范围. 这是在 640 KB(0xA0000)和 1 MB(0x100000)之间的内存范围. 因此, 它正好出现于常规内存 RAM 中间. 这 ...

  8. 【t081】序列长度

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 有一个整数序列,我们不知道她的长度是多少(即序列中整数的个数),但我们知道在某些区间中至少有多少个整数 ...

  9. C# Thread.Join();Thread.Abort();

    Join() 等待当前线程运行完成后,才继续执行主线程后续代码: Abort() 结束当前线程,继续执行主线程后续代码: Thread.Join(); static void Main(string[ ...

  10. (转)hibernate缓存机制详细分析

    在本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级别).二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题. 随笔虽长,但我相 ...