zoukankan      html  css  js  c++  java
  • Codeforces 963A Alternating Sum ( 思维 && 数论 )

    题意 : 题目链接

    分析 :

    Tutorial 讲的很清楚

    至于为什么这样去考虑

    算是一个经验问题吧

    如果一个问题要你给出模意义下的答案

    就多考虑一下答案是要用逆元构造出来

    也就说明有除法的存在

    那么可以去考虑等比数列或者等差数列求和公式等

    #include<bits/stdc++.h>
    #define LL long long
    using namespace std;
    const LL mod = 1e9 + 9;
    
    LL pow_mod(LL a, LL b)
    {
        LL ret = 1;
        while(b){
            if(b & 1) ret = (ret * a) % mod;
            a = (a * a) % mod;
            b >>= 1;
        }return ret;
    }
    
    LL inv(LL a)
    { return pow_mod(a, mod-2); }
    
    LL n, a, b, k, tmp;
    int main(void)
    {
        ios::sync_with_stdio(false); cin.tie(0);
    
        cin>>n>>a>>b>>k;
    
        string str;
        cin>>str;
    
        LL a0 = 0;
        for(int i=0; i<k; i++){
            tmp = ( pow_mod(a, n-i) * pow_mod(b, i) ) % mod;
            if(str[i] == '-') a0 = ((a0 - tmp) + mod) % mod;
            else a0 = (a0 + tmp) % mod;
        }
    
        LL inv_a = inv(a);
        tmp = (b * inv_a)%mod;
        LL q = pow_mod(tmp, k);
    
        LL res;
    
        if(q == 1){
            res = (a0 * (n+1)/k)%mod;
            cout<<res<<endl;
            return 0;
        }
    
        LL qq = pow_mod(tmp, n+1);
        LL inv_q_1 = inv((q-1+mod)%mod);
        res = (a0 * (qq - 1 + mod)%mod )%mod;
        res = (res * inv_q_1) % mod;
    
        cout<<res<<endl;
        return 0;
    }
    View Code
  • 相关阅读:
    .net 日期格式化
    grunt 上手
    设计模式的认识
    顺时针打印矩阵
    WCF 框架运行时类图
    Python闭包详解
    软件用了那些技术
    zoj 1610 Count the Colors(线段树延迟更新)
    快速提高自己的技术的办法?有两个方法
    纯win32实现PNG图片透明窗体
  • 原文地址:https://www.cnblogs.com/qwertiLH/p/8886322.html
Copyright © 2011-2022 走看看