Description

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
思路:若n为奇数, ans(n) = 2^(n-1) + 2^(n-3) +...2^0
   若n为偶数, ans(n) = 2^(n-1) + 2^(n-3) +...2^1
 
由于 2^1+2^2+2^3+2^4 = (2^1+2^2) + 2^2*(2^1+2^2)  故只需要算一半就好了, 那么就可以递归处理,注意,若为奇数项,把最后一项算出来加上去就好了
n为奇数时的递归边界是:2^0 = 1;
n为偶数时的递归边界是:2^1 = 2;
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
using namespace std;
typedef long long LL;
const int N = 100005;
LL n, m, nn, mm;
LL pow_mod(LL b)
{
LL res = 1, a = 2;
while(b) {
if(b & 1) res = res * a % m;
a = (a % m) * a % m;
b >>= 1;
}
return res;
}
LL sum1(LL k)
{
if(k == 1) return 1;
LL tmp, now;
if(k & 1) tmp = pow_mod(k - 1);
else tmp = pow_mod(k); now = sum1(k >> 1) % m;
LL res = (now % m + (now * tmp) % m) % m;
if(k & 1) res = res + pow_mod(k * 2 - 2) % m;
return res; }
LL sum2(LL k)
{
if(k == 1) return 2;
LL tmp, now;
if(k & 1) tmp = pow_mod(k - 1);
else tmp = pow_mod(k); now = sum2(k >> 1) % m;
LL res = (now % m + (now * tmp) % m) % m;
if(k & 1) res = res + pow_mod(k * 2 - 1) % m;
return res;
}
int main()
{
while(~scanf("%lld%lld", &n, &m))
{
LL ans = 0;
if(n & 1) {
nn = (n + 1) >> 1;
ans = sum1(nn);
printf("%lld\n", ans % m);
}
else {
mm = (n) >> 1;
ans = sum2(mm);
printf("%lld\n", ans % m);
}
}
}

  

 
 
 
 
 
 

hdu 4990 Reading comprehension 二分 + 快速幂的更多相关文章

  1. 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 ...

  2. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

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

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

  4. HDU 4990 Reading comprehension

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

  5. hdu4990 Reading comprehension 矩阵快速幂

    Read the program below carefully then answer the question.#pragma comment(linker, "/STACK:10240 ...

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

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

  7. hdu 4990 Reading comprehension(等比数列法)

    题目链接:pid=4990" style="color:rgb(255,153,0); text-decoration:none; font-family:Arial; line- ...

  8. HDU 4506 小明系列故事——师兄帮帮忙(二分快速幂)

    题意:就是输入一个数组,这个数组在不断滚动,而且每滚动一次后都要乘以一个数,用公式来说就是a[i] = a[i-1] * k;然后最后一位的滚动到第一位去. 解题报告:因为题目中的k要乘很多次,达到了 ...

  9. HDU 4549 (费马小定理+矩阵快速幂+二分快速幂)

    M斐波那契数列 Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

随机推荐

  1. sql语句按照汉字拼音首字母排序

    oracle : 在oracle9i中新增了按照拼音.部首.笔画排序功能.设置NLS_SORT值SCHINESE_RADICAL_M 按照部首(第一顺序).笔划(第二顺序)排序SCHINESE_STR ...

  2. yii验证码不使用model在控制器中进行验证

    控制器 public function actionCheckLogin(){ if(!$this->createAction('verify_code')->validate($_POS ...

  3. XML 数据请求与JSON 数据请求

    (1)XML 数据请求 使用 AFNetworking 中的 AFHTTPRequestOperation 和 AFXMLParserResponseSerializer,另外结合第三方框架 XMLD ...

  4. 常用邮箱的服务器(SMTP/POP3)地址和端口总结

    163.com: POP3服务器地址:pop.163.com(端口:110) SMTP服务器地址:smtp.163.com(端口:25) 126邮箱: POP3服务器地址:pop.126.com(端口 ...

  5. notifyDataSetInvalidated和notifyDataSetChanged有什么区别

    notifyDataSetChanged方法通过一个外部的方法控制如果适配器的内容改变时需要强制调用getView来刷新每个Item的内容.public void notifyDataSetChang ...

  6. Yslow网站性能优化工具

    Yslow是一款网站性能优化的插件:

  7. Android Programming: Pushing the Limits -- Chapter 1: Fine-Tuning Your Development Environment

    ADB命令 Application Exerciser Monkey Gradle ProGuard 代码重用 版本控制 静态代码分析 代码重构 开发者模式   ADB命令: @.adb help:查 ...

  8. 清空mysql的历史记录

    # vi ~/.mysql_history show tables; show databases; 清空里面的内容,并不用退出当前shell,就可以清除历史命令!!

  9. MyBatis魔法堂:即学即用篇

    一.前言                   本篇内容以理解MyBatis的基本用法和快速在项目中实践为目的,遵循Make it work,better and excellent原则. 技术栈为My ...

  10. SQLServer内置函数

    类型转换函数cast和convert --cast一般更容易使用,convert的优点是可以格式化日期和数值 select CAST('123.4' as int) --失败 select CONVE ...