zoukankan      html  css  js  c++  java
  • [洛谷P5431]【模板】乘法逆元2

    题目大意:给定$n(nleqslant5 imes10^6)$个正整数$a_i$,和$k$。求:
    $$
    sum_{i=1}^ndfrac{k^i}{a_i}pmod p
    $$
    题解:
    $$
    令P=prod_{i=1}^na_ipmod p\
    ans=dfrac{sum_{i=1}^nk^idfrac P{a_i}}{P}pmod p\
    dfrac P{a_i}可以前缀积后缀积解决
    $$
    卡点:

    C++ Code:

    #include <cstdio>
    #include <cctype>
    #include <algorithm>
    
    namespace std {
    	struct istream {
    #define M (1 << 26 | 3)
    		char buf[M], *ch = buf - 1;
    		inline istream() { fread(buf, 1, M, stdin); }
    		inline istream& operator >> (int &x) {
    			while (isspace(*++ch));
    			for (x = *ch & 15; isdigit(*++ch); ) x = x * 10 + (*ch & 15);
    			return *this;
    		}
    #undef M
    	} cin;
    	struct ostream {
    #define M (1 << 10 | 3)
    		char buf[M], *ch = buf - 1;
    		inline ostream& operator << (int x) {
    			if (!x) {*++ch = '0'; return *this;}
    			static int S[20], *top; top = S;
    			while (x) {*++top = x % 10 ^ 48; x /= 10;}
    			for (; top != S; --top) *++ch = *top;
    			return *this;
    		}
    		inline ostream& operator << (const char x) {*++ch = x; return *this;}
    		inline ~ostream() { fwrite(buf, 1, ch - buf + 1, stdout); }
    #undef M
    	} cout;
    }
    
    #define maxn 5000010
    #define mul(a, b) (static_cast<long long> (a) * (b) % mod)
    int n, mod, k, pr = 1;
    int a[maxn], sl[maxn], sr[maxn];
    
    namespace Math {
    	int pw(int base, int p) {
    		static int res;
    		for (res = 1; p; p >>= 1, base = mul(base, base)) if (p & 1) res = mul(res, base);
    		return res;
    	}
    	int inv(int x) { return pw(x, mod - 2); }
    }
    inline void reduce(int &x) { x += x >> 31 & mod; }
    
    int main() {
    	std::cin >> n >> mod >> k;
    	for (int i = 1; i <= n; ++i) {
    		std::cin >> a[i];
    		sl[i] = pr = mul(pr, a[i]);
    	}
    	pr = Math::inv(pr);
    	sl[0] = sr[n + 1] = 1;
    	for (int i = n; i; --i)
    		sr[i] = mul(sr[i + 1], a[i]);
    	int ans = 0, K = 1;
    	for (int i = 1; i <= n; ++i) {
    		K = mul(K, k);
    		reduce(ans += mul(K, mul(sl[i - 1], sr[i + 1])) - mod);
    	}
    	ans = mul(ans, pr);
    	std::cout << ans << '
    ';
    	return 0;
    }
    

      

  • 相关阅读:
    repair grub in Ubuntu
    DNS attack experiment
    新闻随感(摩托罗拉125亿被Google收购)
    成为C++高手必须要看的书
    nginx
    Nginx Pitfalls
    gcc/gdb
    python 删除文件
    Solve nginx Error 413 Request Entity Too Large
    Solve Nginx Error 413: Request Entity Too Large
  • 原文地址:https://www.cnblogs.com/Memory-of-winter/p/11044239.html
Copyright © 2011-2022 走看看