[Codeforces 963 A. Alternating Sum](http://codeforces.com/problemset/problem/963/A)
题目大意:给出一组长度为n+1且元素为1或者-1的数组S(0~n),数组每k个元素为一周期,保证n+1可以被k整除。给a和b,求![](https://images2018.cnblogs.com/blog/1330878/201805/1330878-20180526212011017-1327546933.png)对1e9+9取模的结果
思路:容易想到,每个周期的∑组成的数列成等比,公比q=(b/a)^k,因此可以用等比数列公式求和。为了保证时间复杂度,需要用到快速幂运算;为了防止中间过程值溢出,需要多处取模,其中用费马小定理求逆元;
代码:
```C++
#include
#include
#include
using namespace std;
typedef long long ll;
const int mod=1e9+9;
ll qpow(ll x,ll n,ll m)
{
ll res=1;
while (n>0)
{
if (n&1)
res=res*x%m;
n>>=1;
x=x*x%m;
}
return res;
}
ll inv(ll x,ll m)
{
return qpow(x,m-2,m);
}
int main()
{
int n,a,b,k,i;
cin>>n>>a>>b>>k;
cin.get();
ll ft=0,q,ans;
for (i=0;i

Codeforces 963 A. Alternating Sum(快速幂,逆元)的更多相关文章

  1. HDU 5685 Problem A | 快速幂+逆元

    Problem A Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. 2016 Asia Jakarta Regional Contest J - Super Sum UVALive - 7720 【快速幂+逆元】

    J-Super Sum 题目大意就是给定N个三元组<a,b,c>求Σ(a1^k1*a2^k2*...*ai^ki*..an^kn)(bi<=ki<=ci) 唉.其实题目本身不难 ...

  3. Open judge C16H:Magical Balls 快速幂+逆元

    C16H:Magical Balls 总时间限制:  1000ms 内存限制:  262144kB 描述 Wenwen has a magical ball. When put on an infin ...

  4. codeforces magic five --快速幂模

    题目链接:http://codeforces.com/contest/327/problem/C 首先先算出一个周期里面的值,保存在ans里面,就是平常的快速幂模m做法. 然后要计算一个公式,比如有k ...

  5. 【牛客小白月赛6】F 发电 - 树状数组&快速幂&逆元

    题目地址:https://www.nowcoder.com/acm/contest/136/F 树状数组.快速幂.逆元的模板运用: #include<iostream> #include& ...

  6. 【牛客小白月赛6】 J 洋灰三角 - 快速幂&逆元&数学

    题目地址:https://www.nowcoder.com/acm/contest/136/J 解法一: 推数学公式求前n项和: 当k=1时,即为等差数列,Sn = n+pn(n−1)/2 当k≠1时 ...

  7. POJ 1845:Sumdiv 快速幂+逆元

    Sumdiv Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16466   Accepted: 4101 Descripti ...

  8. 【codeforces 623E】dp+FFT+快速幂

    题目大意:用$[1,2^k-1]$之间的证书构造一个长度为$n$的序列$a_i$,令$b_i=a_1\ or\ a_2\ or\ ...\ or a_i$,问使得b序列严格递增的方案数,答案对$10^ ...

  9. hdu5698瞬间移动(杨辉三角+快速幂+逆元)

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

随机推荐

  1. vue.js对列表进行编辑未保存随时变更

    1.不要建立在同一vm对象下 2.使用深拷贝$.extend(true, vm.model, obj); 3.开新标签页

  2. Web应用性能分析工具—HAR文件

    Web应用性能分析工具—HAR文件 来源 https://raynorli.com/2018/06/11/web-performance-analysis-har-file/ 客户经常有的一个问题就是 ...

  3. whistle学习(二)之启动、停止、重启、更新whistle等命令

    新版本的whistle支持三种等价命令whistle,w2,wproxy 启动whistle w2 start 启动时指定端口 w2 start -p (// 不设置端口默认使用8899) 默认端口为 ...

  4. visual studio2015 搭建pro*c开发编译环境

    关于pro*c是什么,这里不做介绍,主要说明如何在vs2015里面开发pro*c程序,并编译exe执行文件 一.vs2015环境配置 1.新建一个空的vc++项目,如下图 2.右击项目属性,添加相关的 ...

  5. opencv 模板匹配, 已解决模板过大程序不工作的bug

    #include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv ...

  6. C# 移除数组中重复项

    方法一: static void Main(string[] args) { //看到数组的第一反应应该是排序 ,,,,,,,}; //去掉数组中重复的项 //先排序 arrayAsc(array); ...

  7. 1 c#传递表变量去存储数据的例子

    1 c# 代码 using (SqlConnection con = GetEditorConnection()) { con.Open(); using (SqlCommand command = ...

  8. frp 路由穿透(github开源穿透软件)

    server配置(windows):下载: https://github.com/fatedier/frp/releases [common] # 服务器端端口 bind_port = # 客户端连接 ...

  9. Apache Shiro漏洞复现

    利用burp dns进行检测,脚本如下: import sys import uuid import base64 import subprocess from Crypto.Cipher impor ...

  10. 纯净CentOS搭建harbor镜像私仓

    物理宿主机IP:  192.168.1.4 在官网下载 CentOS-7-x86_64-DVD-1810 用Hyper-v建立一代虚机,安装时遇分辨率问题无法继续,需要在选择启动界面按TAB键以编辑启 ...