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. 3PHP如何用PDO的连接方式方式导出mysql数据

    首先连接mysql,具体看上一篇 接下来在try{}中加入以下代码 $query="select * from 你的数据表名称"          //$query的内容给个SQL ...

  2. Eclipse高级操作 远程调试

    Eclipse高级操作 远程调试 JPDA是SUN JDK自带的远程调试机制.它提供了一套标准的调试接口,可以从虚拟机一级允许外界用特定协议探测虚拟机内部的运作细节.只要你装了JDK1.2以上的SUN ...

  3. Spring Boot + Security + JWT 实现Token验证+多Provider——登录系统

    首先呢就是需求: 1.账号.密码进行第一次登录,获得token,之后的每次请求都在请求头里加上这个token就不用带账号.密码或是session了. 2.用户有两种类型,具体表现在数据库中存用户信息时 ...

  4. LeetCode——264. Ugly Number II

    题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...

  5. [android视频教程] 传智播客android开发视频教程

    本套视频共有67集,是传智播客3G-Android就业班前8天的的课程量.本套视频教程是黎活明老师在2011年底对传智播客原来的Android核心基础课程精心重新录制的,比早期的Android课程内容 ...

  6. 在一个jsp页面内实现简单计算器

    首先创建一个calculate.jsp 这是用Javascript代码来验证,代码如下: <script type="text/javascript"> functio ...

  7. (四十一)c#Winform自定义控件-进度条

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. idea 2019安装完(打不开&&启动不了)问题解决(最全解决方法)

    今天从网盘把idea下载下来后一路安装,准备 设置的时候不管怎么打开 他都无动于衷没办法,卸了安,安了卸,反复折腾了 好几遍 它都无动于衷.于是开始在百度上找答案看了 好几个 方法一遍一遍试还是不行, ...

  9. Linux的crond和crontab

    一.crond cron是一个linux下的定时执行工具(相当于windows下的scheduled task),可以在无需人工干预的情况下定时地运行任务task. 由于cron 是Linux的ser ...

  10. ALTER TABLE permission is required on the target table of a bulk copy operation if the table has triggers or check constraints, but 'FIRE_TRIGGERS' or 'CHECK_CONSTRAINTS' bulk hints are not specified

    这个是使用SqlBulkCopy进行批量复制导致的异常,此问题涉及大容量导入数据时,控制大容量导入操作是否执行(触发)触发器.大容量导入操作应只对包含支持多行插入的 INSERT 和 INSTEAD ...