Favourite Number

Volodymyr’s favourite number is A and it has an odd number of positive divisors. When you add K to this number, the resulting sum also has an odd number of positive divisors. Given K, find all possible values of A
Volodymyr’s favourite number.
Input
The only line of input contains K a positive integer not exceeding 1e9

Output
On the first line, print the number of possible values of A. If the number of such values is greater than zero, on the second line print all possible values of A in ascending order, separated by a single space.
Examples
Input

1

Output

0

Input

2

Output

1
-1

Note

Note that since zero has infinitely many divisors, it cannot be classified as number with an odd number of divisors.
题意很简单,给你一个数K,让你找一个A,A要满足A有奇数个正因子,且A加K后仍有奇数个正因子。
思路:首先你要知道只有平方数才有奇数个因子,所以,A要为平方数或者平方数的负数形式。且得到的也为平方数或者平方数的负数形式。
1.(a^2 )+ (b ^2)=k 压入aa,bb
2.(a^2 )-k =(b ^2) 压入-aa,bb
`` 记得去除零的情形

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
set<ll>s;
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	ll k;
	cin>>k;
	ll a,b,a1,b1;
	for(int i=1;i*i<=k;i++)
	{
		a1=k-i*i;
		a=sqrt(a1);
		if(a*a==a1)
		{
			if(i*i!=0&&a!=0)
			{
				s.insert(-i*i);
				s.insert(-a1);	
			}					
		}
		
		if((k-i*i)%(2*i)==0)
		{
			a=(k-i*i)/(2*i);
			a1=a*a;
			b1=k+a1;
			if(a1==0||b1==0)
			{
				continue;
			} 
			s.insert(a1);
			s.insert(-b1);
		}
		
	}
	cout<<s.size()<<endl;
	set<ll>::iterator it=s.begin();
	for(;it!=s.end();it++)
	{
		cout<<*it<<" ";
	}
	
} 
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值