P8054 A 质因数

明显看到,题目时间限制 500ms500ms,暴力肯定过不了。但是我们观察一下,需要 m<nm < nf(m)>f(n)f(m) > f(n),我们先用暴力看一下规律:

#include <bits/stdc++.h>
using namespace std;

#define int long long

int sum(int x)
{
	int cnt = 0, q = x;
	for (int i = 2; i <= x; i++)
	{
		if (q % i == 0)
		{
			while (q % i == 0)
			{
				q /= i;
				cnt++;
			}
		}
	}
	return cnt;
}

signed main()
{
	int n = 1000;
	for (register int i = 1; i <= n; i++)
	{
		int x = i;
		int g = x;
		int k = sum(g);
		bool f = true;
		for (register int j = 2; j < g; j++)
		{
			if (sum(j) > k)
			{
				goto end;
				f = false;
				break;
			}
		}
		if (f)
		{
			cout << i << " ";
			k = i;
		}
		end: {}
	}
	return 0;
}

显然,输出 0 的很少,我们看一下最终的输出:

1 2 3 4 6 8 12 16 24 32 48 64 96 128 192 256 384 512 768

4+21=64+2^1=6

6+21=86+2^1=8

8+22=128+2^2=12

12+22=1612+2^2=16

16+23=2416+2^3=24

24+23=3224+2^3=32

……

也就是说,对于每一个询问,从 44 开始,第一次加两个 212^1,第二次加两个 222^2,第三次加两个 232^3……,直到加到这个询问的数。如果正好到了这个询问的数,那就输出 0,不然输出 1

代码:

#include <bits/stdc++.h>
using namespace std;

#define int long long

signed main()
{
	int n;
	cin >> n;
	while (n--)
	{
		int x, now = 4, plus = 2;
		scanf("%lld", &x);
		if (x <= 4) printf("0\n");
		else
		{
			bool f = false;
			while (true)
			{
				now += plus;
				if (now == x)
				{
					printf("0\n");
					f = true;
					break;
				}
				now += plus;
				if (now == x)
				{
					printf("0\n");
					f = true;
					break;
				}
				plus <<= 1;
				if (now > x) break;
			}
			if (!f)
			{
				printf("1\n");
			}
		}
	}
	return 0;
}
posted @   HappyBobb  阅读(3)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示