HDU 7105 Power Sum

Problem Description

Given a positive number n, Kris needs to find a positive number k and an array {ai}(ai∈{−1,1}) of length k(1≤k≤n+2), such that:
 

∑i=1kai×i2=n



This is too hard for Kris so you have to help him.

 

Input

The input contains multiple test cases.

The first line contains an integer T(1≤T≤100) indicating the number of test cases.

Each of the next T lines contains one integer n(1≤n≤106).

It's guaranteed that ∑n≤3∗107.

 

Output

The output should contain 2T lines. For each test case, output two lines.

The first line contains one integer, k.

The second line contains a 01-string of length k representing the array, with 0 in the ith position denoting ai=−1 and 1 denoting ai=1.

If there are multiple answers, print any.

 

Sample Input

 
 

2 1 5

 

Sample Output

 
 

1 1 2 11

 题意:给定一个n,要求找到一个长度为k的数组,使得\sum_{i=1}^{k}a_{i}*i^2=n,其中a_{i}只能为1或-1;

思路:经过分析可以发现"1001"为4、"1"为1、"0001"为2、"01"为3;于是对于任何n都可以通过01串构造出来;

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

const int N=100010;

int a[N];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int m=n/4;
		switch (n%4) 
		{
		case 0:cout<<n<<endl;break;
		case 1:cout<<n<<endl<<"1";break;
		case 2:cout<<n+2<<endl<<"0001";break;
		case 3:cout<<n-1<<endl<<"01";break;
		}
		for(int i=0;i<m;i++)
			cout<<"1001";
		puts("");
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Double.Qing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值