hdu 4990 Reading comprehension 二分 + 快速幂
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;
}
#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
[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); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧