暑假数论专题

Prime Distance

给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数对与差值最大的数对。当存在多个时,输出靠前的素数对。

多组数据。每行两个数 L,R。

InputOutput
2 17
14 17
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.

 对于全部数据,1≤L<R<2 ^31,R−L≤10^6。

思路:

        面对到这题刚开始没什么思路,数据又比较大,暴力的方法也是行不通。于是就看了别人的代码,发现这个l与r这个区间最大时1e6,于是就可以将大树的区间转化成一个从0开始的最大到1e6的区间。

       大数的问题解决后就是关于如何去找素数的问题了,那么我们可以用区间筛将素数全部都筛出来。这样直接来找答案即可

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pt cout << "---------------" << endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 10;
int a[maxn], p[maxn], cnt;
bool b[maxn];
void get_prime(int x) {
	for (int i = 2; i <= x; i ++ ) {
		if (!b[i]) p[cnt ++] = i;
		for (int j = 0; p[j] <= x / i; j ++ ) {
			b[p[j] * i] = true;
			if (i % p[j] == 0) {
				break;
			}
		}
	}
}
int main()
{
	get_prime(50000);
	ll l, r;
	while (~scanf("%lld %lld", &l, &r)) {
		if (l == 1) {
			l ++;
		}
		memset(a, 0, sizeof a);
		for (int i = 0; i < cnt; i ++ ) {
			ll x = (l - 1) / p[i] + 1;
			ll y = r / p[i];
			for (int j = x; j <= y; j ++ ) if (j > 1) a[j * p[i] - l] = 1;
		}
		ll x = -1, ma = -1, mi = 0x3f3f3f3f, xa, ya, xi, yi;
		for (int i = 0; i <= r - l; i ++ ) {
			if (!a[i]) {
				if (x == -1) {
					x = i;
					continue;
				} 
				if (ma < i - x) {
					ma = i - x;
					xa = l + x;
					ya = l + i;
				}
				if (mi > i - x) {
					mi = i - x;
					xi = l + x;
					yi = l + i;
				}
				x = i;
			}
		}
		if (ma == -1) {
			printf("There are no adjacent primes.\n");
		} else {
			printf("%lld,%lld are closest, %lld,%lld are most distant.\n", xi, yi, xa, ya);
		}
	}
	return 0;
}

反素数 Antiprime

如果一个大于等于 1 的正整数 n,满足所有小于 n 且大于等于 1 的所有正整数的约数个数都小于 n 的约数个数,则 n 是一个反素数。譬如:1,2,4,6,12,24,它们都是反素数。

请你计算不大于 n 的最大反素数。

一行一个正整数 n。

只包含一个整数,即不大于 n 的最大反素数。

InputOutput
1000
840

对于 10% 的数据,1≤n≤10^3;

对于 40% 的数据,1≤n≤10^6;

对于 100% 的数据,1≤n≤2×10^9。

思路:

又是一道不会的题目,参考了别人的代码,设置两个变量sum表示约数的个数,ans表示当前最大的反素数,然后通过暴搜来获取答案

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pt cout << "---------------" << endl;
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n;
ll ans1 = 0, ans2 = 0;
int a[100] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
void dfs(int now, int cnt, ll k, int sum) {
	if (sum > ans2) ans1 = k, ans2 = sum;
	if (sum == ans2 && k < ans1) {
		ans1 = k;
	}
	for (int i = 1; i <= cnt; i ++ ) {
		k *= a[now];
		if (k > n) break;
		dfs(now + 1, i, k, sum * (i + 1));
	}
}
int main()
{
	scanf("%d", &n);
	dfs(1, 64, 1, 1);
	printf("%lld", ans1);
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值