HDU 5175

我想了很久了,后来还是把N分解质因数,枚举各种组合,反正也不多吧,按题目条件,然后就过了。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define LL __int64
using namespace std; 

LL prime[100010],np;
int fac[100010],nf;
int cnt,nop;
struct Node{
	LL f;
	int cnt;
}node[100100];
LL ans[100010];

void predo(LL n){
	np=0;
	bool isprime[100010];
	memset(isprime,false,sizeof(isprime));
	for(LL i=2;i<=n;i++){
		if(!isprime[i]){
			isprime[i]=true;
			prime[np++]=i;
			for(LL j=i*i;j<=n;j+=i)
			isprime[j]=true;
		}
	}
}

bool Isprime(LL n){
	nf=0;
	for(LL i=0;i<np&&prime[i]<=n;i++){
		if(n%prime[i]==0){
			while(n%prime[i]==0){
				fac[nf++]=prime[i];
				n/=prime[i];
			}
		}
	}
	if(n!=1){
		fac[nf++]=n;
	}
	if(nf<=1) return true;
	return false;
}

LL GCD(LL a,LL b){
	if(b==0) return a;
	return GCD(b,a%b);
}

void dfs(LL n,int pos,LL gcd){
	if(pos==nop+1){
		LL x=n^gcd;
		if(x>=n) return ;
		if(x==0) return ;
		if(gcd==GCD(n,x))
		ans[cnt++]=x;
		return ;
	}
	for(int i=0;i<=node[pos].cnt;i++){
		if(i==0){
			dfs(n,pos+1,gcd);
		}
		else{
			gcd*=node[pos].f;
			dfs(n,pos+1,gcd);
		}
	}
}

int main(){
	int t=0;
	LL n;
	predo(100010LL);
	while(scanf("%I64d",&n)!=EOF){
		if(n==1LL||n==2){
			printf("Case #%d:\n",++t);
			puts("0");
			puts("");
			continue;
		}
		if(Isprime(n)){
			printf("Case #%d:\n",++t);
			puts("1");
			printf("%I64d\n",n-1);
		}
		else{
			nop=-1;
			for(int i=0;i<nf;i++){
				if(i==0||fac[i]!=fac[i-1]){
					node[++nop].f=fac[i];
					node[nop].cnt=1;
				}
				else
				node[nop].cnt++;
			}

			cnt=0;
			dfs(n,0,1LL);
			sort(ans,ans+cnt);
			printf("Case #%d:\n",++t);
			printf("%d\n",cnt);
			if(cnt==0){
				puts("");
				continue;
			}
			printf("%I64d",ans[0]);
			for(int i=1;i<cnt;i++){
				printf(" %I64d",ans[i]);
			}
			printf("\n");
		}
	}
	return 0;
}

  

时间: 2024-10-13 04:47:16

HDU 5175的相关文章

HDU 5175 Misaki&#39;s Kiss again (异或运算,公式变形)

Misaki's Kiss again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 201    Accepted Submission(s): 57 Problem Description After the Ferries Wheel, many friends hope to receive the Misaki's kiss

hdu 5175 Misaki&#39;s Kiss again(GCD和异或)

题意: 给一个数N. 如果GCD(N,M) = N XOR M,则称M是一个kiss   1<=M<=N 问总共有多少个kiss.并且列出所有的值. 思路: 思路一:枚举M.有大量的GCD(N,M)值相等.但是N XOR M=X.当X是定值是,M一定只有一个.所以这个方法明显有大量重复. 发现知道GCD(N,M)的值,可直接求出M.搞定. 令gcd(n,m)=d,可知道d是n的约数 有d=(n xor m)=(m xor n),所以m=(d xor n). 可发现gcd(n,(d xor n)

HDU 5175 Misaki&#39;s Kiss again(数学,暴力枚举)

题目大意: 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<=

HDU 5175 Misaki&#39;s Kiss again

Misaki's Kiss again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 513    Accepted Submission(s): 121 Problem Description After the Ferries Wheel, many friends hope to receive the Misaki's kiss

个人代码问题以及总结

1.二进制枚举超限 不能超过50.总结,可以采取其他枚举子集的方法,不过如果子集数过多的话,说明应该换一种思路了 2.if语句的前后对应 例题1:hdu 5175 //循环体{ m=(n^i); if(m<=0||m>n) continue; //错误在这里,本来希望判断(n^i)在不在1到n之间,却直接把接下来待判断的n^(n/i)判断漏掉了,直接跳过了这次循环 if(kgcd(n,m)==i) nt[k++]=m; if(i*i==n) continue; m=n^(n/i); if(m&

BestCoder Valentine&#39;s Day Round

昨晚在开赛前5分钟注册的,然后比赛刚开始就掉线我就不想说了(蹭网的下场……),只好用手机来看题和提交,代码用电脑打好再拉进手机的(是在傻傻地用手机打了一半后才想到的办法). 1001,也就是 hdu 5174,题意很难叙述了,自己看题吧,这题有数据溢出的风险,我竟然是AC了一发才发觉的(只过了小数据),幸好后来改后赶紧再交一遍才不至于被人hack,因为需要对数据去重,我不想用数组模拟,便尝试下用 map了,可是我的奇葩 map ( ~′⌒`~),连我自己都感到无语了: 1 #include<cs

HDU 5179 beautiful number 离线处理

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5179 beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 176    Accepted Submission(s): 104 Problem Description Let A=∑ni=1ai?10n?i(1≤

Valentine&#39;s Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 221    Accepted Submission(s): 91 Problem Description A girl named Gorwin and a boy named Vivin is a couple. They arriv

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;