http://codeforces.com/contest/964/problem/C

题目大意:给出一个等比序列求和并且mod 1e9+9.

题目分析:等比数列的前n项和公式通过等公比错位相减法可以得到是Sn=A1*(q^n-1)/(q-1).这里注意q不能等于1.

坑点:而本题的q=(b/a)^k,由于a,b,k的数据范围也很大,所以需要求逆元来进行除法取模,而这个过程会出现b/a原本不等于1,而在求逆元取模之后的结果为1,所以不光要判断一下b/a的结果是否等于1,还要判断一下b*mypow(a,mod-2)%mod的结果是否等于1. 被这个坑卡了一个小时..orz

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long ll;
char ch[];
const ll mod=1e9+;
ll mypow(ll x,ll y){
ll ans=;
while(y){
if(y&)ans=(ans%mod)*(x%mod)%mod;
x=(x%mod)*(x%mod)%mod;
y/=;
}
return ans%mod;
}
int main(){
ll n,a,b,k;
cin>>n>>a>>b>>k;
scanf("%s",ch);
ll sum=;
ll bb=;
ll p=(mypow(a,n))%mod;
for(int i=;i<k;i++)
{
if(ch[i]=='+')
{
sum=(sum+p)%mod;
}
else
sum=(sum-p+mod)%mod;
p=(p*b)%mod;
p=(p*mypow(a,mod-))%mod;
}
ll ans=sum;
if(a==b){
sum=(sum%mod)*(((n+)/k)%mod)%mod;
}
else{
ll ad1=mypow(b,k)%mod;
ll ad2=mypow(a,k)%mod;
ll ss=mypow(ad2,mod-)%mod;
ss=(ss*ad1)%mod;
if(ss==){
ll ans=(sum*(n+)/k)%mod;
cout<<ans<<endl;
return ;
}
ll c1=mypow(ss,(n+)/k)%mod;
ll c2=(c1-+mod)%mod;
sum=((sum)*c2)%mod;
sum=(sum*mypow((ss-+mod)%mod,mod-))%mod;
}
cout << (sum+mod)%mod<<endl;
return ;
}

[codeforces round#475 div2 ][C Alternating Sum ]的更多相关文章

  1. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  2. Codeforces Round #564(div2)

    Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...

  3. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  4. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  5. Codeforces Round #361 div2

    ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...

  6. Codeforces Round #475 Div. 2 A B C D

    A - Splits 题意 将一个正整数拆分成若干个正整数的和,从大到小排下来,与第一个数字相同的数字的个数为这个拆分的权重. 问\(n\)的所有拆分的不同权重可能个数. 思路 全拆成1,然后每次将2 ...

  7. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2)

    A. Splits time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...

  8. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 1)

    A. Alternating Sum 就是个等比数列,特判公比为 $1$ 的情况即可. #include <bits/stdc++.h> using namespace std; ; ; ...

  9. Codeforces Round #626 Div2 D,E

    比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...

随机推荐

  1. OOP⑵

    1.问题? 怎么创建对象? 类名 对象名=new 类名(); 在java中只要是看到了()! 这就是方法! 2.构造方法: 创建某个对象的方法! Student stu=new Student(); ...

  2. what’s this?

    jdk,jre,jvm三者区别:JDK: (Java Development ToolKit) java开发工具包.JDK是整个java的核心! 包括了java运行环境 JRE(Java Runtim ...

  3. tomcat的安装及配置

    1.首先进tomcat官网下载zip压缩文件:http://tomcat.apache.org/download-90.cgi 2.解压缩到指定文件压(后面配置环境变量会用到) 3.配置环境变量 4. ...

  4. 开窗函数 函数() OVER()

    -- 初始化 CREATE TABLE T_Person (FName VARCHAR2(20), FCity VARCHAR2(20), FAge INT, FSalary INT); INSERT ...

  5. linux自动更新代码,自动备份数据库,打包应用发布

    切换root用户 sudo su - 1.安装svn,mysql yum install subversion yum install mysql 2.安装 maven 下载:百度云盘地址为 http ...

  6. POJ - 1850 B - Code

    Transmitting and memorizing information is a task that requires different coding systems for the bes ...

  7. cf467D(map,vector,bfs,特点提取)

    D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. codeforce150A(简单的求质数问题)

    A. Win or Freeze time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. C# struct and enum

    struct Person { public int age; public string name; public string fname; public string class; } enum ...

  10. for循环遍历改用map函数

    # for url in urls:# url = response.urljoin(url)# print(url)urls = map(lambda url:response.urljoin(ur ...