Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
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的更多相关文章
- Codeforces Gym 100500F Problem F. Door Lock 二分
Problem F. Door LockTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/at ...
- Codeforces Gym 100002 Problem F "Folding" 区间DP
Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...
- 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 ...
- 【hdu4734】【F(x)】数位dp + 小小的总结一下
(https://www.pixiv.net/member_illust.php?mode=medium&illust_id=65608478) Problem Description For ...
- Codeforces Gym100623J:Just Too Lucky(数位DP)
http://codeforces.com/gym/100623/attachments 题意:问1到n里面有多少个数满足:本身被其各个数位加起来的和整除.例如120 % 3 == 0,111 % 3 ...
- CodeForces - 1245F Daniel and Spring Cleaning (数位DP)
While doing some spring cleaning, Daniel found an old calculator that he loves so much. However, it ...
- 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 ...
- HDU - 4389 X mod f(x)(数位dp)
http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...
- 题解——HDU 4734 F(x) (数位DP)
这道题还是关于数位DP的板子题 数位DP有一个显著的特征,就是求的东西大概率与输入关系不大,理论上一般都是数的构成规律 然后这题就是算一个\( F(A) \)的公式值,然后求\( \left [ 0 ...
随机推荐
- 常见的js函数
改变元素的样式 var changeStyle = function(elem,name,value){ elem.style[name] = value; } 空位补零 fu ...
- 如何合并IP网段
1. 安装IPy pip3 install IPy 2. 写脚本: yuyue@workplace:~ $ cat combine_ip.pyfrom IPy import IPSet, IPimpo ...
- HDU 5430 Reflect
题意:问在一个圆形的镜面里,从任意一点发出一个光源,经n次反射回到起点的情况数是多少. 解法:直接贴题解吧…… 求1至N+1中与N+1互质的个数,即欧拉函数. 代码: #include<stdi ...
- CPC23-4 K.喵喵的神·数
题意:给出整数T,P,求c(T,P) mod P. 解法:用卢卡斯定理. 卢卡斯定理:解决c(n,m) mod p问题.Lucas(n,m,p)=c(n%p,m%p)*Lucas(n/p,m/p,p) ...
- MultiSet
Guava引进了JDK里没有的,但是非常有用的一些新的集合类型.所有这些新集合类型都能和JDK里的集合平滑集成.Guava集合非常精准地实现了JDK定义的接口.Guava中定义的新集合有: Multi ...
- Redis常用命令手册:服务器相关命令
Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用.在编程时,比如各类语言包,这些命令都有对应的方法.下面将Redis提供的命令做一 ...
- MFC ListControl用法
http://blog.csdn.net/lovton/article/details/6527208 1.建立一个对象m_LogList 步骤:在对话listcontrol控件右键点击添加变量-&g ...
- jenkins 命令行 CLI jenkins-cli.jar
部署好jenkins后,一般都是通过jenkins提供的web界面来操作jenkins. 而有些场景则需要通过命令来操作jenkins,例如通过脚本操作jenkins. 在jenkins提供的web界 ...
- iOS数据存储之属性列表理解
iOS数据存储之属性列表理解 数据存储简介 数据存储,即数据持久化,是指以何种方式保存应用程序的数据. 我的理解是,开发了一款应用之后,应用在内存中运行时会产生很多数据,这些数据在程序运行时和程序一起 ...
- redo buffer大小
转载自http://blog.csdn.net/robinson1988/article/details/4729858 log buffer 是SGA中一块循环使用的内存区域,它一般很小,因为有4个 ...