题目链接:

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. 笔试算法题(27):判断单向链表是否有环并找出环入口节点 & 判断两棵二元树是否相等

    出题:判断一个单向链表是否有环,如果有环则找到环入口节点: 分析: 第一个问题:使用快慢指针(fast指针一次走两步,slow指针一次走一步,并判断是否到达NULL,如果fast==slow成立,则说 ...

  2. linux如何正确设置静态ip

    如果是新安装的CentOS7的用户,刚开始应该是没网的,ifconfig命令在CentOS7已经被淘汰了. 1.使用ip addr 即查看分配网卡情况. 2.激活网卡 [root@localhost ...

  3. 15Spring AOP基础

    为什么需要AOP? 先来看一段代码: package com.cn.spring.aop.helloworld; //加减乘除的接口类 public interface ArithmeticCalcu ...

  4. python+selenium之元素的八大定位方法

    以百度搜索框为例,先打开百度网页 1.点右上角爬虫按钮 2.点左下角箭头 3.讲箭头移动到百度搜索输入框上,输入框高亮状态 4.下方红色区域就是单位到输入框的属性: <input id=&quo ...

  5. Vue如何在data中正常引入图片路径

    在Vue项目中通过data设置图片路径,然后在template中引入后页面无法显示图片,浏览器控制台报错:                        刚开始以为是路径出问题了,于是绝对路径.相对路 ...

  6. 升级到Offiec 2016后 Power View 不见了的处理方法

    好吧 并不是没有了,而只是快捷方式需要手动的调整出来, 过程还是挺复杂,给一个官方文档吧. Turn on Power View in Excel 2016 for Windows https://s ...

  7. Leetcode 212.单词搜索II

    单词搜索II 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻&q ...

  8. HDU 1564 找规律博弈

    题目大意是: 从n*n的方格角落的一个起点出发,每次移到上下左右一个未曾到达过的位置,谁不能走了谁就输了 想了好久都想不出,看了大神的题解 Orz了 果然博弈不是脑残的游戏啊... 这里从起点出发,将 ...

  9. Android TextView内容过长加省略号

    在Android TextView中有个内容过长加省略号的属性,即ellipsize,用法如下: 在xml中: android:ellipsize = "end" //省略号在结尾 ...

  10. MU Puzzle HDU - 4662

    Suppose there are the symbols M, I, and U which can be combined to produce strings of symbols called ...