P1176奇怪的数列

背景

一天。学军数学小组的成员遇到了一个奇怪的数列,正巧信息小组的你碰到了他们。

于是他们把这个数列展示给你……

描写叙述

这个数列是这种:

0,1,3,2,6,7,5,4,12,13,15,14,10,11,9,8,24,25,27,26,30,31……

先细致研究一下这个数列的规律。

如今他们请你编写一个程序,要求找出数n在此数列中的位置序号k。

格式

输入格式

输入数据仅仅有一行,为数 n (n<=2^31-1)

输出格式

输出数据仅仅有一行,为数k。

例子1

例子输入1[复制]

  1. 5

例子输出1[复制]

  1. 7

限制

时限:1s

来源

From chnlkw,wlfish

a(n) = if n<2 then n else 2*m + (n mod 2 + m mod 2) mod 2, with m=a(floor(n/2)).

  1. #include <map>
  2. #include <set>
  3. #include <ctime>
  4. #include <cmath>
  5. #include <queue>
  6. #include <vector>
  7. #include <cstdio>
  8. #include <cctype>
  9. #include <string>
  10. #include <cstring>
  11. #include <sstream>
  12. #include <cstdlib>
  13. #include <iostream>
  14. #include <algorithm>
  15.  
  16. using namespace std;
  17.  
  18. #define pb push_back
  19. #define mp make_pair
  20. #define fillchar(a, x) memset(a, x, sizeof(a))
  21. #define copy(a, b) memcpy(a, b, sizeof(a))
  22. #define S_queue<P> priority_queue<P, vector<P>,greater<P> >
  23.  
  24. typedef long long LL;
  25. typedef pair<int, int > PII;
  26. typedef unsigned long long uLL;
  27. template<typename T>
  28. void print(T* p, T* q, string Gap = " "){int d = p < q ? 1 : -1;while(p != q){cout << *p;p += d;if(p != q) cout << Gap; }cout << endl;}
  29. template<typename T>
  30. void print(const T &a, string bes = "") {int len = bes.length();if(len >= 2)cout << bes[0] << a << bes[1] << endl;else cout << a << endl;}
  31.  
  32. const int INF = 0x3f3f3f3f;
  33. const int MAXM = 1e6 + 5;
  34. const int MAXN = 1e4 + 5;
  35.  
  36. uLL fun(uLL x){
  37. if(x < 2) return x;
  38. else{
  39. uLL m = fun(floor(x / 2));
  40. return 2 * m + (x % 2 + m % 2) % 2;
  41. }
  42. }
  43.  
  44. int main(){
  45. uLL n;
  46. //freopen("D://out.txt","w",stdout);
  47. scanf("%I64d", &n);
  48. print(fun(n) + 1);
  49. }

vijos - P1176奇怪的数列 (递归 + 找规律)的更多相关文章

  1. 【hdu 6172】Array Challenge(数列、找规律)

    多校10 1002 HDU 6172 Array Challenge 题意 There's an array that is generated by following rule. \(h_0=2, ...

  2. vijos - P1447开关灯泡 (大数模板 + 找规律 + 全然数 + python)

    P1447开关灯泡 Accepted 标签:CSC WorkGroup III[显示标签] 描写叙述 一个房间里有n盏灯泡.一開始都是熄着的,有1到n个时刻.每一个时刻i,我们会将i的倍数的灯泡改变状 ...

  3. HDU 4639 Hehe(字符串处理,斐波纳契数列,找规律)

    题目 //每次for循环的时候总是会忘记最后一段,真是白痴.... //连续的he的个数 种数 //0 1 //1 1 //2 2 //3 3 //4 5 //5 8 //…… …… //斐波纳契数列 ...

  4. Fibonacci数列(找规律)

    题目描述 Fibonacci数列是这样定义的:F[0] = 0F[1] = 1for each i ≥ 2: F[i] = F[i-1] + F[i-2]因此,Fibonacci数列就形如:0, 1, ...

  5. hdu4639 hehe ——斐波纳契数列,找规律

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4639 refer to: http://blog.csdn.net/dongdongzhang_/ar ...

  6. C基础之递归(思想很重要,学会找规律)

    递归思想的条件:1.函数自己调用自己 2.函数必须有一个固定的返回值(如果没有这个条件会发生死循环) ----规律很重要 简单递归题目一: 设计一个函数计算一个整数的n次方,比如2的3次方,就是8 步 ...

  7. CodeForces 450B Jzzhu and Sequences 费波纳茨数列+找规律+负数MOD

    题目:Click here 题意:给定数列满足求f(n)mod(1e9+7). 分析:规律题,找规律,特别注意负数取mod. #include <iostream> #include &l ...

  8. [Swust OJ 666]--初来乍到(题号都这么溜~~,递归,找规律)

    题目链接:http://acm.swust.edu.cn/problem/0666/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  9. HDU 1165 Eddy's research II(给出递归公式,然后找规律)

    - Eddy's research II Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. String值传递剖析

    转载自 http://www.iteye.com/topic/412531 提要:本文从实现原理的角度上阐述和剖析了:在Java语言中,以String作为类型的变量在作为方法参数时所表现出的“非对象” ...

  2. ajax 不执行

    1.get形式访问: 一个相同的URL 只有一个结果,所以 第二次访问的时候 如果 URL字符串没变化 浏览器是 直接拿出了第一次访问的结果,post则不会 解决办法: 1.url+new Date( ...

  3. SFTP使用key文件登录

    命令: sftp -oPort= -oIdentityFile=/root/.ssh/user.priv user@39.39.100.100 命令选项: -o ssh_option Can be u ...

  4. WPF 内存释放

    原文:WPF 内存释放 在WPF 项目中引用到Transitionals 实现图片转场特效,发现每次图片转场内存都会增加,在每次转场后进行内存压缩 记录 解决方案如下: GC.Collect(); G ...

  5. POJ 1975 Median Weight Bead

    Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Orig ...

  6. 数据结构之---C语言实现拓扑排序AOV图

    //有向图的拓扑排序 //杨鑫 #include <stdio.h> #include <stdlib.h> #include <string.h> #define ...

  7. MySql基础总结(1)

    对于MySql已经是第二次看了,这次基本的目的是将MySql系统的归纳一边,之前学MySql是将笔记写在本子上,以后不想总带着本子,所以再花点时间把笔记归纳在博客中. 下午已将安装步骤具体的写了.如今 ...

  8. mysql-用正则表达式进行搜索

    正则表达式的作用是匹配文本,将一个模式(正则表达式)与一个文本串进行比较,mysql允许你指定正则表达式,过滤select检索出的数据.但是mysql仅仅支持正则表达式的一个子集. 1.基本字符匹配: ...

  9. [poj 2480] Longge's problem 解题报告 (欧拉函数)

    题目链接:http://poj.org/problem?id=2480 题目大意: 题解: 我一直很欣赏数学题完美的复杂度 #include<cstring> #include<al ...

  10. BZOJ 1927 最小费用流问题

    From lydrainbowcat //By SiriusRen #include <queue> #include <cstdio> #include <cstrin ...