POJ 1392 Ouroboros Snake(数位欧拉)

题目链接:http://poj.org/problem?id=1392

题目大意:题意看的我头痛,其实跟HDU2894差不多,但是这题要求输出这条路径上第k个数,而不是输出路径。

解题思路:也跟HDU2894差不多。。。。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define CLR(arr,val)  memset(arr,val,sizeof(arr))
 5 using namespace std;
 6 const int N=16;
 7
 8 int n,k,cnt;
 9 int ans[1<<N];
10 bool vis[1<<N];
11
12 void euler(int st) {
13     int s1=(st<<1)&((1<<n)-1);
14     int s2=s1+1;
15     //先试着添加0,再尝试添加1
16     if (!vis[s1]){
17         vis[s1]=1;
18         euler(s1);
19         ans[++cnt]=0;
20     }
21     if (!vis[s2]) {
22         vis[s2]=1;
23         euler(s2);
24         ans[++cnt]=1;
25     }
26 }
27
28 void init(){
29     CLR(vis,false);
30     CLR(ans,0);
31     cnt=0;
32 }
33
34 int main(){
35     while(~scanf("%d%d",&n,&k)&&(n||k)){
36         init();
37         euler(0);
38         cnt+=n-1;            //cnt此时等于2^n还要补上开头0的n-1位
39         cnt-=k;                //定位到第k个数
40         int t=0;
41         for(int i=0;i<n;i++){
42             t=t*2+ans[cnt-i];
43         }
44         printf("%d\n",t);
45     }
46     return 0;
47 }
时间: 2024-08-01 23:08:20

POJ 1392 Ouroboros Snake(数位欧拉)的相关文章

POJ - 1392 Ouroboros Snake (欧拉回路的应用)

Description Ouroboros is a mythical snake from ancient Egypt. It has its tail in its mouth and continously devours itself. The Ouroboros numbers are binary numbers of 2^n bits that have the property of "generating" the whole set of numbers from

POJ 1392 Ouroboros Snake (欧拉回路)

题目地址:poj1392 欧拉回路水题. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h> using nam

POJ 3090 Visible Lattice Points 欧拉函数

链接:http://poj.org/problem?id=3090 题意:在坐标系中,从横纵坐标 0 ≤ x, y ≤ N中的点中选择点,并且这些点与(0,0)的连点不经过其他的点. 思路:显而易见,x与y只有互质的情况下才会发生(0,0)与(x,y)交点不经过其他的点的情况,对于x,y等于N时,可以选择的点均为小于等于N并且与N互质的数,共Euler(N)个,并且不重叠.所以可以得到递推公式aa[i]=aa[i]+2*Euler(N). 代码: #include <iostream> #in

poj 3090 &amp;&amp; poj 2478(法雷级数,欧拉函数)

http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式很简单:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2*f[i]-1. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <stack> #include <vector> #include

poj 2480 Longge&#39;s problem [ 欧拉函数 ]

传送门 Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7327   Accepted: 2416 Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms.

poj 2773 Happy 2006(欧拉函数应用)

http://poj.org/problem?id=2773 题意:输入n,k,求与n不互素的第k个数,k可能大于n. 思路:以n=6为例,与6互素的数有一定规律.{1,5},{7,12},{13,18}......,发现在[1,n],[n+1,n*2]......[m*n+1,(m+1)*n]区间内素数个数相同,且对应位置的数都相差n的整数倍.因此只要求出[1,n]内的与n互素的数即可.这个过程没必要一个一个枚举,可以用欧拉函数解决.因为欧拉函数已经求出了n的所有质因子,与n不互素的数都与n有

POJ 2513 Colored Sticks(欧拉道路+字典树+并查集)

http://poj.org/problem?id=2513 题意: 给定一些木棒,木棒两端都涂上颜色,求是否能将木棒首尾相接,连成一条直线,要求不同木棒相接的一边必须是相同颜色的. 思路: 题目很明显的是欧拉道路的问题. 欧拉道路的关键是: ①图是连通的. ②最多只能有两个奇点.(不能只存在一个奇点) 本来是想用map映射的,但是太多了,比较费时,这里用字典树的话会比较省时,判断图是否连通可以用并查集来完成. 1 #include<iostream> 2 #include<algori

poj 2478 Farey Sequence(欧拉函数)

Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13204   Accepted: 5181 Description The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbers a/b with 0 < a < b <= n and gcd(a,b)

POJ 2478 Farey Sequence(欧拉函数前n项和)

题意 求欧拉函数的前n项和 水题 打表筛选即可 #include <iostream> #include <math.h> using namespace std; long long a[1000005]={0}; long long c[1000005]={0}; void enlur() { int i,j; for(i=2;i<1000005;i++) { if(!a[i]) { for(j=i;j<1000005;j=j+i) { if(!a[j]) a[j]