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 ;
}