zoukankan      html  css  js  c++  java
  • 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[80][80][2][2] , sum[80],tot= 0;
    int bit[80],length = 0 , LU = 0;
    int LU2[80] ,LU3=0;
    vector<long long>f;
    
    long long dfs(int x,int y,int z , int w)
    {
        if (x == 0) return y;
        if (~dp[x][y][z][w]) return dp[x][y][z][w];
        long long & ans = dp[x][y][z][w] = 0;
        int ed = w ? 1 : bit[x];
        for(int i = 0 ; i <= ed  ; ++ i)
        {
            if (i) 
            {
                if (z == 0)
                 ans += dfs(x-1,y+1,1,w | (i <bit[x]));
            }
            else ans += dfs(x-1,y,0,w | (i<bit[x]));
        }
        return ans;
    }
    
    
    void initiation(long long TN)
    {
        long long x = TN;
        for(int i = f.size() - 1 ; i >= 0 ; -- i)
        {
            if (x >= f[i])
            {
                bit[i] = 1;
                x -= f[i];
                length = max(length,i);
            }
        }
    }
    
    
    void BaoLiLu(long long x)
    {
        for(int i = f.size() - 1 ; i >= 0 ; -- i)
        {
            if (x >= f[i])
            {
                LU2[i] = 1;
                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(1);f.pb(1);
      for(int i = 2 ; ; ++ i)
      {
           f.pb(f[i-1]+f[i-2]);
           if (f[i] > ED) break;
      }
      sum[0] = 0;
      for(int i = 1 ; ; ++ i)
      {
          sum[i] = sum[i-1] + f[i-1]*i;
          if (sum[i] > N)
          {
              LU = i - 1;
              break;
          }
      }
      for(int i=1 ;i<=LU;i++) tot+=f[i-1];
     // cout<<LU<<" "<<sum[LU]<<endl;
      tot+=(N-sum[LU])/(LU+1);
        //cout<<tot<<endl;
      long long DIS = (N-sum[LU]) % (LU+1);
     // for(int i = 1 ; i <= LU ; ++ i) cout << sum[i] << " ";cout << endl;
      memset(dp,-1,sizeof(dp));
      memset(bit,0,sizeof(bit));
      initiation(tot);
      long long OUT = 0;
      //cout << "-----------------------" <<endl;
      //for(int i = length ; i >= 1 ; -- i) cout <<bit[i] << " ";cout <<endl;
      //cout << "-----------------------" <<endl;
      OUT += dfs(length,0,0,0);
    //cout << "Dis is " << DIS << endl;
    //cout <<"tot is " <<tot <<endl;
    //cout << "LU3 is " <<LU3 << endl;
      BaoLiLu(tot+1);
      for(int i = LU3 ; i >= 0 ; -- i)
      {
           if (DIS == 0) break;
         OUT += LU2[i];
         DIS--;
      }
      printf("%I64d
    ",OUT);
      return 0;
    }
  • 相关阅读:
    上传文件到七牛云
    工具类
    SpringBoot 整合 JWT Token
    JWT
    SpringBoot使用RedisTemplate整合Redis
    poj1850 Code
    洛谷P1313 计算系数
    洛谷P1602 Sramoc问题
    —Libre#2009. 「SCOI2015」小凸玩密室
    2014-9-27 NOIP模拟赛
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4713724.html
Copyright © 2011-2022 走看看