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 ...
随机推荐
- Android Message和obtainMessage的区别
类概述 定义一个包含任意类型的描述数据对象,此对象可以发送给Handler.对象包含两个额外的int字段和一个额外的对象字段,这样可以使得在很多情况下不用做分配工作. 尽管Message的构造器是公开 ...
- C#中嵌入互操作类型的含义
首先说一下它的含义: 1. ”嵌入互操作类型”中的嵌入就是引进.导入的意思,类似于c#中using,c中include的作用,目的是告诉编译器是否要把互操作类型引入. 2. “互操作类型”实际是指一系 ...
- poj 3311 Hie with the Pie
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
- MFC ListControl用法
http://blog.csdn.net/lovton/article/details/6527208 1.建立一个对象m_LogList 步骤:在对话listcontrol控件右键点击添加变量-&g ...
- C++ static内容小结
C++中static总结比较好的博客:http://blog.csdn.net/laixingjun/article/details/9139839 http://blog.csdn.net/xiaj ...
- ORA-12162: TNS:net service name is incorrectly specified
今天在进行修改oracle_sid环境变量的时候,将相关的环境变量值去掉,从而不能进入sqlplus,报错如下: [oracle@kel ~]$ sqlplus / as sysdba SQL*Plu ...
- Java IO流中的File类学习总结
一.File类概述 File类位于java.io包中,是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹. File类有多种重载的构造方法.File类保存文件或目录的各种 ...
- java的动态代理机制
前几天看到java的动态代理机制,不知道是啥玩意,然后看了看.死活不知道 invoke(Object proxy, Method m, Object[] args)种的proxy是个什么东西,放在这里 ...
- SQL Server 中的三种分页方式
USE tempdb GO SET NOCOUNT ON --创建表结构 IF OBJECT_ID(N'ClassB', N'U') IS NOT NULL DROP TABLE ClassB GO ...
- Unity3D文件读写
这里主要是简单的文件读写,不推荐使用,最好用的还是PlayerPrefs. using UnityEngine; using System.Collections; using System.IO; ...