C - Reading comprehension 二分法 求等比数列前N项和
2024-09-04 14:05:12
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>
#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;
}
}
最新文章
- Redux状态管理方法与实例
- [bigdata] spark集群安装及测试
- iOS:使用模板引擎渲染HTML界面
- CDS
- MVC使用ASP.NET Identity 2.0实现用户身份安全相关功能,比如通过短信或邮件发送安全码,账户锁定等
- markdown安装和使用
- CSS/JS图片鼠标悬浮一道光闪过
- 关于iPhone多点触控
- Android Touch事件传递机制解析 (推荐)
- nodejs快速入门
- MySQL基础之第18章 性能优化
- python中xrange和range的区别
- DAO 基础学习笔记
- LoaderManager使用具体解释(二)---了解LoaderManager
- THE R QGRAPH PACKAGE: USING R TO VISUALIZE COMPLEX RELATIONSHIPS AMONG VARIABLES IN A LARGE DATASET, PART ONE
- Java语言的9个主要特性
- 【Ubuntu 16】安装net-snmp
- C#开发WEBService服务 C++开发客户端调用WEBService服务
- php中数组直接用加号相加array+array
- AI-视图组件-五个接口的最终简化版