#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. SQL Server 编程入门

    一.T—SQL 的组成 1.DML(数据操作语言 Data Manipulation Language) 查询.插入.删除和修改数据库中的数据.SELECT.INSERT.UPDATE.DELETE ...

  2. AJPFX总结Socket的低层次Java网络编程

    Socket的低层次Java网络编程 1 Socket通讯 网络上的两个程序通过一个双向的通讯连接实现数据的交换,这个双向链路的一端称为一个Socket.Socket通常用来实现客户方和服务方的连接. ...

  3. poj2718 Smallest Difference

    思路: 暴力乱搞. 实现: #include <iostream> #include <cstdio> #include <sstream> #include &l ...

  4. 公众号如何获取已关注用户的unionid的问题

    避免误导,先加一句:首先,得公众号绑定开放平台 这个问题困扰了我一早上,我尝试了很多次获取unionid都失败. 微信的开发文档上有说: 关于特殊场景下的静默授权 1.上面已经提到,对于以snsapi ...

  5. mysql中判断条件

    if / case when 判断 SELECT CASE 1 WHEN 1 THEN "one" WHEN 2 THEN "two" ELSE "m ...

  6. PHP运算符考察点

    PHP运算符优先级 运算符优先级指定了两个表达式绑定得有多"紧密".例如,表达式 1 + 5 * 3 的结果是 16 而不是 18 是因为乘号(*)的优先级比加号(+)高.必要时可 ...

  7. CREATE SCHEMA - 定义一个新的模式

    SYNOPSIS CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ] CREATE SCHEM ...

  8. Oracle使用plsql连不上本地数据库,cmd中使用sqlplus连的上的可能解决方案

    1.无监听程序 原因: 最有可能是oracle监听的服务没有启动起来. 2.ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 原因: 1.服务没有配置127.0.0.1或监听程序没 ...

  9. print keys %map_function 输出 散列的值: OK_funcsplit_funcpackage_VAR

    my %map_function = (     88     "OK_func" => "open_statement",     89     &qu ...

  10. Spring Boot . 2 -- 用Spring Boot 创建一个Java Web 应用

    通过 start.spring.io 创建工程 通过 IDEA 创建工程