hdu - 4990
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的更多相关文章
- 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(等比数列法)
题目链接:pid=4990" style="color:rgb(255,153,0); text-decoration:none; font-family:Arial; line- ...
- hdu 4990 Reading comprehension 二分 + 快速幂
Description Read the program below carefully then answer the question. #pragma comment(linker, " ...
- HDU 4990 Ordered Subsequence --数据结构优化DP
题意:给一串数字,问长度为m的严格上升子序列有多少个 解法:首先可以离散化为10000以内,再进行dp,令dp[i][j]为以第i个元素结尾的长度为j的上升子序列的个数, 则有dp[i][j] = S ...
- HDU 4990 Reading comprehension
快速幂 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #i ...
- Reading comprehension HDU - 4990
Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024 ...
- 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 ...
- 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] 故 ...
- hdu 4990(数学,等比数列求和)
Reading comprehension Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- HTML提供的6种空格
HTML提供了6种空格(space entity),它们拥有不同的宽度. 非断行空格( )是常规空格的宽度,可运行于所有主流浏览器.其它几种空格( . . ..)在不同浏览器中宽度各异. ...
- 【codeforces 789A】Anastasia and pebbles
[题目链接]:http://codeforces.com/contest/789/problem/A [题意] 有n种物品,每种物品有wi个; 你有两个口袋,每个口袋最多装k个物品; 且口袋里面只能装 ...
- Mac下SVN基本操作和常见错误
一.基本操作 1 从服务器上下载代码 svn checkout http://xxx.xxx.xxx/xxx 2 获取最新的代码 svn update 3 提交代码 svn commit -m ...
- Vue 小实例 跑马灯效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spring Data JPA坑点记录
本篇进行Spring-data-jpa的介绍,几乎涵盖该框架的所有方面,在日常的开发当中,基本上能满足所有需求.这里不讲解JPA和Spring-data-jpa单独使用,所有的内容都是在和Spring ...
- 2018-2-13-win10-UWP-Markdown-含源代码
title author date CreateTime categories win10 UWP Markdown 含源代码 lindexi 2018-2-13 17:23:3 +0800 2018 ...
- 这群程序员疯了!他们想成为IT界最会带货的男人
随着网红主播越来越火,通过直播带货种草的形式也成了今年双12的热点. 不过,网红主播带货早已见怪不怪,但你们见过程序员直播带货吗!? 近日,趁着阿里云双12年末采购节,阿里云邀请了一波程序员GG来为大 ...
- reactNative性能优化
本文将简单介绍一下我所收集到的React Native应用优化方法,希望对你有所启发.很多方法也是适用React web应用的. 包体积优化 无论是热更新方案走网络下载js,还是直接将js打进apk, ...
- 使用app-inspector时报错connect ECONNREFUSED 127.0.0.1:8001的解决方案
在使用 app-inspector -u udid时,报错如图所示 输入如下命令即可解决 npm config set proxy null 再次启动app-inspector即可成功
- 一天入门 Python 的一些心得
1. 前言 好久没写文了.最近在搞一些好玩的技术用到了 Python .我原以为要花些时日,谁知道第一天入门之后便没有再刻意地去学习它了.这里就写写其中的一些关键点吧.如果我去学一门语言不是因为它火了 ...