Alternating Sum

题意很简单 就是对一个数列求和。

题解:如果不考虑符号 每一项都是前一项的 (b/a)倍, 然后考虑到符号的话, 符号k次一循环, 那么 下一个同一符号的位置 就是 这一个位置的 (b/a)^k倍了, 然后我们可以发现这个是一个等比数列, 最后我们对等比数列求和就好了。

注意的就是 (b/a)^k % mod == 1的情况,我们可以将前K个数总和在一起, 在一起求等比的和就好了。

我们可以将公式 cir*(1-q^time) / (1 - q) 其中q = (b/a)^k 转化成 cir * (a1^(time*k) - b^(time*k)) / (a1^(time*k) - b^k * a ^((t-1)*k)) 然后因为要进行mod操作 所以 再转换成 cir * (a1^(time*k) - b^(time*k)) *inv( (a1^(time*k) - b^k * a ^((t-1)*k))) 就好了。

代码:

 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
const int N = 1e5+;
int n, a, b, k;
char str[N];
LL qpow(int a, int b){
LL ret = ;
while(b){
if(b&) ret = (ret*a)%mod;
a = (a%mod*a%mod) % mod;
b >>= ;
}
return ret%mod;
}
int main(){
scanf("%d%d%d%d",&n,&a,&b,&k);
scanf("%s", str);
int len = strlen(str);
LL ans = ;
LL tmp, cir = ;
for(int i = ; i < len; i++){
tmp = qpow(a,n-i) * qpow(b,i) % mod;
if(str[i] == '+') {
cir += tmp;
cir %= mod;
}
else {
cir -= tmp;
if(cir < ) cir += mod;
cir %= mod;
}
}
int time = (n+) / len;
int lf = n+ - len*time;
int be = len*time;
for(int i = ; be <= n; i++, be++){
tmp = qpow(a,n-be) * qpow(b,be) % mod;
if(str[i] == '+') {
ans += tmp;
ans %= mod;
}
else {
ans -= tmp;
if(ans < ) ans += mod;
ans %= mod;
}
}
LL t1 = (qpow(a,len*time) - qpow(b,len*time))%mod;
if(t1 < ) t1 += mod;
LL t2 = (qpow(a,len*time) % mod - qpow(b,len)*qpow(a,(time-)*len)%mod) %mod;
if(t2 < ) t2 += mod;
LL t3 = t1 *(qpow(t2,mod-))% mod;
if(t2!=){
ans = (ans + cir * t3 % mod)%mod;
}
else {
ans = (ans+cir*time%mod)%mod;
}
printf("%I64d", ans);
return ;
}
/*
8 2 3 2
++
*/

Codeforces 964C Alternating Sum的更多相关文章

  1. codeforces 963A Alternating Sum

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

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

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

  3. CF 964C Alternating Sum

    给定两正整数 $a, b$ .给定序列 $s_0, s_1, \dots, s_n,s_i$ 等于 $1$ 或 $-1$,并且已知 $s$ 是周期为 $k$ 的序列并且 $k\mid (n+1)$,输 ...

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

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

  5. Codeforces 963E Alternating Sum 等比数列+逆元

    题目大意: 看一下样例就明白了 基本思路: 题目中明确提到k为一个周期,稍作思考,把k项看作一项,然后发现这是个等比数列,q=(b/a)^k, 然后重点就是怎样处理等比数列求和表达式中的除法,这个时候 ...

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

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

  7. Codeforces 396B On Sum of Fractions 数论

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

  8. CF963A Alternating Sum

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

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

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

随机推荐

  1. Linux EXT2 文件系统

    磁盘是用来储文件的,但是必须先把磁盘格式化为某种格式的文件系统,才能存储文件.文件系统的目的就是组织和管理磁盘中的文件.在 Linux 系统中,最长见的是 ext2 系列的文件系统.其早期版本为 ex ...

  2. ubuntu搭建环境

    1.终端输入 sudo apt- add-apt-repository ppa:ondrej/php  sudo add-apt-repository ppa:ondrej/php  sudo apt ...

  3. c# 控制台console进度条

    1 说明 笔者大多数的开发在 Linux 下,多处用到进度条的场景,但又无需用到图形化界面,所以就想着弄个 console 下的进度条显示. 2 步骤 清行显示 //清行处理操作 int curren ...

  4. 分布式ID系列之为什么需要分布式ID以及生成分布式ID的业务需求

    为什么需要分布式id生成系统 在复杂分布式系统中,往往需要对大量的数据和消息进行唯一标识.如在美团点评的金融.支付.餐饮.酒店.猫眼电影等产品的系统中,数据日渐增长,对数据分库分表后需要有一个唯一ID ...

  5. oracle-11g2下载安装笔记

    一.下载链接地址 http://download.oracle.com/otn/nt/oracle11g/112010/win64_11gR2_database_1of2.zip http://dow ...

  6. [NUnit] discover test finished: 0 found issue

    %Temp%\VisualStudioTestExplorerExtensions & restart visual studio

  7. JavaScript数据结构——树的实现

    在计算机科学中,树是一种十分重要的数据结构.树被描述为一种分层数据抽象模型,常用来描述数据间的层级关系和组织结构.树也是一种非顺序的数据结构.下图展示了树的定义: 在介绍如何用JavaScript实现 ...

  8. win10 我的电脑下面的六个文件夹的隐藏

      第一步   第二步     第三步 修改注册表,要隐藏那个文件夹,ThisPCPolicy 改为 "Hide" 修改我的文档的注册表值,使我的文档文件夹隐藏     <w ...

  9. javascript中的浅拷贝和深拷贝(拷贝引用和拷贝实例)

    作者:千锋教育链接:https://www.zhihu.com/question/23031215/answer/326129003来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  10. Java并发编程实战笔记—— 并发编程3

    1.实例封闭 class personset{ private final Set<Person> myset = new HashSet<Person>(); public ...