论T1考场上的神奇做法
查看原帖
论T1考场上的神奇做法
328170
Kalium楼主2020/11/8 09:36

由于本人不会二进制拆分

所以在考场上打了个神奇做法

思路:根据样例观察可得,如果是奇数绝对输出-1,偶数是肯定能行的,那么数据就到10^7,我把每一位数上的完全平方打个表,存到7个数组里面,然后对于输入的n进行1位1位判断,看看数组中哪个数大于它,就减去这个数组前面的那一个数并且输出,如果等于,直接输出即可

我贴个代码

#include <iostream>
#include <cstdio>
#define inf 0x3f3f3f3f

using namespace std;

int n;
int a[8] = {2, 4, 8, inf};
int b[8] = {8, 16, 32, 64, inf};
int c[8] = {64, 128, 256, 512, inf};
int d[8] = {512, 1024, 2048, 4096, 8192, inf};
int e[8] = {8192, 16384, 32768, 65536, inf};
int f[8] = {65536, 131072, 262144, 524288, inf};
int g[8] = {524288, 1048576, 2097152, 4194304, 8388608, inf};
int cnt;

inline int num(int x)
{
	cnt = 0;
	for (int i = x; i; i /= 10)
		cnt ++; 
	return cnt;
}

int main()
{
	freopen("power.in", "r", stdin);
	freopen("power.out", "w", stdout);
	scanf("%d", &n);
	if (n % 2)
	{
		printf("-1\n");
		return 0;
	}
	while (num(n) == 7)
	{
		for (int i = 1; i <= 5; i ++)
		{
			if (n == g[i])
			{
				cout << g[i] << endl;
				return 0;
			}
			if (n < g[i])
			{
				n -= g[i - 1];
				cout << g[i - 1] << " ";
				break;
			}
		}
	}
	while (num(n) == 6)
	{
		for (int i = 1; i <= 4; i ++)
		{
			if (n == f[i])
			{
				cout << f[i] << endl;
				return 0;
			}
			if (n < f[i])
			{
				n -= f[i - 1];
				cout << f[i - 1] << " ";
				break;
			}
		}
	}
	while (num(n) == 5)
	{
		for (int i = 1; i <= 4; i ++)
		{
			if (n == e[i])
			{
				cout << e[i] << endl;
				return 0;
			}
			if (n < e[i])
			{
				n -= e[i - 1];
				cout << e[i - 1] << " ";
				break;
			}
		}
	}
	while (num(n) == 4)
	{
		for (int i = 1; i <= 5; i ++)
		{
			if (n == d[i])
			{
				cout << d[i] << endl;
				return 0;
			}
			if (n < d[i])
			{
				n -= d[i - 1];
				cout << d[i - 1] << " ";
				break;
			}
		}
	}
	while (num(n) == 3)
	{
		for (int i = 1; i <= 4; i ++)
		{
			if (n == c[i])
			{
				cout << c[i] << endl;
				return 0;
			}
			if (n < c[i])
			{
				n -= c[i - 1];
				cout << c[i - 1] << " ";
				break;
			}
		}
	}
	while (num(n) == 2)
	{
		for (int i = 1; i <= 4; i ++)
		{
			if (n == b[i])
			{
				cout << b[i] << endl;
				return 0;
			}
			if (n < b[i])
			{
				n -= b[i - 1];
				cout << b[i - 1] << " ";
				break;
			}
		}
	}
	//cout << n << endl;
	while (num(n) == 1 && n > 1)
	{
		for (int i = 1; i <= 4; i ++)
		{
			if (n == a[i])
			{
				cout << a[i] << endl;
				return 0;
			}
			if (n < a[i])
			{
				n -= a[i - 1];
				cout << a[i - 1] << " ";
				break;
			}
		}
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

很长,但是洛谷的民间我过了,不知道ccf怎么样

2020/11/8 09:36
加载中...