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. 安装oracle XML Database 组件

    近期部署项目数据库,编译包时遇到错误:   PACKAGE CTG.CTG_CSB_COMMON 编译错误  错误:PLS-00201: identifier 'XMLDOM' must be dec ...

  2. FFmpeg在Android使用3

    android 移植ffmpeg后so库的使用   只需要将我们编译好的ffmpeg的so包(在/obj/local/armeabi/libffmpeg.so)copy到所在ndk下的\platfor ...

  3. C#判断用户是否使用微信浏览器,并据此来显示真实内容或二维码

    平时我们看一些网页的时候会发现这样的功能:有的页面只能在微信里访问,如果在电脑上访问就只显示当前地址的二维码.这个用C#怎么实现呢?我们结合代码来看看. 首先,我们需要先判断用户使用的是什么浏览器,这 ...

  4. Oracle 创建和使用视图

    一.what(什么是视图?) 1.视图是一种数据库对象,是从一个或者多个数据表或视图中导出的虚表,视图所对应的数据并不真正地存储在视图中,而是存储在所引用的数据表中,视图的结构和数据是对数据表进行查询 ...

  5. “自私”的Linux

    导读 “如果当时我真的知道从头建立一个操作系统的难度,肯定是不会有勇气去做的.”1991年8月25日,随着林纳斯·托瓦兹(Linus Torvalds)这句“天真”的描述,Linux系统正式与世人见面 ...

  6. Storm入门教程 第五章 一致性事务【转】

    Storm是一个分布式的流处理系统,利用anchor和ack机制保证所有tuple都被成功处理.如果tuple出错,则可以被重传,但是如何保证出错的tuple只被处理一次呢?Storm提供了一套事务性 ...

  7. 使用buildbot实现持续集成(转载)

    转载自:http://www.oschina.net/p/buildbot 使用 Buildot 实现持续集成 使用基于 Python 的工具实现持续集成的理论与实践 牛仔式编码的日子在大多数组织中早 ...

  8. delegate 为什么用 weak属性

    weak指针主要用于“父-子”关系,父亲拥有一个儿子的strong指针,因此是儿子的所有者:但是为了阻止所有权回环,儿子需要使用weak指针指向父亲:你的viewcontroller通过strong指 ...

  9. 【windows核心编程】一个API拦截的例子

    API拦截 修改PE文件导入段中的导入函数地址 为 新的函数地址 这涉及PE文件格式中的导入表和IAT,PE文件中每个隐式链接的DLL对应一个IMAGE_IMPORT_DESCRIPTOR描述符结构, ...

  10. Flume OG 与 Flume NG 的区别

    1.Flume OG:Flume original generation 即Flume 0.9.x版本    Flume NG:Flume next generation ,即Flume 1.x版本 ...