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[][][][];
unsigned long long val[];
unsigned long long N;
int bit[] , length ;
unsigned long long MAXBIT; long long dfs(int x,int y,int z,int w)
{
if (x == -) return (y == && z == MAXBIT);
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];
if (z == MAXBIT) ed = ;
for(int i = ; i <= ed ; ++ i)
{
if (i) // 取1
{
ans += dfs(x - , (int)(((unsigned long long)y + val[x]) % MAXBIT), z + ,w | (i < bit[x]));
}
else // 取0
{
ans += dfs(x - , y, z ,w | (i < bit[x])) ;
}
}
return ans;
} int main(int argc,char *argv[])
{
scanf("%I64u",&N);
for(int i = ; i <= ; ++ i)
{
bit[i] = N & ;
N >>= ;
}
val[] = ;
for(int i = ; i <= ; ++ i) val[i] = val[i-] * (unsigned long long ) ;
for(int i = ; i >= ; -- i) if(bit[i]) {length = i ; break;}
long long ans = ;
for(int i = ; i <= length ; ++ i)
{
MAXBIT = i;
memset(dp,-,sizeof(dp));
ans += dfs(length,,,);
}
printf("%I64d\n",ans);
return ;
}

Codeforces Gym 100418J Lucky tickets 数位DP的更多相关文章

  1. Gym 100418J Lucky tickets(数位dp)

    题意:给定一个n.求区间[1, n]之间的全部的a的个数.a满足: a能整除  把a表示自身二进制以后1的个数 思路:题意非常绕.... 数位dp,对于全部可能的1的个数我们都dfs一次 对于某一个可 ...

  2. Codeforces Gym100623J:Just Too Lucky(数位DP)

    http://codeforces.com/gym/100623/attachments 题意:问1到n里面有多少个数满足:本身被其各个数位加起来的和整除.例如120 % 3 == 0,111 % 3 ...

  3. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  4. CodeForces 55D Beautiful numbers(数位dp+数学)

    题目链接:http://codeforces.com/problemset/problem/55/D 题意:一个美丽数就是可以被它的每一位的数字整除的数. 给定一个区间,求美丽数的个数. 显然这是一道 ...

  5. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  6. Codeforces 1290F - Making Shapes(数位 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 数位 dp 好题. 首先,由于是凸包,一但向量集合确定,凸包的形态肯定就已经确定了.考虑什么样的向量集合能够组成符合条件的凸包,我们假设第 ...

  7. CodeForces 55D Beautiful numbers(数位dp)

    数位dp,三个状态,dp[i][j][k],i状态表示位数,j状态表示各个位上数的最小公倍数,k状态表示余数 其中j共有48种状态,最大的是2520,所以状态k最多有2520个状态. #include ...

  8. CodeForces 628 D Magic Numbers 数位DP

    Magic Numbers 题意: 题意比较难读:首先对于一个串来说, 如果他是d-串, 那么他的第偶数个字符都是是d,第奇数个字符都不是d. 然后求[L, R]里面的多少个数是d-串,且是m的倍数. ...

  9. codeforces 401D. Roman and Numbers 数位dp

    题目链接 给出一个<1e18的数, 求将他的各个位的数字交换后, 能整除m的数的个数. 用状态压缩记录哪个位置的数字已经被使用了, 具体看代码. #include<bits/stdc++. ...

随机推荐

  1. 【转】如何在IOS中使用3D UI - CALayer的透视投影

    原文网址:http://www.tairan.com/archives/2041/ 例子代码可以在 http://www.tairan.com/thread-3607-1-1.html 下载 iOS的 ...

  2. Tombstone crash

    首先,android平台应用程序可能产生以下四种crash:App层:Force close crashANR crashNative层:Tombstone crashKernel层:Kernel p ...

  3. http://blog.csdn.net/jbb0403/article/details/42102527

    http://blog.csdn.net/jbb0403/article/details/42102527

  4. 《Python 学习手册4th》 第十二章 if测试和语法规则

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  5. 为Fitnesse-20140630定制RestFixture代码

    摘要:Fitnesse插件RestFixture在最新版Fitnesse输出测试结果为html文本,而非html.本博文记录RestFixture定制代码的过程. 准备开发环境 假定你已经正确安装JD ...

  6. SQL你必须知道的-查询聚合分组排序

    use MySchoolTwo    -- 简单查询    select * from Student    -- 话说这种查询的效率要比 * 要高级点    select sId , sName , ...

  7. html在图片上实现下雨效果

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  8. 【原】Storm Local模式和生产环境中Topology运行配置

    Storm入门教程 1. Storm基础 Storm Storm主要特点 Storm基本概念 Storm调度器 Storm配置 Guaranteeing Message Processing(消息处理 ...

  9. Kindle Paperwhite 2使用体验

    博客开通后一懒就扔下了几十天,着实自惭.鉴于是第一篇,先说点题外话. 一转眼读研的生活已经过去一年有余.曾经的同学已经在职场拼搏,同龄人的生活状态也自然地带给自己一份紧迫感:不敢再贪恋校园生活的安逸, ...

  10. Memory Cache(内存缓存)

    当Google测试了Google Search服务的可用性后,发现速度是最影响Web应用的可用性的因素之一.相对于作用相同但是速度慢的应用,用户更喜欢速度快的应用.多来年,Google已经掌握了如何使 ...