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. webpack学习(五)entry和output的基础配置

    1:entry和output就是打包的入口和出口的两个对象.但是如果入口文件就一个的话(应该说只希望打包出一个脚本文件), entry直接跟一个字符串(入口文件路径)就可以了.如:entry : &q ...

  2. 【b804】双栈排序

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入 ...

  3. MAMP "403 Forbidden You don't have permission to access / on this server."

    2015年01月22日 17:27:31 阅读数:3488 用MAMP搭建本地服务器的时候,设置好ip和端口等属性之后,浏览器访问,报 403错误: Forbidden You don't have ...

  4. vuex 快速上手,具体使用方法总结(含使用例子)

    网上有关vuex的文章很多,但有些比较复杂,这篇文章能让你快速使用vuex: vuex 用处:管理全局状态(类似全局变量,每个组件都能访问到) vuex 用法: //下面是一个js文件,用最简单最全的 ...

  5. Jmeter 发送json

    阅读更多 使用jmeter发送json数据.方法有三种 原创,转载请注明出处 1.利用CSV Data set Config. 参考: http://demi-panda.com/2013/01/08 ...

  6. Linux 内核 中断 urb

    函数 usb_fill_int_urb 是一个帮忙函数, 来正确初始化一个 urb 来发送给 USB 设备的 一个中断端点: void usb_fill_int_urb(struct urb *urb ...

  7. redis scan count的含义/二进制安全问题

    redis是单线程的,keys查询键类似hbase的全表扫描(也可以理解为select *),大数据量时非常耗时,因此官方给出了scan,使用scan类似数据库分页,可以指定查询多少个元素,官网的说明 ...

  8. C#获取美团评价信息

    闲来无事,朋友需要一家美团店铺的评价消息,索性做个小工具. 一:第一步找到目标网站 地址:https://www.meituan.com/meishi/4460141/ 二:分析网页请求 在目标网页, ...

  9. VXLAN IBGP RR 实验

    网络拓扑图: SPINE1配置 ====================================================== hostname SPINE-1nv overlay ev ...

  10. Xcode崩溃日志分析工具symbolicatecrash用法

    1.什么是symbolicatecrash? symbolicatecrash是Xcode自带的一个分析工具,可以通过机器上的崩溃日志和应用的.dSYM文件定位发生崩溃的位置,把crash日志中的一堆 ...