codeforces思维题题解(937 div4 D)

原题:Problem - D - Codeforces

题意简化:给一个数n,1 < n < 1e5,问这个数是否可以被每一位都是0 和 1的数(二进制十进制数)相乘表示。

题解:先预处理1 - 1e5 只有1和0的数,存入一个vector容器中。随后再遍历一次1 - 1e5的数进行判断。可以写一个递归的判断,称满足题目条件的数为好数,具体表示为:如果num = 1,num是好数,即递归的结束条件。如果存在num / i (i为先前处理的二进制十进制数 且 不为1)为好数,则num为好数。

将所有结果用bool数组预处理好后,进行多组输入判定并输出即可。

AC代码:

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
using ll = long long;
using namespace std;
const int N = 3e5 + 10;
ll INF = 4e18;
int a[N], p[N], c[N];
bool vis[N];

void solve4()
{
	int n; cin >> n;
	if (vis[n])cout << "YES" << "\n";
	else cout << "NO\n";
}

vector<int> vv;
bool check(int ii)
{
	if (ii == 1)return true;
	for (auto& x : vv)
	{
		if (x == 1)continue;
		if (ii / x == 0)return false;
		if (check(ii / x) && (ii % x == 0))return true;
	}
}
int main()
{
	ios::sync_with_stdio(0), cout.tie(0), cin.tie(0);
	for (int i = 1; i <= 1e5; i++)
	{
		string temp = to_string(i);
		bool ok = 1;
		for (int j = 0; j < temp.length(); j++)
		{
			if (temp[j] != '0' && temp[j] != '1')ok = 0;
		}
		if (ok)
		{
			vv.push_back(i);
			vis[i] = true;
		}
	}

	for (int i = 1; i <= 1e5; i++)
	{
		if (vis[i])continue;
		if (check(i))vis[i] = true;
	}

	int _ = 1; cin >> _;
	while (_--)
		solve4();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值