#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 100010
#define mod 1000000007
using namespace std; ll quick_mod(ll a,ll b,ll c)
{
ll ans=;
while(b)
{
if(b&)
{
ans=(ans*a)%c;
b--;
}
b/=;
a=(a*a)%c;
}
return ans;
}
int main()
{
// freopen("a.txt","r",stdin);
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
printf("%lld\n",quick_mod(a,b,c));
return ;
}

http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013

这个题需要用到两次二分:第一次是在求3^n次方,第二次是求3^0+3^1+...3^n.

第一次二分就是上面的快速幂,第二次二分:

加入n=5       3^1+3^2+3^3+3^4+3^5 = 3^1+3^2 + 3^2*(3^1+3^2) +3^5

这样就可以递归求解了,跟矩阵快速幂类似。

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 100010
#define mod 1000000007
using namespace std; ll power(ll a,ll b)
{
ll ans=;
while(b)
{
if(b&)
{
ans=(ans*a)%mod;
b--;
}
b/=;
a=(a*a)%mod;
}
return ans;
}
ll c;
ll sum(ll a,ll k)
{
if(k == ) return a;
c=sum(a,k>>);
ll ans=(c+c*power(a,(k>>)))%mod; //每次 计算出两项
if(k&) ans=(ans+power(a,k))%mod; //是奇数的话要加上最后那一项
return ans;
}
int main()
{
//freopen("a.txt","r",stdin);
ll n;
scanf("%lld",&n);
//printf("%lld\n",(power(3,20)-1)/2%mod);
printf("%lld\n",((sum(,n)%mod))+);
return ;
}

最新文章

  1. ubuntu12.04samba服务器配置,亲测可用(转)
  2. 返璞归真vc++之感言
  3. JAVA TCP/IP Socket通信机制以及应用
  4. 一段C++代码想到的问题
  5. iOS音频处理
  6. [Protractor] Test Simple Binding With Protractor
  7. Javascript进阶篇——(DOM—节点---获取浏览器窗口可视区域大小+获取网页尺寸)—笔记整理
  8. 直插式精巧I/O模块:WIZ812MJ数据手册V1.1
  9. mysql 二级索引
  10. Canvas scale- 缩放
  11. jQ的一些常识
  12. fiddler 按条件过滤
  13. [转]Maven与nexus关系
  14. RABC --权限控制解读
  15. 可变数组(PLSQL)
  16. Shell - 简明Shell入门10 - 管道(Pipe)
  17. 机器翻译评价指标之BLEU详细计算过程
  18. 算法笔记_204:第四届蓝桥杯软件类决赛真题(Java语言C组)
  19. cf463d
  20. Top 5 SSH Clients for Windows (Alternatives of PuTTY)

热门文章

  1. Java_JDBC连接数据库
  2. Java多线程——线程的创建方式
  3. MVC中使用MVCPager简单分页
  4. idea 部署struts所遇到的问题\
  5. java synchronized(object/this)的 区别
  6. web页面打印--铺满A4
  7. Javascript IE 内存释放
  8. ALTER TRIGGER - 修改一个触发器的定义
  9. webpack的详细介绍和使用
  10. vue解决IOS10低版本白屏问题