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 ;
}

hdu 5175(数论)的更多相关文章

  1. GCD and LCM HDU 4497 数论

    GCD and LCM HDU 4497 数论 题意 给你三个数x,y,z的最大公约数G和最小公倍数L,问你三个数字一共有几种可能.注意123和321算两种情况. 解题思路 L代表LCM,G代表GCD ...

  2. HDU 4497 数论+组合数学

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4497 解题思路:将满足条件的一组x,z,y都除以G,得到x‘,y',z',满足条件gcd(x',y' ...

  3. hdu 4542 数论 + 约数个数相关 腾讯编程马拉松复赛

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4542 小明系列故事--未知剩余系 Time Limit: 500/200 MS (Java/Others) ...

  4. hdu 4961 数论?

    http://acm.hdu.edu.cn/showproblem.php?pid=4961 给定ai数组; 构造bi, k=max(j | 0<j<i,a j%ai=0), bi=ak; ...

  5. hdu 1664(数论+同余搜索+记录路径)

    Different Digits Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. hdu 3641 数论 二分求符合条件的最小值数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=3641 学到: 1.二分求符合条件的最小值 /*================================= ...

  7. hdu 4059 数论+高次方求和+容斥原理

    http://acm.hdu.edu.cn/showproblem.php? pid=4059 现场赛中通过率挺高的一道题 可是容斥原理不怎么会.. 參考了http://blog.csdn.net/a ...

  8. HDU 4651 数论 partition 求自然数的拆分数

    别人的解题报告: http://blog.csdn.net/zstu_zlj/article/details/9796087 我的代码: #include <cstdio> #define ...

  9. hdu 5505(数论-gcd的应用)

    GT and numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. SQL SERVER 时间相关操作笔记

    1.DATEADD函数: A.  MSDN上的示例:http://msdn.microsoft.com/zh-cn/library/ms186819%28v=sql.90%29.aspx

  2. java文件的I/O

    [原创] java文件的I/O操作,简单来说就是向文件中写入数据以及从文件中读出数据,这是我们平日做的最多的操作,这里给出两种文件I/O操作,当然还有许多的操作方法,各种流的使用,可谓是高深莫测:不管 ...

  3. lintcode-124-最长连续序列

    124-最长连续序列 给定一个未排序的整数数组,找出最长连续序列的长度. 说明 要求你的算法复杂度为O(n) 样例 给出数组[100, 4, 200, 1, 3, 2],这个最长的连续序列是 [1, ...

  4. springmvc项目搭建四-基于前端框架完善页面的数据显示

    上一篇把前端框架先放上去了,现在开始前后端进行交互,对数据进行显示. 效果如图所示...中间经历了数据显示不上去的问题,是对于spring的注解了解不够,问题及其解决可以参照上一篇问题处理... 目前 ...

  5. Object empty value key filter

    Object empty value key filter 过滤空值 Utils emptykeysFilter() "use strict"; /** * * @author x ...

  6. Java 如何正确停止一个线程

    自己在做实验性小项目的时候,发现自己遇到一个问题:如何控制线程的"死亡"? 首先,如何开启一个线程呢? 最简单的代码: public class Main { public sta ...

  7. RDMA

    什么是RDMA? 来源 https://blog.csdn.net/u011459120/article/details/78469098 1. 概述 RDMA是Remote Direct Memor ...

  8. Generator的基本用法

    Generator函数是一个状态机,封装了多个内部状态.执行一个Generator,会返回一个迭代器对象,通过迭代器对象,可以遍历Generator函数内部的每个状态.因此,Generator函数可以 ...

  9. MySQL使用笔记(六)条件数据记录查询

    By francis_hao    Dec 17,2016 条件数据记录查询 mysql> select field1,field2-- from table_name where 条件; 其中 ...

  10. Kafka自我学习2-Zookeeper cluster

    Test enviroment : zoo1, zoo2, zoo3 cluster 1. Install zookeeper, package in kafka [root@zoo1 ~]# pwd ...