Problem F. Fibonacci System
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86821#problem/B

Description

Little John studies numeral systems. After learning all about fixed-base systems, he became interested in more unusual cases. Among those cases he found a Fibonacci system, which represents all natural numbers in an unique way using only two digits: zero and one. But unlike usual binary scale of notation, in the Fibonacci system you are not allowed to place two 1s in adjacent positions. One can prove that if you have number N = anan−1 . . . a1F in Fibonacci system, its value is equal to N = an · Fn + an−1 · Fn−1 + . . . + a1 · F1, where Fk is a usual Fibonacci sequence defined by F0 = F1 = 1, Fi = Fi−1 + Fi−2. For example, first few natural numbers have the following unique representations in Fibonacci system: 1 = 1F 2 = 10F 3 = 100F 4 = 101F 5 = 1000F 6 = 1001F 7 = 1010F John wrote a very long string (consider it infinite) consisting of consecutive representations of natural numbers in Fibonacci system. For example, the first few digits of this string are 110100101100010011010. . . He is very interested, how many times the digit 1 occurs in the N-th prefix of the string. Remember that the N-th prefix of the string is just a string consisting of its first N characters. Write a program which determines how many times the digit 1 occurs in N-th prefix of John’s string.

Input

The input file contains a single integer N (0 ≤ N ≤ 1015).

Output

Output a single integer — the number of 1s in N-th prefix of John’s string

Sample Input

21

Sample Output

10

HINT

题意

把数转化成费布拉奇数之后,然后接在一起,然后问你前n位有多少个1

题解

正解大概是贪心找规律什么的

我们是数位dp,直接处理出前n个费布拉奇数的1的个数,然后暴力出最后一个费布拉奇数的1的个数

代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <iomanip>
#include <string>
#include <ctime>
#include <list>
typedef unsigned char byte;
#define pb push_back
#define input_fast std::ios::sync_with_stdio(false);std::cin.tie(0)
#define local freopen("in.txt","r",stdin)
#define pi acos(-1) using namespace std;
const long long ED = 1e15;
long long N , dp[][][][] , sum[],tot= ;
int bit[],length = , LU = ;
int LU2[] ,LU3=;
vector<long long>f; long long dfs(int x,int y,int z , int w)
{
if (x == ) return y;
if (~dp[x][y][z][w]) return dp[x][y][z][w];
long long & ans = dp[x][y][z][w] = ;
int ed = w ? : bit[x];
for(int i = ; i <= ed ; ++ i)
{
if (i)
{
if (z == )
ans += dfs(x-,y+,,w | (i <bit[x]));
}
else ans += dfs(x-,y,,w | (i<bit[x]));
}
return ans;
} void initiation(long long TN)
{
long long x = TN;
for(int i = f.size() - ; i >= ; -- i)
{
if (x >= f[i])
{
bit[i] = ;
x -= f[i];
length = max(length,i);
}
}
} void BaoLiLu(long long x)
{
for(int i = f.size() - ; i >= ; -- i)
{
if (x >= f[i])
{
LU2[i] = ;
x -= f[i];
LU3 = max(i,LU3);
}
}
} int main(int argc,char *argv[])
{
freopen("fibonacci.in","r",stdin);
freopen("fibonacci.out","w",stdout);
//local;
scanf("%I64d",&N);
f.pb();f.pb();
for(int i = ; ; ++ i)
{
f.pb(f[i-]+f[i-]);
if (f[i] > ED) break;
}
sum[] = ;
for(int i = ; ; ++ i)
{
sum[i] = sum[i-] + f[i-]*i;
if (sum[i] > N)
{
LU = i - ;
break;
}
}
for(int i= ;i<=LU;i++) tot+=f[i-];
// cout<<LU<<" "<<sum[LU]<<endl;
tot+=(N-sum[LU])/(LU+);
//cout<<tot<<endl;
long long DIS = (N-sum[LU]) % (LU+);
// for(int i = 1 ; i <= LU ; ++ i) cout << sum[i] << " ";cout << endl;
memset(dp,-,sizeof(dp));
memset(bit,,sizeof(bit));
initiation(tot);
long long OUT = ;
//cout << "-----------------------" <<endl;
//for(int i = length ; i >= 1 ; -- i) cout <<bit[i] << " ";cout <<endl;
//cout << "-----------------------" <<endl;
OUT += dfs(length,,,);
//cout << "Dis is " << DIS << endl;
//cout <<"tot is " <<tot <<endl;
//cout << "LU3 is " <<LU3 << endl;
BaoLiLu(tot+);
for(int i = LU3 ; i >= ; -- i)
{
if (DIS == ) break;
OUT += LU2[i];
DIS--;
}
printf("%I64d\n",OUT);
return ;
}

Codeforces Gym 100286F Problem F. Fibonacci System 数位DP的更多相关文章

  1. Codeforces Gym 100500F Problem F. Door Lock 二分

    Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...

  2. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...

  3. Codeforces Beta Round #51 D. Beautiful numbers 数位dp

    D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...

  4. 【hdu4734】【F(x)】数位dp + 小小的总结一下

    (https://www.pixiv.net/member_illust.php?mode=medium&illust_id=65608478) Problem Description For ...

  5. Codeforces Gym100623J:Just Too Lucky(数位DP)

    http://codeforces.com/gym/100623/attachments 题意:问1到n里面有多少个数满足:本身被其各个数位加起来的和整除.例如120 % 3 == 0,111 % 3 ...

  6. CodeForces - 1245F Daniel and Spring Cleaning (数位DP)

    While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it ...

  7. HDU - 4734 F(x) (数位dp)

    For a decimal number x with n digits (A nA n-1A n-2 ... A 2A 1), we define its weight as F(x) = A n  ...

  8. HDU - 4389 X mod f(x)(数位dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...

  9. 题解——HDU 4734 F(x) (数位DP)

    这道题还是关于数位DP的板子题 数位DP有一个显著的特征,就是求的东西大概率与输入关系不大,理论上一般都是数的构成规律 然后这题就是算一个\( F(A) \)的公式值,然后求\( \left [ 0 ...

随机推荐

  1. Java—Map.Entry

    Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法. keySet()方法返回值是Map中key值的集合:e ...

  2. hdu 1198 Farm Irrigation

    令人蛋疼的并查集…… 我居然做了大量的枚举,居然过了,我越来越佩服自己了 这个题有些像一个叫做“水管工”的游戏.给你一个m*n的图,每个单位可以有11种选择,然后相邻两个图只有都和对方连接,才判断他们 ...

  3. hdu1896 bjfu1268 水题

    很简单的模拟,我是用的优先队列.不多说,上代码(这是bjfuoj的,hdu的要稍改一下): /* * Author : ben */ #include <cstdio> #include ...

  4. bjfu1211 推公式,筛素数

    题目是求fun(n)的值 fun(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])C[n][k] ...

  5. php里面为什么header之前有输出报错 源码分析

    众所周知,php 里面 header之前有输出的话,会报错,例如下面这样   就这个错误,我们开始查阅php源代码,到底是怎样做的,至于php源代码分析,安装,和调试时怎样配置的,我会专门写一篇文章去 ...

  6. 对FileUpload文件上传控件的一些使用方法说明

    //创建时间:2014-03-12 //创建人:幽林孤狼 //说明:FileUpload文件上传控件使用说明(只是部分)已共享学习为主 //可以上传图片,txt文档.doc,wps,还有音频文件,视屏 ...

  7. 现代程序设计 homework-05

    本次作业要求设计服务器和客户端,由于之前对网络编程是一窍不通,上上节课听宗学长讲述Tcp的时候心里想这个东西还真是高大上啊一点儿都听不懂,但是上个周末看了看C#网络编程的博客和书之后,发现这个东西入门 ...

  8. 修复Debian(Ubuntu)Grub2 引导

    重装win7, 之前的系统debian 的引导就没有了. 而debian 的盘似乎没有ubuntu的livecd模式,于是用ultraISO将ubuntu的ios文件写入到u盘中. boot时选择启动 ...

  9. rsync 无密码传输文件

    最近机器迁移,需要备份文件,但各个机器间不能穿梭,即无法通过scp来传输文件, 在运维的建议下,选用了rsync作为传输的工具. 默认情况Ubuntu安装了rsync服务,但在/etc下没有配置文件, ...

  10. (转载)Java之外观模式(Facade Pattern)

    1.概念 为子系统中的一组接口提供一个统一接口.Facade模式定义了一个高层接口,这个接口使得这子系统更容易使用. 2.UML 3.代码 下面是一个具体案例的代码: package facade; ...