Gym 100418J Lucky tickets(数位dp)
题意:给定一个n。求区间[1, n]之间的全部的a的个数。a满足:
a能整除 把a表示自身二进制以后1的个数
思路:题意非常绕....
数位dp,对于全部可能的1的个数我们都dfs一次
对于某一个可能的1的个数p来说。状态dp(len, i, j, k)里的每一位分别表示当前位,当前确定位的值模除p,已经有了多少个1。是否已经小于给定的n。
注意的是这题范围非常大,要用unsigned long long 来定义n
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define ULL unsigned long long
#define LL long long
#define pii (pair<int, int>)
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int digit[100];
ULL cnt_one;
ULL n;
LL d[100][100][100];
LL dfs(int len, ULL r, int k, int s) {
if(!len) return (r==0&&k==cnt_one) ? 1:0;
if(!s && d[len][r][k]!=-1) return d[len][r][k];
LL ans = 0;
ans += dfs(len-1, r, k, s&&!digit[len]);
if(!(s&&!digit[len]) && k<cnt_one) ans += dfs(len-1, (r+(1ULL<<(len-1)))%cnt_one, k+1, s&&digit[len]);
if(!s) d[len][r][k] = ans;
return ans;
}
ULL solve(ULL n) {
int cnt = 0, su = 0;
while(n) {
digit[++cnt] = n & 1;
su++; n >>= 1;
}
LL ans = 0;
for(cnt_one = 1; cnt_one <= su; cnt_one++) {
memset(d, -1, sizeof(d));
ans += dfs(cnt, 0, 0, 1);
}
return ans;
}
int main() {
while(cin >> n) {
cout << solve(n) << endl;
}
return 0;
}
Gym 100418J Lucky tickets(数位dp)的更多相关文章
- Codeforces Gym 100418J Lucky tickets 数位DP
Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...
- Codeforces Gym100623J:Just Too Lucky(数位DP)
http://codeforces.com/gym/100623/attachments 题意:问1到n里面有多少个数满足:本身被其各个数位加起来的和整除.例如120 % 3 == 0,111 % 3 ...
- poj 2346 Lucky tickets(区间dp)
题目链接:http://poj.org/problem?id=2346 思路分析:使用动态规划解法:设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目. 将 ...
- Gym - 102040B Counting Inversion (数位dp)
题意:求[a,b]区间内的数字中正序对的个数. 具体思路参考: https://blog.csdn.net/weixin_43135318/article/details/88061396 https ...
- Codeforces Gym 100231L Intervals 数位DP
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...
- POJ-2346 Lucky tickets(线性DP)
Lucky tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3298 Accepted: 2174 Descrip ...
- DP+高精度 URAL 1036 Lucky Tickets
题目传送门 /* 题意:转换就是求n位数字,总和为s/2的方案数 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 高精度直接拿J ...
- 数位DP GYM 100827 E Hill Number
题目链接 题意:判断小于n的数字中,数位从高到低成上升再下降的趋势的数字的个数 分析:简单的数位DP,保存前一位的数字,注意临界点的处理,都是套路. #include <bits/stdc++. ...
- Codeforces Gym 100286F Problem F. Fibonacci System 数位DP
Problem F. Fibonacci SystemTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudg ...
随机推荐
- python 数据的基本类型(字符串)
python 基础 ascii:字母,数字,特殊字符:1个字节(byte) 8个字位(bit)unicode: 16位两个字节,升级32个字节 4个字位utf-8:最少一个字节 8个表示. 英文 8字 ...
- Java中Calendar(日历)相关API举例
Java中Calendar(日历)相关API举例,实现功能:输入一个年份和月份打印出这个月的日历. package calendarPrint; import java.util.Calendar; ...
- B - Spyke Talks
Problem description Polycarpus is the director of a large corporation. There are n secretaries worki ...
- SQL使用总结——集合操作函数
Oracle中集合操作符专门用于合并多条select 语句的结果,包括:UNION, UNION ALL, INTERSECT, MINUS.当使用集合操作符时,必须确保不同查询的列个数和数据类型匹配 ...
- 关于原生app、webApp、混合app的介绍
WebApp 原生App(Native App) 混合App(hybrid App) webApp: 用html5,css3 js开发的网页,运行在移动端的浏览器 zepto.angular.vue. ...
- 8) 十分钟学会android--Activity的生命周期之停止与重启
恰当的停止与重启我们的activity是很重要的,在activity生命周期中,他们能确保用户感知到程序的存在并不会丢失他们的进度.在下面一些关键的场景中会涉及到停止与重启: 用户打开最近使用app的 ...
- android系统源码下载
ubuntu 安装git curl python 确保主目录下有一个 bin/ 目录,并且该目录包含在路径中: mkdir ~/bin PATH=~/bin:$PATH 下载 Repo 工具,并确 ...
- c++常用功能封装
C++文件读写的封装 在C++开发中,文件读写是很常用的功能,出于代码复用的考虑,将它们进行封装. 其中,默认读写的文件是文本文件,并且需要按行处理.调用者只需要关注读取出来的每一行的处理方式.写文件 ...
- ubuntu16.04下编译安装OpenCV
一: 预先配置 为使OpenCV的安装在编译时更完备,预先安装好所有的开发平台: 二:编译OpenCV 在OpenCV官网下载UNIX的源码包: 安装一下软件: sudo apt-get instal ...
- JS面向对象(2)——原型链
原型链用于ECMAScript的继承.其思想是利用原型让一个引用类型继承另一个引用类型的属性和方法.说人话,我们知道,一个构造函数Subtype,其原型对象有一个指向构造函数的指针,这是联系构造函数和 ...