这个题目套公式 2^(n-1)-1,再来个快速幂基本上就可以AC了
写这个题目的: 公式容易推到错: 容易写成 2^n-1/2。。。这样写出来结果也不错 但是一直哇
AC:
#include<iostream> #include<cstdio> #include<cstring> #define N 1000000007 using namespace std; typedef long long ll; int pow(ll x,ll y) { ll res=1; while(y) { if(y&1) res = res * x %N; x = x * x % N; y>>=1; } return res-1; } int main() { int t; scanf("%d",&t); while(t--) { ll n,m; scanf("%lld",&n); m=pow(2,n-1); cout<<m<<endl; } }
本文介绍了一种使用快速幂算法解决特定数学问题的方法,通过公式2^(n-1)-1来计算,避免了常见的错误如写成2^n-1/2。提供了完整的C++代码实现,包括快速幂函数的定义和主函数中读取输入、调用快速幂函数的过程。

818

被折叠的 条评论
为什么被折叠?



