HDU2683:完全数+组合数

HDU2683

题解:

(n+2)^n = \sum C(n,i)2^{i}n^{n-i}

两边约,得到g(n) = 2n,n为完全数。

完全数:本身等于所有真因子的和。

如果2^{p}-1是素数,那么(2^{p}-1)2^{p-1}是完全数。枚举一下,发现完全数屈指可数。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char t;
ll x,y;
vector<ll>v;
ll power(ll a,ll n){
	ll ans = 1;
	while(n){
		if(n&1)	ans = ans * a;
		a = a * a;
		n >>= 1;
	}
	return ans;
}
bool is_prime(ll n){
	for(ll i=2;i*i<=n;i++){
		if(n % i == 0)	return false;
	}
	return true;
}
void perfect_num(){
	for(int i=2;i<50;i++){
		ll tmp = power(2,i-1);
		if(is_prime(2*tmp-1))
			v.push_back(tmp*(2*tmp-1));
	}
}
int main(){
	perfect_num();
	while(~scanf(" %c",&t))
	if(t == 'A'){
		scanf("%lld%lld",&x,&y);
		if(x > y)	swap(x,y);   //有可能x比y大
		int ans = 0;
		for(int i=0;i<v.size();i++)
			if(x <= v[i] && v[i] <= y)	ans++;
			printf("%d\n",ans);
	}else{
		scanf("%lld",&x);
		bool flag = false;
		for(int i=0;i<v.size();i++)
			if(v[i] == x)	flag = true;
		printf("%d\n",flag);
	}
	return 0;
}

 

posted @ 2019-03-16 11:18  月光下の魔术师  阅读(13)  评论(0)    收藏  举报