码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5297 Y sequence(容斥)

时间:2015-07-31 23:37:34      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:hdu 5297 Y sequence


考虑62以内的指数,x为奇数个质数因子,就减掉,偶数个加上。计算x为指数的不满足数直接pow(n,1/x)即可。


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long ll;
const int fac[30] = {-2, -3, -5, -7, -11, -13, -17, -19, -23, -29, -31, -37, -41, -43, -47, -53, -59, -61, -67};

ll N;
int R;
vector<int> S;

void init () {
	S.clear();
	scanf("%lld%d", &N, &R);

	for (int i = 0; abs(fac[i]) <= R; i++) {
		int tmp = S.size();
		for (int j = 0; j < tmp; j++) {
			if (abs(fac[i] * S[j]) <= 63)
				S.push_back(fac[i] * S[j]);
		}
		S.push_back(fac[i]);
	}
}

ll get(ll n) {
	if (n == 1)
		return 0;
	ll ret = n;
	for (int i = 0; i < S.size(); i++) {
		ll tmp = (ll)(pow(n + 0.5, 1.0 / abs(S[i]))) - 1;
		if (S[i] < 0)
			ret -= tmp;
		else
			ret += tmp;
	}
	return ret - 1;
}

void solve () {
	ll ret = N;
	while (true) {
		ll tmp = get(ret);
		if (tmp == N)
			break;
		ret += N - tmp;
	}
	printf("%lld\n", ret);
}

int main () {
	int cas;
	scanf("%d", &cas);
	for (int i = 1; i <= cas; i++) {
		init();
		solve();
	}
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

hdu 5297 Y sequence(容斥)

标签:

原文地址:http://blog.csdn.net/keshuai19940722/article/details/47176337

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!