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 count_chars()函数 语法
php count_chars()函数 语法 作用:返回一个字符串,包含所有在字符串中使用过的不同字符.直线电机选型 语法:count_chars(string,mode) 参数: 参数 描述 str ...
- ORACLE错误:ORA-28001: the password has expired解决方法
Oracle提示错误消息ORA-28001: the password has expired,是由于Oracle11G的新特性所致, Oracle11G创建用户时缺省密码过期限制是180天(即6个月 ...
- Solr JAVA客户端SolrJ的使用
一.Solrj简介 SolrJ是操作Solr的JAVA客户端,它提供了增加.修改.删除.查询Solr索引的JAVA接口.SolrJ针对 Solr提供了Rest 的HTTP接口进行了封装, SolrJ底 ...
- CSS3订单提交按钮Loading代码
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...
- 修改mac下homebrew的源 加快下载速度
把源改为清华的镜像 # HOMEBREW_BOTTLE_DOMAIN就是目标源 修改这个路径就可以里 echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirro ...
- php 中 http_build_query用法
http_build_query (PHP 5) http_build_query -- 生成 url-encoded 之后的请求字符串描述string http_build_query ( arra ...
- MSSQL sql常用判断语句
.判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名') drop database [数据库名] 2 判断 ...
- CET-6 分频周计划生词筛选(Week 3)
点我阅读 Week 3 2016.09.11 p113 manipulate + propel p114 expedition + deficit p115 all p116 envisage p11 ...
- 队列问题非STL解决方案
队列问题非STL解决方案 常年使用STL解决队列问题,以至于严重生疏队列的根本原理... 直到今日 被老师被迫 使用算法原理解决问题,方才意识到我对队列一窍不通... ...直到 经过一系列的坑蒙拐骗 ...
- Recurrent Neural Network(1):Architecture
Recurrent Neural Network是在单个神经元上,除了输入与输出外,添加了一条Recurrent回路.也就是说,节点当前的状态将会影响其未来的状态.下式可以表征此关系: st= f(s ...