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. 【转】个人常用iOS第三方库以及XCode插件介绍 -- 不错

    原文网址:http://adad184.com/2015/07/08/my-favorite-libraries-and-plugins/ 第三方库是现在的程序员离不开的东西 不光是APP开发 基本上 ...

  2. Dropping water balloons

    题意: 给你k个水球n层楼(n很大) 现在做实验在楼上向下丢水球,若水球没破可以重新丢,求把所有水球弄破的最小试验次数. 分析: 开始完全没思路啊.从正面求没法做不会表示状态,做实验是只能从第一层,一 ...

  3. duilib中的V和H布局中滚动条问题

    转自博客:http://blog.csdn.net/damingg/article/details/41149037 首先看一段xml代码 [html] view plaincopy <?xml ...

  4. 【STL】帮你复习STL泛型算法 一

    STL泛型算法 #include <iostream> #include <vector> #include <algorithm> #include <it ...

  5. C++ 编程第二章小结

    switch()用法的注意事项 1:switch语句中的表达式只能是整形数据,字符型数据和枚举型数据,case后面的产量表达式的类型必须与switch括号后面的类型相匹配 2:各个case(包括def ...

  6. 【原】Kryo序列化篇

    Kryo是一个快速有效的对象图序列化Java库.它的目标是快速.高效.易使用.该项目适用于对象持久化到文件或数据库中或通过网络传输.Kryo还可以自动实现深浅的拷贝/克隆. 就是直接复制一个对象对象到 ...

  7. cxf 动态创建客户端,局域网能正常调用服务端,外网不能访问

  8. 过度拟合(overfitting)

    我们之前解决过一个理论问题:机器学习能不能起作用?现在来解决另一个理论问题:过度拟合. 正如之前我们看到的,很多时候我们必须进行nonlinear transform.但是我们又无法确定Q的值.Q过小 ...

  9. java BigInteger类的用法

    import java.math.BigInteger; Scanner in = new Scanner(System.in); BigInteger x1 = new BigInteger(&qu ...

  10. homework09-虐心的现程设终于要告一段落了

    V3.0版本今天凌晨出炉 添加了随机生成 添加了文件打开 完全按照老师的要求搞定了 V2.0版本更新 添加了中间数组变量显示 这次作业写了整整一天,把以前能用的代码都改了一个遍 最后变成了网页版的小程 ...