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. [转]ASP.NET WebApi OWIN 实现 OAuth 2.0

    OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. OAuth 允许用户提供一个令牌, ...

  2. VUE框架思想

    学习VUE的第一步就是先了解这个框架的的核心思想 Vue.js的核心思想就是,它是一套__渐进式的自底层向上增量开发__的__MVVM__结构的框架 什么是框架? 简单的讲,框架就是将与业务无关的重复 ...

  3. vue中的时间修饰符stop,self

    stop阻止自身以外的冒泡 self只会阻止自身冒泡

  4. 用es5实现模板字符串

    废话不多说,主要是利用正则表达式replace+eval动态取值(纯属娱乐) String.prototype.myReplace = function(){ return this.replace( ...

  5. PDF.JS 读取文件流前端展示 C#

    最近再搞PDF得展示问题,因为aspose.pdf成本太高,只能使用pdf.js这个开源强大的前端东东了. 在百度了很久后 网上大都是node,java,php的事例,有位大哥的是C#的后台代码按他写 ...

  6. windows下使用cmake+mingw配置makefile

    前面一节说了cmake简易使用,但是实际开发中项目文件非常多,使用哪种简易方式会导致代码十分混乱,因此本文介绍一种cmake管理大型项目的demo流程. 具体步骤如下: 1.创建相关的项目目录 cmd ...

  7. vs指定QT的工作目录(依赖第三方动态库时,这时vs编译出来后,运行会提示缺少动态库)good

    当一个工程依赖第三方动态库时,这时vs编译出来后,运行会提示缺少动态库.解决方法: 项目->属性->调试: 工作目录:指定程序运行时的目录 环境:指定程序运行时的环境变量 我们可以在环境变 ...

  8. Python_自定义关键字的使用

    1.在Python中新建一个套件MOSAPP(一般为APP名称):New Suite→Directory 2.在套件下新建个资源文件My:New Resource:My 3.在My资源文件库下新建个关 ...

  9. mysql主从之基于atlas读写分离

    一 mysql读写分离的概念 写在主库,主库一般只有一个,读可以分配在多个从库上,如果写压力不大的话,也能把读分配到主库上. 实现是基于atlas实现的,atlas是数据库的中间件,程序只需要连接at ...

  10. CSS3(3)---2D变形(transform)

    CSS3(3)---2D变形(transform) 2D变形是CSS3中具有颠覆性的特征之一,它常用的属性有:移动(translate).缩放(scale).旋转(rotate).倾斜 (skew). ...