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; 
}

InputMulti test cases,each line will contain two integers n and m. Process to end of file. 
TechnicalSpecification 
1<=n, m <= 1000000000OutputFor each case,output an integer,represents the output of above program.Sample Input

1 10
3 100

Sample Output

1
5 等比数列二分求和:

(1)时,

(2)时,那么有

(3)时,那么有

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 18
#define N 33
#define MOD 10000007
#define INF 1000000009
const double eps = 1e-;
const double PI = acos(-1.0);
LL n, m, ans;
/*
等比数列二分求和
*/
LL fpow(LL a, LL b)
{
LL ret = , tmp = a;
while (b != )
{
if (b & )
ret = ret*tmp%m;
tmp = tmp*tmp%m;
b /= ;
}
return ret%m;
}
LL sum(LL a, LL k)
{
LL t, cur;
if (k == )
return a;
else if (k % == )
{
t = sum(a, k / ),cur = fpow(a, k / + );
t = (t + t * cur%m) % m;
t = (t + cur) % m;
}
else
{
t = sum(a, k / ), cur = fpow(a, k / );
t = (t + t * cur % m) % m;
}
return t;
}
LL solve(LL num)
{
if (num == )
return ;
else
return ( + sum(, num)) % m;
}
int main()
{
while (cin >> n >> m)
{
if (n & )
cout << solve(n/)%m << endl;
else
cout << solve((n - )/)*%m << endl;
}
}
												

C - Reading comprehension 二分法 求等比数列前N项和的更多相关文章

  1. poj 1845(等比数列前n项和及高速幂)

    Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13959   Accepted: 3433 Descripti ...

  2. 【POJ 1845】 Sumdiv (整数唯分+约数和公式+二分等比数列前n项和+同余)

    [POJ 1845] Sumdiv 用的东西挺全 最主要通过这个题学了约数和公式跟二分求等比数列前n项和 另一种小优化的整数拆分  整数的唯一分解定理: 随意正整数都有且仅仅有一种方式写出其素因子的乘 ...

  3. 18. 求交错序列前N项和

    求交错序列前N项和 #include <stdio.h> int main() { int numerator, denominator, flag, i, n; double item, ...

  4. 循环-10. 求序列前N项和*

    /* * Main.c * C10-循环-10. 求序列前N项和 * Created on: 2014年7月30日 * Author: Boomkeeper *******部分通过******* */ ...

  5. 循环-21. 求交错序列前N项和

    /* * Main.c * C21-循环-21. 求交错序列前N项和 * Created on: 2014年8月18日 * Author: Boomkeeper ***********测试通过**** ...

  6. 求交错序列前N项和(15 分)

    7-2 求交错序列前N项和(15 分) 本题要求编写程序,计算交错序列 1-2/3+3/5-4/7+5/9-6/11+... 的前N项之和. 输入格式: 输入在一行中给出一个正整数N. 输出格式: 在 ...

  7. 第2章-6 求交错序列前N项和 (15分)

    第2章-6 求交错序列前N项和 (15分) 本题要求编写程序,计算交错序列 1-2/3+3/5-4/7+5/9-6/11+- 的前N项之和. 输入格式: 输入在一行中给出一个正整数N. 输出格式: 在 ...

  8. 求等差数列前$n$项和$S_n$的最值

    一.方法依据: 已知数列\(\{a_n\}\)是等差数列,首项为\(a_1\),公差为\(d\),前\(n\)项和为\(S_n\),则求\(S_n\)的最值常用方法有两种: (1).函数法:由于\(S ...

  9. 05-0. 求序列前N项和(15)

    本题要求编写程序,计算序列 2/1+3/2+5/3+8/5+... 的前N项之和.注意该序列从第2项起,每一项的分子是前一项分子与分母的和,分母是前一项的分子. 输入格式: 输入在一行中给出一个正整数 ...

随机推荐

  1. 学学cookie和session

    什么是Cookie? HTTP Cookie(也叫 Web Cookie或浏览器 Cookie)是服务器发送到用户浏览器并保存在本地的一小块数据,它会在浏览器下次向同一服务器发起请求时被携带并发送到服 ...

  2. 9.10NOIP模拟题

      9.10 NOIP模拟赛 题目名称 区间 种类 风见幽香 题目类型 传统 传统 传统 可执行文件名 section kinds yuuka 输入文件名 section.in kinds.in yu ...

  3. 能力 or say 职业 规划

    2019.5.8 黑盒测试,白盒测试,接口测试,自动化测试,性能测试..  往测试工程师发展,再是测试开发,高级测试开发..  要是真的喜欢前端,可以再转吧.前端后端应该要清楚它们的区别 前端:广度, ...

  4. Codeforces 771C

    我的树形dp果然是渣... 题意:给一棵树,共n(0<n<=15e4)个节点,可在树上进行跳跃,每次跳的最大距离为k(0<k<=5),定义f(s,t)为(dis(s,t)+k) ...

  5. springMVC是什么等七个问题

  6. 最新省市区划分码code

    爬取国家统计局省市区code 提供php爬取脚本以及json和sql https://github.com/zzDylan/area-code 觉得好用给个star,3q

  7. 【转】Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)

    概要 前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例 ...

  8. Microsoft SQL Server学习(一)--基本概念

    数据库的分类 关系型数据库 非关系型数据库 数据库的发展 数据都是以文件的形式存储在硬盘上FATFAT32NTFS LinuxEXT 数据库设计流程 文件系统缺陷 数据库的专业术语 数据库引擎服务 E ...

  9. 六时车主 App 隐私政策

    六时车主 App 隐私政策   本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义 ...

  10. CaffeMFC:caffe.pb.h(2525): error C2059: syntax error : 'constant'

    下边的语句会报 syntax error : 'constant'. static const DimCheckMode STRICT = V1LayerParameter_DimCheckMode_ ...