#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. Java_JDBC连接数据库

    package com.accp.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Pre ...

  2. AJPFX关于构造器的总结

    构造器        构造器定义        构造器作用        构造器特点        构造器修饰符        默认构造器        构造器重载        构造器和一般函数的区 ...

  3. Vue.js学习笔记--1.基础HTML和JS属性的使用

    整理自官网教程 -- https://cn.vuejs.org/ 1. 在HTML文件底部引入Vue <script src="https://cdn.jsdelivr.net/npm ...

  4. 读取Java文件到byte数组的三种方式

    package zs; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io ...

  5. 微擎we7模块和模板安装方法

    2017年06月08日 09:26:55 源码学习分享 阅读数:15643 标签: we7 更多 个人分类: 微擎we7   版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...

  6. C# 处理年月日提取时间

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. 扩展 IHttpModule

    上篇提到请求进入到System.Web后,创建完HttpApplication对象后会执行一堆的管道事件,然后可以通过HttpModule来对其进行扩展,那么这篇文章就来介绍下如何定义我们自己的mod ...

  8. zabbix4.2学习笔记--新建用户组和用户

    新建用户组 zabbix中管理机器是以用户组划分,这里新建一个只读用户群组和只读用户 新建用户组 点击 管理-用户组-创建用户群组,如下图 点击创建之后,有三列设置,分别是用户群组.权限和标签过滤器, ...

  9. [JOYOI] 自然数拆分Lunatic版

    题目背景 话说小小鱼看了P1171(自然数拆分)之后感觉异常不爽,于是异常邪恶地将题目加强. 题目描述 输入自然数n,然后将其拆分成由若干数相加的形式,参与加法运算的数可以重复. 输入格式 输入只有一 ...

  10. 【spring】jar包详解与模块依赖关系

    以spring3.X为例 jar包详解 1. spring-core.jar:包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心: 2. spri ...