zoukankan      html  css  js  c++  java
  • [SDOI2008]沙拉公主的困惑

    题面

    传送门

    Sol

    题目要求(sum_{i=1}^{n!}[gcd(i, m!)==1])
    (N=n!,M=m!),莫比乌斯反演一波
    就变成了(sum_{d|M}mu(d)frac{N}{d})
    因为(M|N)所以(d|N)
    而有个定理(sum_{d|M}frac{mu(d)}{d}=frac{varphi(M)}{M})
    那么就是求(frac{varphi(M)}{M}*N)
    就是(varphi(m!)*frac{n!}{m!})
    (varphi(m!)=varphi(m)*(m-1)!)
    化简

    [ans=n!*Pi_{P|m}(1-frac{1}{P}) (P为质数) \ =n!*Pi_{P|m}frac{P-1}{P} ]

    那就变成SB题了
    预处理就好了

    # include <bits/stdc++.h>
    # define IL inline
    # define RG register
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    const int _(1e7 + 1);
    
    IL ll Read(){
    	RG char c = getchar(); RG ll x = 0, z = 1;
    	for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    	for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    	return x * z;
    }
    
    int n, m, Zsy, prime[_], num, fac[_], inv[_], id[_];
    bool isprime[_];
    
    IL int Pow(RG ll x, RG ll y){
    	RG ll ret = 1;
    	for(; y; y >>= 1, x = x * x % Zsy) if(y & 1) ret = ret * x % Zsy;
    	return ret;
    }
    
    IL void Sieve(){
    	isprime[1] = 1; fac[1] = 1;
    	for(RG int i = 2; i < _; ++i){
    		if(!isprime[i]) prime[++num] = i , inv[num] = Pow(i, Zsy - 2);
    		for(RG int j = 1; j <= num && i * prime[j] < _; ++j){
    			isprime[i * prime[j]] = 1;
    			if(!(i % prime[j])) break;
    		}
    		fac[i] = 1LL * fac[i - 1] * i % Zsy;
    	}
    	for(RG int i = 1; i < num; ++i)
    		for(RG int j = prime[i]; j < prime[i + 1]; ++j) id[j] = i;
    	inv[0] = prime[0] = 1;
    	for(RG int i = 1; i <= num; ++i){
    		prime[i] = 1LL * (prime[i] - 1) * prime[i - 1] % Zsy;
    		inv[i] = 1LL * inv[i] * inv[i - 1] % Zsy;
    	}
    }
    
    IL int Calc(){  return 1LL * fac[n] * prime[id[m]] % Zsy * inv[id[m]] % Zsy; }
    
    int main(RG int argc, RG char* argv[]){
    	RG int T = Read(); Zsy = Read();
    	Sieve();
    	while(T--){
    		n = Read(); m = Read();
    		printf("%d
    ", Calc());
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    Spark Streaming源码解读之Receiver生成全生命周期彻底研究和思考
    linux 修改时间时区,修改语言
    远程链接mysql error 2003
    Android NDK r10c 编译boost 1.55 (使用Cygwin)
    linux上cocos2dx Android打包环境
    linux上cocos2dx 环境配置
    linux, windows编译安装 boost库 (boost 1.56)
    编译安装 gcc 4.8.3
    vim配置添加python
    mvn设置
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8330555.html
Copyright © 2011-2022 走看看