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. HDU 4811 Ball -2013 ICPC南京区域现场赛

    题目链接 题意:三种颜色的球,现给定三种球的数目,每次取其中一个放到桌子上,排成一条线,每次放的位置任意,问得到的最大得分. 把一个球放在末尾得到的分数是它以前球的颜色种数 把一个球放在中间得到的分数 ...

  2. MAC系统下,删除.svn文件

    MAC系统下,.svn文件是隐藏的. 如果项目是非export导出的,那么项目中会有很多的.svn文件. 如果项目的体积非常庞大,我们如何快速的批量删除.svn文件呢?下面是操作方法: 打开终端,cd ...

  3. php date函数 参数详细

    time()在PHP中是得到一个数字,这个数字表示从1970-01-01到现在共走了多少秒,很奇怪吧 不过这样方便计算, 要找出前一天的时间就是 time()-60*60*24; 要找出前一年的时间就 ...

  4. WhaleSong

    Chasingwaves by myself in theocean of endless sorrow Makingwishes that i will find myherd tomorrow 5 ...

  5. NYOJ题目112指数运算

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAs0AAAIICAIAAAAaCETRAAAgAElEQVR4nO3drW7jWtwv4PcmwnMhxb ...

  6. maven入门基础(转)

    maven介绍 maven是构建工具,也是构建管理工具.ant只是构建工具,因为不支持生成站点功能,只有预处理,编译,打包,测试,部署等功能. maven坐标 groupId:项目组织的逆向域名,比如 ...

  7. web.config详解 -- asp.net夜话之十一

    1.配置文件节点说明    1.1 <appSettings>节点    1.2 <connectionStrings>节点    1.3 <compilation> ...

  8. 傻瓜式十分钟免费开启 HTTPS,是时候为你的站点加上小绿锁了

    http://gold.xitu.io/entry/57df65690bd1d00057f9455b?from=singlemessage&isappinstalled=0 原文链接:http ...

  9. Shell编程基础教程5--文本过滤、正则表达式、相关命令

    5.文本过滤.正则表达式.相关命令    5.1.正则表达式(什么是正则表达式?正则表达式怎么进行匹配?常用命令)        简介:            一种用来描述文本模式的特殊语法      ...

  10. Analysis Services OLAP 概述2

    在DW/BI系统中,关系型数据库是存储和管理数据的最佳场所.但是关系数据库本身的智能化程度不够.关系型数据库缺乏如下功能: 丰富的元数据,帮助用户浏览数据和创建查询. 强大的分析计算和函数,在对上下文 ...