codeforces gym100418J
题目简述
给定N 求1到N中有多少个幸运数字 幸运数字的定义为 这个数能被它二进制表示下1的个数整除
其中(1 ≤ N ≤ 1019)
--------------------------------------------------------------------------------------------------------------------------------
第二道数位DP题 在这里感谢一下 Gatevin 学长的讲解帮我克服了对数位DP的畏惧
这题我的做法和前面我写的那篇 windy数 的题解的思路差不多的
先从最低位到最高位处理一下只有当前位(当前位之前假设都是前导0)有限定的时候的方案数
( f数组的四位分别是 当前位是哪一位 这一位是0还是1 现在的集合中有几个1 现在的集合中的数modx的值)
然后再从最高位到最低位扫一遍即可
#include <bits/stdc++.h>
using namespace std;
const int N=;
unsigned long long n;
int lim[N];
unsigned long long f[N][][N][N];
int top;
void prepare(int x)
{
memset(f,,sizeof(f));
f[][][][]=;
f[][][][%x]=;
for(int i=;i<top-;++i)
for(int j=;j<=x;++j)
for(int k=;k<x;++k)
{
f[i+][][j][k]+=f[i][][j][k];
f[i+][][j][k]+=f[i][][j][k];
f[i+][][j+][(k+(1ULL<<i+))%x]+=f[i][][j][k];
f[i+][][j+][(k+(1ULL<<i+))%x]+=f[i][][j][k];
}
}
unsigned long long check(int x)
{
unsigned long long re=;
int tmp=,cnt=;
for(int i=top-;i>=;--i)
if(lim[i])
{
if(x-cnt>=)
re+=f[i][][x-cnt][(x-tmp)%x];
tmp=((1ULL<<i)+tmp)%x;
++cnt;
}
return re+(cnt==x&&!tmp);
}
int main()
{
scanf("%llu",&n);
while(n)
{
lim[top++]=n&;
n>>=;
}
unsigned long long ans=;
for(int i=;i<=top;++i)
{
prepare(i);
ans+=check(i);
}
printf("%llu",ans);
return ;
}
codeforces gym100418J的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
随机推荐
- 继承Process类,另一种方法计算累加和以及阶乘
#定义一个类 继承Process类 from multiprocessing import Process import os import time class jiecheng(Process): ...
- MYSQL学习笔记/2019
以下内容均为转载,只是方便今后学习:如有不妥,请联系删除:特此感谢原博主精心制作!(文章底部贴有原文链接) /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ m ...
- 最长公共上升子序列 (LIS+LCS+记录)
[题目描述] 给出两个序列,求出最长公共上升子序列的长度,并输出其中一个解. [题目链接] http://noi.openjudge.cn/ch0206/2000/ [算法] 经典问题,结合了LIS和 ...
- 自己做的html5手机站点
这个站点採用html5+css3+jquerymobile 仅仅开发了前台的功能,前台的界面比較美观,后端不方便放上,各位有空能够自己开发这个站点的界面有參考各个菜谱站点的页面的样式,可是又不一样,是 ...
- Linux磁盘分区实例演示
一直觉得Linux的分区操作很高大上,之前在物理机上装的Linux,所以不敢随便乱来,今天重回Windows,下面利用Windows上的Linux虚拟机实际操作一次磁盘分区,要求:1G的硬盘分成2个2 ...
- Leetcode Lect3 二分法总结
二分法模板 非递归版本: public class Solution { /** * @param A an integer array sorted in ascending order * @pa ...
- StackExchange.Redis 使用LuaScript脚本模糊查询hash
原文:StackExchange.Redis 使用LuaScript脚本模糊查询hash 获取redis连接 public class RedisHelper { private static rea ...
- 构建自己的AngularJS,第一部分:作用域和digest 转摘:http://www.ituring.com.cn/article/39865
构建自己的AngularJS,第一部分:Scope和Digest 原文链接:http://teropa.info/blog/2013/11/03/make-your-own-angular-part- ...
- java crm 系统 进销存 springmvc SSM项目项目源码
统介绍: 1.系统采用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC浏览器使用) 2.springmvc +spring4.3.7+ mybaits3.3 SSM 普 ...
- github托管代码
安装git客户端 github是服务端,要想在自己电脑上使用git我们还需要一个git客户端, windows用户请下载 http://msysgit.github.com/ mac用户请下载 htt ...