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. 【opencv】图片标注文字

    IplImage* pstImg; HI_CHAR as8Title[25];CvFont stTimeFont;//字体信息cvInitFont(&stTimeFont,CV_FONT_HE ...

  2. IOS - Passbook

    1. 什么是Passbook Passbook是苹果公司于北京时间2012年6月12日上午,在全球开发者大会(WWDC)上宣布了iOS 6系统将提供操作一个全新的应用——Passbook 这是一款可以 ...

  3. 25个增强iOS应用程序性能的提示和技巧(中级篇)(3)

    25个增强iOS应用程序性能的提示和技巧(中级篇)(3) 2013-04-16 14:42 破船之家 beyondvincent 字号:T | T 本文收集了25个关于可以提升程序性能的提示和技巧,分 ...

  4. 经典.net试题

    经典.net面试题目 1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . private :   私有成员, 在类的内部才可以访问. pr ...

  5. JavaScript基础——处理字符串

    String对象是迄今为止在JavaScript中最常用的对象.在你定义一个字符串数据类型的变量的任何时候,JavaScript就自定为你创建一个String对象.例如: var myStr = &q ...

  6. 加载gif动画的三种方式

    GifView.h/** * 调用结束就开始播放动画,如果需要用户指定何时播放的话,只需要把timer的开始放到合适的位置.通过对CFDictonaryRaf 也就是gifProperties的改变, ...

  7. 重温WCF之数据契约中使用枚举(转载)(十一)

    转载地址:http://www.zhuli8.com/wcf/EnumMember.html 枚举类型的定义总是支持序列化的.当我们定义一个新的枚举时,不必应用DataContract特性,就可以在数 ...

  8. Arch Linux Installation Guide

    Arch Linux Installation Guide   timedatectl set-ntp true   sed -i '/Score/{/China/!{n;s/^/#/}}' /etc ...

  9. [LeetCode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  10. git branch用法总结

    git branch      git branch 不带参数:列出本地已经存在的分支,并且在当前分支的前面加“*”号标记,例如:   #git branch* master   newbranch ...