C - Reading comprehension 二分法 求等比数列前N项和
#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项和的更多相关文章
- poj 1845(等比数列前n项和及高速幂)
Sumdiv Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13959 Accepted: 3433 Descripti ...
- 【POJ 1845】 Sumdiv (整数唯分+约数和公式+二分等比数列前n项和+同余)
[POJ 1845] Sumdiv 用的东西挺全 最主要通过这个题学了约数和公式跟二分求等比数列前n项和 另一种小优化的整数拆分 整数的唯一分解定理: 随意正整数都有且仅仅有一种方式写出其素因子的乘 ...
- 18. 求交错序列前N项和
求交错序列前N项和 #include <stdio.h> int main() { int numerator, denominator, flag, i, n; double item, ...
- 循环-10. 求序列前N项和*
/* * Main.c * C10-循环-10. 求序列前N项和 * Created on: 2014年7月30日 * Author: Boomkeeper *******部分通过******* */ ...
- 循环-21. 求交错序列前N项和
/* * Main.c * C21-循环-21. 求交错序列前N项和 * Created on: 2014年8月18日 * Author: Boomkeeper ***********测试通过**** ...
- 求交错序列前N项和(15 分)
7-2 求交错序列前N项和(15 分) 本题要求编写程序,计算交错序列 1-2/3+3/5-4/7+5/9-6/11+... 的前N项之和. 输入格式: 输入在一行中给出一个正整数N. 输出格式: 在 ...
- 第2章-6 求交错序列前N项和 (15分)
第2章-6 求交错序列前N项和 (15分) 本题要求编写程序,计算交错序列 1-2/3+3/5-4/7+5/9-6/11+- 的前N项之和. 输入格式: 输入在一行中给出一个正整数N. 输出格式: 在 ...
- 求等差数列前$n$项和$S_n$的最值
一.方法依据: 已知数列\(\{a_n\}\)是等差数列,首项为\(a_1\),公差为\(d\),前\(n\)项和为\(S_n\),则求\(S_n\)的最值常用方法有两种: (1).函数法:由于\(S ...
- 05-0. 求序列前N项和(15)
本题要求编写程序,计算序列 2/1+3/2+5/3+8/5+... 的前N项之和.注意该序列从第2项起,每一项的分子是前一项分子与分母的和,分母是前一项的分子. 输入格式: 输入在一行中给出一个正整数 ...
随机推荐
- E20170619-hm
bucket n. 水桶; open hash [词典] [计] 开放散列,开混列; spirit n. 精神,心灵; 情绪; 勇气; 精髓; flesh n. 肉; 肉体; 果肉; 皮 ...
- 基于行为树的AI 与 Behavior Designer插件
优点: 0.行为逻辑和状态数据分离,任何节点都可以反复利用. 1.高度模块化状态,去掉状态中的跳转逻辑,使得状态变成一个"行为". 2."行为" ...
- Python基础数据类型(五) dict字典
字典dict{} 字典数字自动排序 enumerate 枚举 for i,k in enumerate(dic,1) #第二个参数默认不写就是0 ---枚举 print(i,k) dict,以{}来表 ...
- jdk1.8 api 下载
链接: https://pan.baidu.com/s/1Wmf2vzXxclVcBPUfPp_g_A 提取码: dpwu 希望那些CSDN的不要借此収积分,行行好吧你,小众程序员就是为了方便 凑字数 ...
- 题解报告:poj 3233 Matrix Power Series(矩阵快速幂)
题目链接:http://poj.org/problem?id=3233 Description Given a n × n matrix A and a positive integer k, fin ...
- ANDROID 开发之 SQLite
SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大.SQLite具备下列特点: 1.轻量级 使用 SQLit ...
- 简述UIDatePicker的用法
1.Locale 设置DatePicker的地区,即设置DatePicker显示的语言. 1.跟踪所有可用的地区,取出想要的地区 NSLog(@"%@", [NSLocale av ...
- Android 在fragment中实现返回键单击提醒 双击退出
尝试用mvp架构加dagger2来重写了一下,大致功能都实现了,还没有全部完成. 项目地址 接近完成的时候,想在天气信息页面实现一个很常见的功能,也就是点击屏幕下方的返回键的时候不是返回到上一个act ...
- VMWare 安装Ubuntu 16.04
1.新建虚拟机 (1)点击文件-->新建虚拟机 (2)选择 自定义(高级)--> 下一步 (3)选择Workstation 12.0 --> 下一步 (4)选择 稍后安装操作系统 - ...
- C# windform自定义控件的属性小知识
word中的加粗变斜之类的一直让我以为是button,直到我接触了自定义控件,才发现实现这种机能最好的是CheckBox,然后我们在做一个系统的时候,这种控件有可能要用好多次,总不能在用一次的时候,就 ...