Problem Description
After the Ferries Wheel, many friends hope to receive the Misaki's kiss again,so Misaki numbers them
1,2...N−1,N
,if someone's number is M and satisfied the
GCD(N,M)
equals to
N
XOR
M
,he will be kissed again.
Please help Misaki to find all
M(1<=M<=N)
.
Note that:
GCD(a,b)
means the greatest common divisor of
a
and
b
.
A
XOR
B
means
A
exclusive or
B
Input
There are multiple test cases.
For each testcase, contains a integets
N(0<N<=1010)
Output
For each test case,
first line output Case #X:,
second line output
k
means the number of friends will get a kiss.
third line contains
k
number mean the friends' number, sort them in ascending and separated by a space between two numbers
Sample Input
3
5
15
Sample Output
Case #1:
1
2
Case #2:
1
4
Case #3:
3
10 12 14
这道题求0到N-1中,有哪些数m满足gcd(N,m)=N^m.这题的思路是暴力枚举N的所有约数k,也就是0到sqrt(N),然后判断m=n^k满不满足gcd(n,m)=k,注意要考虑约数的“对面”,还要排除约数k*k==N中德一个重复情况,当时被坑是题目里m<=N的限制。还有输出表示的坑,贡献了一次presentation,规定输出为三行,即使cnt为0也要留意空行。
上代码:
#include <iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
long long int n,k,m,cnt;
long long int result[200000];
long long int gcd(long long int a,long long int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int main()
{
int cs=1;
while(scanf("%I64d",&n)!=EOF)
{
cnt=0;
for(long long int i=1;i*i<=n;i++)
{
if(n%i==0)
{
m=n^i;
if(gcd(n,m)==i&&m!=0&&m<=n)//m<=n
{
result[cnt]=m;
cnt++;
}
long long int tmp=n/i;
m=n^tmp;
if(gcd(n,m)==tmp&&m!=0&&tmp!=i&&m<=n)
{
result[cnt]=m;
cnt++;
}
}
}
printf("Case #%d:\n",cs);
printf("%I64d\n",cnt);
sort(result,result+cnt);
for(int i=0;i<cnt;i++)
{
if(i==cnt-1)
{
printf("%I64d\n",result[i]);
}else
{
printf("%I64d ",result[i]);
}
}
if(cnt==0)
{
printf("\n");//错了一个presentation,规定是三行输出,没有也要留一行
}
cs++;
}
//cout << "Hello world!" << endl;
return 0;
}