Codeforces Round #392 (Div. 2)F Geometrical Progression

題目大意:

       找出所有長度爲n,每一項都在l到r之間的等比數列的個數(q!=1)

解題思路:

      其實除了幾種特殊情況外,基本都是枚舉分子分母互質的分數,考慮到只有兩項的時候數量較大,所以單獨判斷

代碼:

#include "iostream"
#include "cstdio"
#include "math.h"
#include "algorithm"
#include "string"
#include "string.h"
#include "vector"
#include "map"
#include "queue"
using namespace std;
long long n, l, r;
long long pow(long long a, long long x) {
	long long ans = 1;
	while (x) {
		if (x & 1)
			ans *= a;
		a *= a;
		x /= 2;
	}
	return ans;
}
long long gcd(long long a, long long b)
{
	return (b>0) ? gcd(b, a%b) : a;
}
int main() {
	while (scanf("%lld %lld %lld", &n, &l, &r) != EOF) {
		if (n == 1) {
			printf("%lld\n", r - l + 1);
		}
		else if (n == 2) {
			printf("%lld\n", (r - l + 1)*(r - l));
		}
		else if (n > 25) {
			puts("0");
		}
		else {
			long long ans = 0;
			long long maxnum = pow(2, log(100000000*0.1)/log(2.0) / (n - 1));
			maxnum = min(maxnum, 3200LL);
			for (long long i = 1;i <= maxnum;i++) {
				for (long long j = i + 1;j <= maxnum;j++) {
					if (gcd(i, j) == 1) {
						long long a = pow(j, n - 1);
						long long b = pow(i, n - 1);
						if (l*a / b > r)
							continue;
						ans += (r*b / a) / b - (l - 1) / b;
					}
				}
			}
			printf("%lld\n", ans * 2);
		}
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章