hdu 4990 Reading comprehension(等比数列法)
思路:曾经有一个矩阵乘法的做法请戳这儿。。
。
。
開始我们把数都不模。。。
能够得到一个规律
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(等比数列法)的更多相关文章
- HDU - 4990 Reading comprehension 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...
- hdu 4990 Reading comprehension 二分 + 快速幂
Description Read the program below carefully then answer the question. #pragma comment(linker, " ...
- HDU 4990 Reading comprehension
快速幂 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #i ...
- HDU 4990 Reading comprehension(矩阵快速幂)题解
思路: 如图找到推导公式,然后一通乱搞就好了 要开long long,否则红橙作伴 代码: #include<set> #include<cstring> #include&l ...
- HDU 4990 Reading comprehension 简单矩阵快速幂
Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...
- 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 ...
- hdu 4990(数学,等比数列求和)
Reading comprehension Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu-4990 Reading comprehension(快速幂+乘法逆元)
题目链接: Reading comprehension Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification
论文选读二:Multi-Passage Machine Reading Comprehension with Cross-Passage Answer Verification 目前,阅读理解通常会给出 ...
随机推荐
- Spring Boot . 4 -- 定制 Spring Boot 配置 【2】
除了第一篇中使用 覆写的方式进行 自动配置的更改外,还可以通过 Spring Boot 中提供的 application.properties 文件改变应用的运行时配置.这种配置的方式粒度是非常精细的 ...
- 原生javascript实现call、apply和bind的方法
var context = {id: 12}; function fun (name, age) { console.log(this.id, name, age) } bind bind() 方法会 ...
- tomcat无法正确解析请求参数
24-Mar-2018 14:11:20.564 INFO [http-nio-8080-exec-3] org.apache.coyote.http11.Http11Processor.servic ...
- Linux系统安装Apache
一,Apache和tomcat的区别与联系 apache是web服务器,web服务器专门处理http请求: tomcat是运行在apache上的应用服务器: apache是普通服务器,本身只支持htm ...
- PHP:压缩 Zip
文章来源:http://www.cnblogs.com/hello-tl/p/7661222.html <?php # 文件字符集 header("Content-type: text ...
- angular2集成highchart
集成highchart的配置困扰了我很久,今天终于解决了: 1.修改tsconfig.app.json: "compilerOptions": { //... "type ...
- Windows 下安装 Node.js
搭建博客系列的 Node.js 环境安装.Windows 下面安装可以通过图形化界面进行安装,非常方面. 1.打开 Node.js 官网,下载对应版本的安装包(msi 后缀的) 2.双击运行下载的程序 ...
- BNUOJ 26228 Juggler
Juggler Time Limit: 3000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 42 ...
- 面向对象:元类、异常处理(try...except...)
元类: python中一切皆对象,意味着: 1. 都可以被引用,如 x = obj 2. 都可以被当做函数的参数传入 3. 都可以被当做函数的返回值 4. 都可以当做容器类的元素(列表.字典.元祖.集 ...
- [K/3Cloud] 调用其他界面时通过Session传递对象参数
DynamicFormShowParameter参数的CustomParams参数列表只支持string类型的参数,对于复杂参数的传递需要通过单据View对象的共享Session来完成,如: 在调用界 ...