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<<" ";
	}
	
} 
发布了31 篇原创文章 · 获赞 13 · 访问量 714

猜你喜欢

转载自blog.csdn.net/weixin_43310882/article/details/103986085