Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) C.Alternating Sum 题意+解析+ 答案

C. Alternating Sum
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two integers aa and bb. Moreover, you are given a sequence s0,s1,,sns0,s1,…,sn. All values in ss are integers 11 or 1−1. It's known that sequence is kk-periodic and kk divides n+1n+1. In other words, for each kink≤i≤n it's satisfied that si=siksi=si−k.

Find out the non-negative remainder of division of ni=0sianibi∑i=0nsian−ibi by 109+9109+9.

Note that the modulo is unusual!

Input

The first line contains four integers n,a,bn,a,b and kk (1n109,1a,b109,1k105)(1≤n≤109,1≤a,b≤109,1≤k≤105).

The second line contains a sequence of length kk consisting of characters '+' and '-'.

If the ii-th character (0-indexed) is '+', then si=1si=1, otherwise si=1si=−1.

Note that only the first kk members of the sequence are given, the rest can be obtained using the periodicity property.

Output

Output a single integer — value of given expression modulo 109+9109+9.

Examples
Input
Copy
2 2 3 3
+-+
Output
Copy
7
Input
Copy
4 1 5 1
-
Output
Copy
999999228
Note

In the first example:

(ni=0sianibi)(∑i=0nsian−ibi) = 22302131+20322230−2131+2032 = 7

In the second example:

(ni=0sianibi)=14501351125211531054=781999999228(mod109+9)(∑i=0nsian−ibi)=−1450−1351−1252−1153−1054=−781≡999999228(mod109+9).

这题还没过,大佬的思路(还未验证)

以下公式提出一个a^n后就有这个等比数列

b/a可以用乘法逆元c=b*a^(-1)来表示;

则有c==1单独讨论);

对于以k为周期的数列,

可以考虑求出每一个i+ki+2k......i+m*k(i+m*k<=n);

则变形为X=

C=,则有X=

则每一个k的表达式为(sign)

快速幂加逆元,可以在Ologn)内求出每一个i

总时间复杂度O(k*logn)


大佬代码1(已过):

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll p=1e9+9;
ll Inv,C;
ll pow1(ll a,ll n)
{
    ll ans=1;
    a=a%p;
    while(n)
    {
        if(n&1)ans=ans*a%p;
        a=a*a%p;
        n>>=1;
    }
    return ans;
}
ll f(ll n)
{
    if(C==1)return n+1;
    return (pow1(C,n+1)-1+p)%p*Inv%p;
}
int main()
{
    ll n,A,B,k,c,x;
    cin>>n>>A>>B>>k;
   // if(A!=B)
        c=B*pow1(A,p-2)%p;
    C=pow1(c,k);
    if(C!=1)
        Inv=pow1(C-1,p-2);
    ll s=pow1(A,n);
    ll ans=0;
    string ss;
    cin>>ss;
    for(int i=0; i<min(n+1,k); i++)
    {
            if(ss[i]=='+')x=1;
            else x=-1;
            ans+=x*f((n-i)/k)*pow1(c,i)%p;
            ans=ans+p;
            ans%=p;

    }
 cout<<ans*s%p<<endl;
    return 0;
}

大佬代码2(已过):

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll p=1e9+9;
ll Inv,C;
ll pow1(ll a,ll n)
{
    ll ans=1;
    a=a%p;
    while(n)
    {
        if(n&1)ans=ans*a%p;
        a=a*a%p;
        n>>=1;
    }
    return ans;
}
ll f(ll n)
{
    if(C==1)return n+1;
    return (pow1(C,n+1)-1+p)%p*Inv%p;
}
int main()
{
    ll n,A,B,k,c,x;
    cin>>n>>A>>B>>k;
   // if(A!=B)
        c=B*pow1(A,p-2)%p;
    C=pow1(c,k);
    if(C!=1)
        Inv=pow1(C-1,p-2);
    ll s=pow1(A,n);
    ll ans=0;
    string ss;
    cin>>ss;
    for(int i=0; i<min(n+1,k); i++)
    {
            if(ss[i]=='+')x=1;
            else x=-1;
            ans+=x*f((n-i)/k)*pow1(c,i)%p;
            ans=ans+p;
            ans%=p;

    }
 cout<<ans*s%p<<endl;
    return 0;
}

这题时间超时了,思路有点不对,能处理一些数据,但1e9无法很好处理
超时代码:
#include<bits/stdc++.h>

using namespace std;
string str;
#define ll long long
ll mi(ll a,ll b)
{
    ll ans=1;
    a%=1000000009;
    while (b)
    {
        if (b%2==1) ans=ans*a%1000000009;
        b>>=1;
        a=a*a%1000000009;
    }
    return ans;
}
int main()
{
    ll n,a,b,c,t,ans=0,sum=0;
    cin >> n >> a>> b >> c ;
    cin >> str;
    for(int i=0; i<=n; i++)
    {

        if(str[i%c]=='-')
        {
            ans-=mi(a,n-i)*(mi(b,i));
            //cout << 32<< endl;
        }
        else if(str[i%c]=='+')
        {
            ans+=mi(a,n-i)*(mi(b,i));
            //cout << 33<< endl;
        }
        ans=ans%1000000009;
    }
    //cout << 3 << endl;
    if(ans<0)
    {
        ans=ans+1000000009;
    }
    cout << ans << endl;
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值