题目链接:

pid=4990" style="color:rgb(255,153,0); text-decoration:none; font-family:Arial; line-height:26px">http://acm.hdu.edu.cn/showproblem.php?pid=4990

思路:曾经有一个矩阵乘法的做法请戳这儿。。

開始我们把数都不模。。。

能够得到一个规律

n:1        ans:1      4^0 
                        n:2     ans:2         2*(4^0)

2                
5      4^0+4^1                        4              10 
     2*(4^0+4^1)

3                 21    4^0+4^1+4^2                6 
            42      2*(4^0+4^1+4^2  )

7                 85    4^0+4^1+4^2+4^3         8 
            170   
2*(4^0+4^1+4^2+4^3  )

所以能够看出规律。。

。然后我们直接计算。

。。。注意不能用等比数列的求和公式。。。。得用分治法中的等比数列求和。。。。。

code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath> using namespace std; typedef __int64 LL; int mod; LL power(LL p,LL n) //高速幂
{
LL sq=1;
while(n>0)
{
if(n%2) sq=sq*p%mod;
n/=2;
p=p*p%mod;
}
return sq;
} LL sum(LL p,LL n) //等比数列求和
{
if(n==0) return 1;
if(n%2)
{
return (sum(p,n/2)*(1+power(p,n/2+1)))%mod;
}
else
{
return (sum(p,n/2-1)*(1+power(p,n/2+1))+power(p,n/2))%mod;
}
} int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
mod=m;
int ans=0;
if(n&1)
{
ans=sum(4,n/2);
}
else
{
ans=sum(4,n/2-1);
ans*=2;
}
printf("%d\n",ans%mod);
}
return 0;
}

hdu 4990 Reading comprehension(等比数列法)的更多相关文章

  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 二分 + 快速幂

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

  3. HDU 4990 Reading comprehension

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

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

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

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

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

  6. HDU 4990 Reading comprehension 矩阵快速幂

    题意: 给出一个序列, \(f_n=\left\{\begin{matrix} 2f_{n-1}+1, n \, mod \, 2=1\\ 2f_{n-1}, n \, mod \, 2=0 \end ...

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

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

  8. hdu-4990 Reading comprehension(快速幂+乘法逆元)

    题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K ( ...

  9. 论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification

    论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification 目前,阅读理解通常会给出 ...

随机推荐

  1. vue跨域解决方法 及设置api路径方法

    vue项目中,前端与后台进行数据请求或者提交的时候,如果后台没有设置跨域,前端本地调试代码的时候就会报“No 'Access-Control-Allow-Origin' header is prese ...

  2. <Spring Cloud>入门一 Eureka Server

    1.搭建父工程 主要是添加版本依赖,此处版本是: spring-boot  : 2.0.8.RELEASE spring-cloud : Finchley.SR2 <?xml version=& ...

  3. Python:用户自定义异常

    实际开发中,有时候系统提供的异常类型不能满足开发的需求.这时候你可以通过创建一个新的异常类来拥有自己的异常.异常类继承自 Exception 类,可以直接继承,或者间接继承. 1.自定义异常类型 #1 ...

  4. buf.equals()

    buf.equals(otherBuffer) otherBuffer {Buffer} 返回:{Boolean} 返回一个 boolean 标识,无论 this 和 otherBuffer 是否具有 ...

  5. 《机器学习实战》-逻辑(Logistic)回归

    目录 Logistic 回归 本章内容 回归算法 Logistic 回归的一般过程 Logistic的优缺点 基于 Logistic 回归和 Sigmoid 函数的分类 Sigmoid 函数 Logi ...

  6. 修改Python的镜像源

    Mac OS下修改Python的镜像源 步骤: 切换到家目录 创建目录 .pip 并切换到该目录 创建 pip.conf 文件并写入配置信息 [global] index-url = https:// ...

  7. windows下mysql 5.7版本中修改编码为utf-8的方法

    方法如下 首先通过 show variables like 'character_set_%';查看mysql字符集情 默认编码为 latin1 然后关闭数据库 在mysql安装目录下找到my.ini ...

  8. Spring核心技术(七)——Spring容器的扩展

    本文将讨论如何关于在Spring生命周期中扩展Spring中的Bean功能. 容器的扩展 通常来说,开发者不需要通过继承ApplicationContext来实现自己的子类扩展功能.但是Spring ...

  9. Spring 事物注解属性

    @Transactional属性 . propagation 事物的传播属性 . isolation 事物的隔离属性 . readonly 设置只读属性 . timeout 设置超时属性 . roll ...

  10. 九度oj 题目1060:完数VS盈数

    题目1060:完数VS盈数 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:6461 解决:2426 题目描述: 一个数如果恰好等于它的各因子(该数本身除外)子和,如:6=3+2+1.则称其 ...