• Codeforces Gym 100418J Lucky tickets 数位DP


    Lucky tickets
    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86686#problem/J

    Description

    Everyone knows that in less than a month the Ice Hockey World Championship will start in Minsk. But few of the guests of our city know that this is the reason why the new tickets and system of accounting were introduced in the public transport (which at the moment is unstable due to some structural flaws). It is obvious that the new tickets require the new formula for determining whether they are lucky or not. It is such an important task that it was given to the authors of BSUIR Open problems. After months of deliberation and disputes the following formula was proposed: a ticket is lucky if it is divisible by the number of symbol 1 in the binary representation. And then the authors thought that the number of these lucky tickets would be too big. Therefore, they asked you for help. Determine the quantity of lucky tickets with numbers from the interval [1..N].

    Input

    The first line contains the integer number N (1 ≤ N ≤ 1019).

    Output

    Output the number of lucky tickets.

    Sample Input

    153

    Sample Output

    42

    HINT

    题意

    给你一个N,然后问你[1,n]内,有多少个数可以被由它转化成的二进制里面的1的个数整除(读起来比较迷,实际上还是比较好理解的

    比如9的二进制形式为1001,但是9%2!=0,所以它不是

    比如8的二进制形式为1000,8%1==0,所以它是

    题解

    数位dp,从高位到低位,mod就直接暴力存一下余数,大致做法和不要62这道题比较像

    搞一搞就好了

    代码:

    #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;
    long long dp[70][70][70][2];
    unsigned long long val[70];
    unsigned long long N;
    int bit[70] , length ;
    unsigned long long MAXBIT;
    
    long long dfs(int x,int y,int z,int w)
    {
        if (x == -1) return (y == 0 && z == MAXBIT);
        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];
        if (z == MAXBIT) ed = 0;
        for(int i = 0 ; i <= ed ; ++ i)
        {
            if (i) // 取1
            {
                ans += dfs(x - 1 , (int)(((unsigned long long)y + val[x]) % MAXBIT), z + 1 ,w | (i < bit[x]));
            }
            else // 取0
            {
                ans += dfs(x - 1 , y, z  ,w | (i < bit[x])) ;
            }
        }
        return ans;
    }
    
    int main(int argc,char *argv[])
    {
      scanf("%I64u",&N);
      for(int i = 0 ; i <= 63 ; ++ i)
      {
          bit[i] = N & 1;
          N >>= 1;
      }
      val[0] = 1;
      for(int i = 1 ; i <= 63 ; ++ i) val[i] = val[i-1] * (unsigned long long ) 2 ;
      for(int i = 63 ; i >= 0 ; -- i) if(bit[i]) {length = i ; break;}
      long long ans = 0;
      for(int i = 1 ; i <= length ; ++ i)
      {
           MAXBIT = i;
           memset(dp,-1,sizeof(dp));
           ans += dfs(length,0,0,0);
      }
      printf("%I64d
    ",ans);
      return 0;
    }
  • 相关阅读:
    Python 命令模式和交互模式
    Python自带IDE设置字体
    Python2.7和3.7区别
    Kubernetes1.91(K8s)安装部署过程(八)-- kubernetes-dashboard安装
    Kubernetes1.91(K8s)安装部署过程(七)--coredns安装
    nginx 设置自签名证书以及设置网址http强制转https访问
    Kubernetes1.91(K8s)安装部署过程(六)--node节点部署
    VMware安装VMware tool是 遇到The path "" is not a valid path to the 3.10.0-693.el7.x86_64 kernel headers.
    第三方git pull免密码更新
    Kubernetes1.91(K8s)安装部署过程(五)--安装flannel网络插件
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4711736.html
Copyright © 2020-2023  润新知