Educational Codeforces Round 33 题解

E. Counting Arrays

#ifndef DEBUG
#pragma GCC optimize("O2")
#endif

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<limits.h>
#include<assert.h>
using namespace std;
#define all(X) (X).begin(), (X).end()
#define rep(I,A,B) for(int I=(A);I<(B);I++)
#define repd(I,A,B) for(int I=(A);I>(B);I--)
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<int> VI;
typedef vector<PII> VPII;
typedef vector<LL> VLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &&head, U &&... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W("{"); _W(x.first); putchar(','); _W(x.second); _W("}");}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() { }
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }

#define N 2000001 
#define P 1000000007 

struct Z { // 带模整数,假设-P<=x<2P
	int x;
	Z(int x = 0) : x(norm(x)) {}
	Z inv() const { assert(x != 0); return pow(P - 2); }
	Z pow(int b) const {
		Z a=x,res=1;
		for(; b; b/=2,a*=a)
			if(b&1) res *= a;
		return res;
	}
	Z &operator*=(const Z &rhs) { x = 1LL * x * rhs.x % P; return *this; }
	Z &operator+=(const Z &rhs) { x = norm(x + rhs.x); return *this; }
	Z &operator-=(const Z &rhs) { x = norm(x - rhs.x); return *this; }
	Z &operator/=(const Z &rhs) { return *this *= rhs.inv(); }
	friend Z operator*(const Z &lhs, const Z &rhs) { Z res = lhs; res *= rhs; return res; }
	friend Z operator+(const Z &lhs, const Z &rhs) { Z res = lhs; res += rhs; return res; }
	friend Z operator-(const Z &lhs, const Z &rhs) { Z res = lhs; res -= rhs; return res; }
	friend Z operator/(const Z &lhs, const Z &rhs) { Z res = lhs; res /= rhs; return res; }
	friend ostream &operator<<(ostream &os, const Z &z) { os << z.x; return os; }
	int norm(int x) { return x<0 ? x+=P : (x>=P ? x-=P : x); }
};

typedef vector<Z> VZ;

Z C(VZ &f, VZ &inv, int n, int m) {
	return f[n]*inv[m]*inv[n-m];
}

int main()
{
    #ifdef DEBUG
    freopen("1.in","r",stdin);
    //freopen("tmp.out","w",stdout);
    #endif

    VZ f(N), inv(N); // 数组不能只开到1e5,不然会越界
    f[0]=1;
    rep(i,1,N)
    	f[i]=f[i-1]*i;
    //W(f);
    inv[0]=inv[1]=1;
    rep(i,2,N)
    	inv[i]=(-P/i)*inv[P%i];
    rep(i,2,N)
    	inv[i]=inv[i-1]*inv[i];
    //W(inv);
    
    int q;
    R(q);
    rep(_,0,q) {
    	int n,m; R(n,m);
    	
    	Z ans=1;
    	int i=2;
    	while(i<= n/i) {
    		if(n%i==0) {
    			int c=0;
    			while(n%i==0) {
    				n/=i;
    				c++;
    			}
    			ans *= C(f,inv,c+m-1,m-1);
    		}
    		i++;
    	}
    	
    	if(n>1) { // 如果没分解干净
    		ans *= C(f,inv,m,m-1);
    	}
    	
    	ans *= Z(2).pow(m-1);
    	W(ans);
    }
    
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值