Loading

CF900D Unusual Sequences - 数论,容斥

题解

首先显然若 \(x\nmid y\) 则无解。否则,令 \(y\gets \frac{y}{x}\)

\(f_{k,g}\) 为选若干个数 \(\langle a_{i=1}^n\rangle\) 使得 \(\sum_{i=1}^n a_i=k\)\(g\mid \gcd(a_1,a_2,\dots,a_n)\) 的方案数。由组合恒等式易得若 \(g\mid k\)\(f_{k,g}=2^{k/g-1}\),否则 \(=0\)

所以 \(f\) 中只有 \(\operatorname{d}(y)\) 个位置有值,\(\operatorname{d}\) 是约数个数函数。

加个光速幂,暴力转移即可,时间复杂度 \(\mathcal{O}([\operatorname{d}(y)]^2)\),不知道为啥跑得挺快。

代码

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define For(Ti,Ta,Tb) for(int Ti=(Ta);Ti<=(Tb);++Ti)
#define Dec(Ti,Ta,Tb) for(int Ti=(Ta);Ti>=(Tb);--Ti)
template<typename T> void Read(T &_x){
	_x=0;int _f=1;
	char ch=getchar();
	while(!isdigit(ch)) _f=(ch=='-'?-1:_f),ch=getchar();
	while(isdigit(ch)) _x=_x*10+(ch^48),ch=getchar();
	_x*=_f;
}
template<typename T,typename... Args> void Read(T &_x,Args& ...others){
	Read(_x);Read(others...);
}
typedef long long ll;
typedef pair<int,ll> pil;
const int Mod=1e9+7;
template<int _V> struct FastPower{
	static const int _Size=sqrt(_V+.5)+1;
	long long _pow1[_Size],_pow2[_Size];
	const int _Base,_Mod;
	FastPower(int _base,int _mod):_Base(_base),_Mod(_mod){
		_pow1[0]=_pow2[0]=1;
		for(int _i=1;_i<_Size;++_i)
			_pow1[_i]=_pow1[_i-1]*_Base%_Mod;
		long long _temp=_pow1[_Size-1]*_Base%_Mod;
		for(int _i=1;_i<_Size;++_i)
			_pow2[_i]=_pow2[_i-1]*_temp%_Mod;
	}
	long long operator()(int _p){
		return _pow1[_p%_Size]*_pow2[_p/_Size]%_Mod;
	}
};
ll x,y;
vector<pil> d;
int main(){
	Read(x,y);
	if(y%x) return puts("0"),0;
	y/=x,x=1;
	FastPower<Mod> pow2(2,Mod);
	for(int i=1;i*i<=y;++i){
		if(y%i==0) d.push_back({i,pow2(y/i-1)}),d.push_back({y/i,pow2(i-1)});
	}
	sort(d.begin(),d.end());
	d.erase(unique(d.begin(),d.end()),d.end());
	d.insert(d.begin(),{0,0});
	for(auto it=prev(d.end());it!=d.begin();--it){
		for(auto jt=next(it);jt!=d.end();++jt){
			if(jt->first%it->first==0)
				it->second=(it->second+Mod-jt->second)%Mod;
		}
	}
	printf("%lld\n",d[1].second);
	return 0;
}
posted @ 2021-10-21 19:28  Alan_Zhao_2007  阅读(66)  评论(0编辑  收藏  举报