#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 ;
}

快速幂取模模板 && 51nod 1013 3的幂的和的更多相关文章

  1. A - Alice and the List of Presents (排列组合+快速幂取模)

    https://codeforces.com/contest/1236/problem/B Alice got many presents these days. So she decided to ...

  2. POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩阵快速幂取模)

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20309   Accepted:  ...

  3. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  4. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  5. UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!

    题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...

  6. POJ3641-Pseudoprime numbers(快速幂取模)

    题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...

  7. 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模

    题目地址:http://ac.jobdu.com/problem.php?pid=1085 题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k). ...

  8. HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模

    小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  9. CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模

    很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...

随机推荐

  1. 在spring boot 中使用itext和itextrender生成pdf文件

    转载请注明出处 https://www.cnblogs.com/majianming/p/9539376.html 项目中需要对订单生成pdf文件,在第一版本其实已经有了比较满意的pdf文档,但是还是 ...

  2. java获取公网ip以及物理地址和代理商

    package ipconfig; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStr ...

  3. 学习笔记 第十二章 CSS3+HTML5网页排版

    第12章   CSS3+HTML5网页排版 [学习重点] 正确使用HTML5结构标签 正确使用HTML5语义元素 能够设计符合标准的网页结构 12.1  使用结构标签 在制作网页时,不仅需要使用< ...

  4. oracle 时间格式转化以及计算

    --A表中的日期字段 create_date   例如:2017-08-05  转化为2017年8月5日   oracle 在这里的双引号会忽略 select to_char(to_date(tt.c ...

  5. 阿里云虚拟主机的域名添加https的方法

    第一步:购买CDN套餐,阿里云虚拟主机目前是不支持https的,不过可以通过阿里云的CDN服务来跳转一下实现部署https 静态HTTPS请求数根据你的网站访问量来选择 第二步:申请SSL证书服务,有 ...

  6. 使用Jenkins进行android项目的自动构建(5)

    之前在项目中引入的单元测试使用的是JUnit,可以在构建前进行测试,这里在介绍一下使用Instrumentation 进行单元测试.使用Instrumentation进行测试,比之前多一些步骤,需要把 ...

  7. 黑马程序员----java基础:异常

    dff ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 经常写程序的人对try...catch...finally语句肯定是不陌生的了.但是好多 ...

  8. [转]1小时内打造你自己的PHP MVC框架

    简介 MVC框架在现在的开发中相当流行,不论你使用的是JAVA,C#,PHP或者IOS,你肯定都会选择一款框架.虽然不能保证100%的开发语言都会使用框架,但是在PHP社区当中拥有*多数量的MVC框架 ...

  9. informix数据库的日志

    看到一句sql,没见过: CREATE TABLE aaad ( chianm ), course ), score INTEGER ) IN adbs EXTENT SIZE LOCK MODE R ...

  10. iTOP-4412开发板-实战教程-ssh服务器移植到arm开发板

    本文转自迅为开发板:http://www.topeetboard.com 在前面实战教程中,移植了“串口文件传输工具”,整个移植过程是比较简单的,而且我 们没有做任何协议方面的了解,只是“配置”+“编 ...