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;
}
原文地址:https://www.cnblogs.com/qscqesze/p/4711736.html