题目大意:

看一下样例就明白了

基本思路:

题目中明确提到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 等比数列+逆元的更多相关文章

  1. codeforces 963A Alternating Sum

    codeforces 963A Alternating Sum 题解 计算前 \(k\) 项的和,每 \(k\) 项的和是一个长度为 \((n+1)/k\) ,公比为 \((a^{-1}b)^k\) ...

  2. Codeforces 964C Alternating Sum

    Alternating Sum 题意很简单 就是对一个数列求和. 题解:如果不考虑符号 每一项都是前一项的 (b/a)倍, 然后考虑到符号的话, 符号k次一循环, 那么 下一个同一符号的位置 就是 这 ...

  3. Codeforces 963A Alternating Sum(等比数列求和+逆元+快速幂)

    题目链接:http://codeforces.com/problemset/problem/963/A 题目大意:就是给了你n,a,b和一段长度为k的只有'+'和‘-’字符串,保证n+1被k整除,让你 ...

  4. Codeforces 963A Alternating Sum ( 思维 && 数论 )

    题意 : 题目链接 分析 : Tutorial 讲的很清楚 至于为什么这样去考虑 算是一个经验问题吧 如果一个问题要你给出模意义下的答案 就多考虑一下答案是要用逆元构造出来 也就说明有除法的存在 那么 ...

  5. Codeforces 963 A. Alternating Sum(快速幂,逆元)

    Codeforces 963 A. Alternating Sum 题目大意:给出一组长度为n+1且元素为1或者-1的数组S(0~n),数组每k个元素为一周期,保证n+1可以被k整除.给a和b,求对1 ...

  6. CF963A Alternating Sum

    思路:利用周期性转化为等比数列求和. 注意当a != b的时候 bk * inv(ak) % (109 + 9)依然有可能等于1,不知道为什么. 实现: #include <bits/stdc+ ...

  7. Codeforces 396B On Sum of Fractions 数论

    题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...

  8. codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...

  9. [codeforces round#475 div2 ][C Alternating Sum ]

    http://codeforces.com/contest/964/problem/C 题目大意:给出一个等比序列求和并且mod 1e9+9. 题目分析:等比数列的前n项和公式通过等公比错位相减法可以 ...

随机推荐

  1. Airtest断言方法

    1,第一种断言方式:验证UI界面 a.存在 b.不存在 2,断言第二种方式:验证数值 assert_equal:断言相等 assert_not_equal:断言不等 3,我发现Airtest一个bug ...

  2. (3)狄泰软件学院C++课程学习剖析一

    深度剖析C++第一部分 1.类是一种模型,这种模型可以创建出一个对应的实体.有了类不一定有对应的实体,但是一个实体必定属于某一个类. 2.类用于抽象的描述 一类事物所持有的属性和行为:对象是具体的事物 ...

  3. 201903-2 CCF 二十四点

    题面: 考场写的30分== #include<bits/stdc++.h> using namespace std; stack<int>st; stack<char&g ...

  4. 基于MyBatis实现Dao理论

    基于MyBatis实现Dao理论 推荐使用xml提供sql 实现接口推荐使用Mapper自动实现DAO接口,让我们更关注sql书写本身

  5. 用redis实现队列实例

    queue input #coding = utf-8__autor__ = 'litao'import redisimport timepool = redis.ConnectionPool(hos ...

  6. 线段树2(P3373)

    传送 感谢洛谷题解让我理清了这一撮标记 这里多了一个乘法操作,乘法的优先级高于加法.我们来思考一下有关标记的问题. 首先由两种操作,可以想到要有两个标记,一个标记乘法(mul[k]),一个标记加法(a ...

  7. Flask-SQLALchemy动态的filter_by和filter

    1.filter_by filter_by用于查询简单的列名,不支持比较运算符. filters = {'name': 'fengyao', 'age': 26} User.query.filter_ ...

  8. springcloud核心组件Eureka、Ribbon、Feign、Hystrix、Zuul

    各个组件角色扮演:    Eureka:各个服务启动时,Eureka Client都会将服务注册到Eureka Server,并且Eureka Client还可以反过来从Eureka Server拉取 ...

  9. IntelliJ IDEA 2018 2.X破解

    一 下载idea 官网 :https://www.jetbrains.com/idea/ 二 下载破解包 IntelliJ IDEA 2018.1.X--2.X(2.6可用,图就不改了) 链接: ht ...

  10. Linear Regression and Gradient Descent

    随着所学算法的增多,加之使用次数的增多,不时对之前所学的算法有新的理解.这篇博文是在2018年4月17日再次编辑,将之前的3篇博文合并为一篇. 1.Problem and Loss Function ...