Codeforces 963E Alternating Sum 等比数列+逆元
题目大意:
看一下样例就明白了
基本思路:
题目中明确提到k为一个周期,稍作思考,把k项看作一项,然后发现这是个等比数列,q=(b/a)^k,
然后重点就是怎样处理等比数列求和表达式中的除法,这个时候就要用到逆元,因为1e9+9是素数,
所以直接用费马小定理求逆元就好了,说到这个,可以学一下卢卡斯定理,这个比较有用处,然后需要注意
两点:
1)快速幂a的每次乘方里面都要%mod,这个到底是为什么我也不知道,难道不是只在外面取模一次就好了吗
2)最后判断条件是t2是否等于0,而不是a是否等于b,难道不是等价的吗?为什么会这样?
代码如下:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<vector> using namespace std; typedef long long ll;
typedef long long LL;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const int maxn = 100000+10;
const ll mod = 1e9+9; char s[maxn];
ll qpow(int a,int b){
ll res=1;
while(b){
if(b&1) res=(res*a)%mod;
a=(a%mod*a%mod)%mod;
b>>=1;
}
return res%mod;
}
int main(){
int n,a,b,k;
scanf("%d%d%d%d",&n,&a,&b,&k);
scanf("%s", s);
int len = strlen(s);
LL ans = 0;
LL tmp, cir = 0;
for(int i = 0; i < len; i++){
tmp = qpow(a,n-i) * qpow(b,i) % mod;
if(s[i] == '+') {
cir += tmp;
cir %= mod;
}
else {
cir -= tmp;
if(cir < 0) cir += mod;
cir %= mod;
}
}
int time = (n+1) / len;
int lf = n+1 - len*time;
int be = len*time;
for(int i = 0; be <= n; i++, be++){
tmp = qpow(a,n-be) * qpow(b,be) % mod;
if(s[i] == '+') {
ans += tmp;
ans %= mod;
}
else {
ans -= tmp;
if(ans < 0) ans += mod;
ans %= mod;
}
}
ll t1=(qpow(a,len*time)-qpow(b,len*time))%mod;
if(t1<0) t1+=mod;
ll t2=(qpow(a,k*time)-(qpow(a,k*(time-1))*qpow(b,k)%mod))%mod;
if(t2<0) t2+=mod;
ll t3=t1*qpow(t2,mod-2)%mod;
if(t2==0){
printf("%I64d\n",(ans+cir*time%mod)%mod);
}else{
printf("%I64d\n",(ans+cir*t3%mod)%mod);
}
return 0;
}
Codeforces 963E Alternating Sum 等比数列+逆元的更多相关文章
- codeforces 963A Alternating Sum
codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...
- Codeforces 964C Alternating Sum
Alternating Sum 题意很简单 就是对一个数列求和. 题解:如果不考虑符号 每一项都是前一项的 (b/a)倍, 然后考虑到符号的话, 符号k次一循环, 那么 下一个同一符号的位置 就是 这 ...
- Codeforces 963A Alternating Sum(等比数列求和+逆元+快速幂)
题目链接:http://codeforces.com/problemset/problem/963/A 题目大意:就是给了你n,a,b和一段长度为k的只有'+'和‘-’字符串,保证n+1被k整除,让你 ...
- Codeforces 963A Alternating Sum ( 思维 && 数论 )
题意 : 题目链接 分析 : Tutorial 讲的很清楚 至于为什么这样去考虑 算是一个经验问题吧 如果一个问题要你给出模意义下的答案 就多考虑一下答案是要用逆元构造出来 也就说明有除法的存在 那么 ...
- Codeforces 963 A. Alternating Sum(快速幂,逆元)
Codeforces 963 A. Alternating Sum 题目大意:给出一组长度为n+1且元素为1或者-1的数组S(0~n),数组每k个元素为一周期,保证n+1可以被k整除.给a和b,求对1 ...
- CF963A Alternating Sum
思路:利用周期性转化为等比数列求和. 注意当a != b的时候 bk * inv(ak) % (109 + 9)依然有可能等于1,不知道为什么. 实现: #include <bits/stdc+ ...
- Codeforces 396B On Sum of Fractions 数论
题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- [codeforces round#475 div2 ][C Alternating Sum ]
http://codeforces.com/contest/964/problem/C 题目大意:给出一个等比序列求和并且mod 1e9+9. 题目分析:等比数列的前n项和公式通过等公比错位相减法可以 ...
随机推荐
- 做网站用php还是python
单纯说做网站,显然是php更适合,php是专为web而生,而Python只是可以做web.php也比python更简单,更容易学,对于新手更友好. 从权威技术网站w3techs.com2017年7月2 ...
- Fckeditor实现WORD粘贴图片自动上传
在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper. 通过知乎提供的思路找到粘贴的原理,通过TheViper找 ...
- python保存selenium的cookies写入和读出
def write_cookie(self, cookie): try: with open("cookies%s" % self.uid, "wb+") as ...
- (转)linux 命令访问url: curl http://www.baidu.com/index.html
转:https://blog.csdn.net/michael1112/article/details/79119424 1.elinks - lynx-like替代角色模式WWW的浏览器 例如: e ...
- Docker 里面新建mysql 容器
1.获取MySQL镜像, a.直接从docker hub 下载docker镜像 docker pull +镜像名称 b.从别的项目上把镜像export出来 dockr load i + 镜像的TAR ...
- DB-MDM:MDM/主数据管理 百科
ylbtech-DB-MDM:MDM/主数据管理 百科 主数据管理(MDM Master Data Management)描述了一组规程.技术和解决方案,这些规程.技术和解决方案用于为所有利益相关方( ...
- leetcode 125. 验证回文串(python)
给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...
- (转载)Java 8 认识 HashMap
原链接:传送门 摘要 HashMap是Java程序员使用频率最高的用于映射(键值对)处理的数据类型.随着JDK(Java Developmet Kit)版本的更新,JDK1.8对HashMap底层的实 ...
- PHP开发环境搭建及开发工具
PHP服务器组件非常多有WampServer.XAMPP.AppServ.phpStudy.phpnow等. 菜鸟教程推荐: WampServer,这也是目前window平台上使用最广泛的,操作也非常 ...
- 线程休眠只会用 Thread.sleep?来,教你新姿势!
线程休眠是 Java 开发经常会用到的一个手段,就是让当前线程睡一会儿,睡醒之后再继续运行. 咱大多数程序员,多线程虽然学得不好,但线程休眠,无人不知,无人不晓,也都会用,不就是用 Thread.sl ...