题目大意:

看一下样例就明白了

基本思路:

题目中明确提到k为一个周期,稍作思考,把k项看作一项,然后发现这是个等比数列,q=(b/a)^k,

然后重点就是怎样处理等比数列求和表达式中的除法,这个时候就要用到逆元,因为1e9+9是素数,

所以直接用费马小定理求逆元就好了,说到这个,可以学一下卢卡斯定理,这个比较有用处,然后需要注意

两点:

1)快速幂a的每次乘方里面都要%mod,这个到底是为什么我也不知道,难道不是只在外面取模一次就好了吗

2)最后判断条件是t2是否等于0,而不是a是否等于b,难道不是等价的吗?为什么会这样?

代码如下:

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<iostream>
  5. #include<string>
  6. #include<algorithm>
  7. #include<queue>
  8. #include<vector>
  9.  
  10. using namespace std;
  11.  
  12. typedef long long ll;
  13. typedef long long LL;
  14. typedef pair<int,int> pii;
  15. const int inf = 0x3f3f3f3f;
  16. const int maxn = 100000+10;
  17. const ll mod = 1e9+9;
  18.  
  19. char s[maxn];
  20. ll qpow(int a,int b){
  21. ll res=1;
  22. while(b){
  23. if(b&1) res=(res*a)%mod;
  24. a=(a%mod*a%mod)%mod;
  25. b>>=1;
  26. }
  27. return res%mod;
  28. }
  29. int main(){
  30. int n,a,b,k;
  31. scanf("%d%d%d%d",&n,&a,&b,&k);
  32. scanf("%s", s);
  33. int len = strlen(s);
  34. LL ans = 0;
  35. LL tmp, cir = 0;
  36. for(int i = 0; i < len; i++){
  37. tmp = qpow(a,n-i) * qpow(b,i) % mod;
  38. if(s[i] == '+') {
  39. cir += tmp;
  40. cir %= mod;
  41. }
  42. else {
  43. cir -= tmp;
  44. if(cir < 0) cir += mod;
  45. cir %= mod;
  46. }
  47. }
  48. int time = (n+1) / len;
  49. int lf = n+1 - len*time;
  50. int be = len*time;
  51. for(int i = 0; be <= n; i++, be++){
  52. tmp = qpow(a,n-be) * qpow(b,be) % mod;
  53. if(s[i] == '+') {
  54. ans += tmp;
  55. ans %= mod;
  56. }
  57. else {
  58. ans -= tmp;
  59. if(ans < 0) ans += mod;
  60. ans %= mod;
  61. }
  62. }
  63. ll t1=(qpow(a,len*time)-qpow(b,len*time))%mod;
  64. if(t1<0) t1+=mod;
  65. ll t2=(qpow(a,k*time)-(qpow(a,k*(time-1))*qpow(b,k)%mod))%mod;
  66. if(t2<0) t2+=mod;
  67. ll t3=t1*qpow(t2,mod-2)%mod;
  68. if(t2==0){
  69. printf("%I64d\n",(ans+cir*time%mod)%mod);
  70. }else{
  71. printf("%I64d\n",(ans+cir*t3%mod)%mod);
  72. }
  73. return 0;
  74. }

  

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. php count_chars()函数 语法

    php count_chars()函数 语法 作用:返回一个字符串,包含所有在字符串中使用过的不同字符.直线电机选型 语法:count_chars(string,mode) 参数: 参数 描述 str ...

  2. ORACLE错误:ORA-28001: the password has expired解决方法

    Oracle提示错误消息ORA-28001: the password has expired,是由于Oracle11G的新特性所致, Oracle11G创建用户时缺省密码过期限制是180天(即6个月 ...

  3. Solr JAVA客户端SolrJ的使用

    一.Solrj简介 SolrJ是操作Solr的JAVA客户端,它提供了增加.修改.删除.查询Solr索引的JAVA接口.SolrJ针对 Solr提供了Rest 的HTTP接口进行了封装, SolrJ底 ...

  4. CSS3订单提交按钮Loading代码

    <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"& ...

  5. 修改mac下homebrew的源 加快下载速度

    把源改为清华的镜像 # HOMEBREW_BOTTLE_DOMAIN就是目标源 修改这个路径就可以里 echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirro ...

  6. php 中 http_build_query用法

    http_build_query (PHP 5) http_build_query -- 生成 url-encoded 之后的请求字符串描述string http_build_query ( arra ...

  7. MSSQL sql常用判断语句

    .判断数据库是否存在 if exists (select * from sys.databases where name = '数据库名')    drop database [数据库名]  2 判断 ...

  8. CET-6 分频周计划生词筛选(Week 3)

    点我阅读 Week 3 2016.09.11 p113 manipulate + propel p114 expedition + deficit p115 all p116 envisage p11 ...

  9. 队列问题非STL解决方案

    队列问题非STL解决方案 常年使用STL解决队列问题,以至于严重生疏队列的根本原理... 直到今日 被老师被迫 使用算法原理解决问题,方才意识到我对队列一窍不通... ...直到 经过一系列的坑蒙拐骗 ...

  10. Recurrent Neural Network(1):Architecture

    Recurrent Neural Network是在单个神经元上,除了输入与输出外,添加了一条Recurrent回路.也就是说,节点当前的状态将会影响其未来的状态.下式可以表征此关系: st= f(s ...