Misaki's Kiss again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1621    Accepted Submission(s): 414

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

Hint

In the third sample, gcd(15,10)=5 and (15 xor 10)=5, gcd(15,12)=3 and (15 xor 12)=3,gcd(15,14)=1 and (15 xor 14)=1

 
Source
 
题意:找到1=<m<=n里面满足 gcd(n,m) = n xor m 的m的个数.然后输出所有的 m .
题解:数据量 10^10 ,减少到 10^5 就不会超时了.所以我们从异或操作考虑 , n^m = k ---> n^k = m 然后从gcd(n,m)考虑,因为 n<=m 所以 gcd(n,m)必定是 n 的因子,所以我们可以在 O(sqrt(n)) 的时间里面将 n 的因子全部弄出来,然后枚举其因子, n^factor[i] = m ---> gcd(n,m) == factor[i] 那么这个m就是满足条件的,注意一点就是当 m 的个数为 0 的时候,后面要输出空行。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
LL factor[];
LL gcd(LL a,LL b){
return b==?a:gcd(b,a%b);
}
LL ans[];
int main()
{
LL n;
int t=;
while(scanf("%lld",&n)!=EOF){
factor[] = ;
int id = ;
for(LL i=;i*i<=n;i++){
if(n%i==){
if(i*i==n){
factor[id++] = i;
}else{
factor[id++] = i;
factor[id++] = n/i;
}
}
}
factor[id++] = n;
int cnt = ;
for(LL i=;i<id;i++){
LL M = n^factor[i];
if(M<||M>n) continue;
if(gcd(n,M)==factor[i]){
ans[cnt++] = M;
}
}
printf("Case #%d:\n",t++);
if(cnt==){
printf("0\n\n");
}else{
sort(ans,ans+cnt);
printf("%d\n",cnt);
for(int i=;i<cnt-;i++){
printf("%lld ",ans[i]);
}
printf("%lld\n",ans[cnt-]);
}
}
return ;
}

最新文章

  1. SQL SERVER 多数据导入
  2. oracle表分区以及普表转分区表(转)
  3. 【Java】多线程_学习笔记
  4. 后台运行程序screen or nohup
  5. comboBox绑定数据库、模糊查询
  6. mysql初识之数据文件及其他文件
  7. submit text 插件安装教程
  8. bugumongo--ConnectToMongoDB
  9. LeetCode Same Tree (判断相同树)
  10. php实现返回上一页的功能的3种有效方法
  11. 从SG函数浅谈解决博弈问题的通法
  12. C# Assembly类_反射
  13. python基础教程
  14. retina屏实现border边框1px
  15. mknod用法以及主次设备号【转】
  16. W3Cschool学习笔记&mdash;&mdash;XHTML基础教程
  17. 我是这么配置mariadb的。 为了能够操作汉字数据~
  18. Java-NIO(二):缓冲区(Buffer)的数据存取
  19. K8s(7)-安装Web UI
  20. donet core 应用 部署到CentOS

热门文章

  1. 详解python 局部变量与全局变量
  2. lubuntu 使用USB摄像头
  3. System.NullReferenceException:未将对象引用设置到对象的实例,这是一个新鸟,中鸟,老鸟都避不开的错误
  4. C# 几种读取MAC地址的方法
  5. WebSocket简单介绍(WebSocket 实战)(3)
  6. HDU 6201 transaction transaction transaction(拆点最长路)
  7. [Leetcode] search a 2d matrix 搜索二维矩阵
  8. POJ3415 Common Substrings 【后缀数组 + 单调栈】
  9. vue中使用 echarts3.0 或 echarts2.0 (模拟迁徙图,折线图)
  10. IE浏览器被固定启动时访问某网页的处理方法