Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) (弱鸡持续更新中) 特别注意a的-k次方取模方法

点击打开链接

A. Splits 数学题 思维题

#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    scanf("%d",&n);
    cout<<n/2+1<<endl;
    return 0;
}

B. Messages 数学题 思维题

//要么选择损失B,要么选择损失C
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,A,B,C,T;
    scanf("%d%d%d%d%d",&n,&A,&B,&C,&T);
    int sum=0;
    for(int i=0;i<n;i++)
    {
        int t;
        scanf("%d",&t);
        sum+=A-(T-t)*B+(T-t)*C;
    }
    if(B<C) cout<<sum<<endl;
    else cout<<n*A<<endl;
    return 0;
}

C. Alternating Sum 数论 等比数列 快速幂取模 特别注意a的-k次方取模的方法

//等比数列
//a的-k次方取模一定要注意
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MD=1e9+9;
//快速幂取模
LL po(LL a,LL b)
{
    LL ans=1;
    while(b)
    {
        if(b&1) ans=ans*a%MD;
        b>>=1,a=a*a%MD;
    }
    return ans;
}
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    LL n,a,b,k;
    cin>>n>>a>>b>>k;
    string s;
    cin>>s;
    LL cur=0;
    for(int i=0;i<k;i++)                        //先计算一个周期内的值
    {
        if(s[i]=='+') cur=(cur+po(a,(n-i))*po(b,i)%MD)%MD;
        else cur=(cur-po(a,(n-i))*po(b,i)%MD+MD)%MD;
    }
    //cout<<cur<<endl;
    //等比数列 公比为(b/a)^k
    LL t=po(a,k*(MD-2)%(MD-1))*po(b,k)%MD;                  //a^(-k)*b^(k)
    if(t==1)
        cout<<cur*((n+1)/k)%MD;
    else
        {
            //等比数列求和公式
            LL sx=(1-po(t,(n+1)/k)+MD)%MD;
            cout<<((cur*sx%MD*po(1-t,(MD-2)%(MD-1)))%MD+MD)%MD;
        }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37428263/article/details/80058050