题意:给定一个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)的更多相关文章

  1. Codeforces Gym 100418J Lucky tickets 数位DP

    Lucky ticketsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view ...

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

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

  3. poj 2346 Lucky tickets(区间dp)

    题目链接:http://poj.org/problem?id=2346 思路分析:使用动态规划解法:设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目. 将 ...

  4. Gym - 102040B Counting Inversion (数位dp)

    题意:求[a,b]区间内的数字中正序对的个数. 具体思路参考: https://blog.csdn.net/weixin_43135318/article/details/88061396 https ...

  5. Codeforces Gym 100231L Intervals 数位DP

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...

  6. POJ-2346 Lucky tickets(线性DP)

    Lucky tickets Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3298 Accepted: 2174 Descrip ...

  7. DP+高精度 URAL 1036 Lucky Tickets

    题目传送门 /* 题意:转换就是求n位数字,总和为s/2的方案数 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 高精度直接拿J ...

  8. 数位DP GYM 100827 E Hill Number

    题目链接 题意:判断小于n的数字中,数位从高到低成上升再下降的趋势的数字的个数 分析:简单的数位DP,保存前一位的数字,注意临界点的处理,都是套路. #include <bits/stdc++. ...

  9. 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 ...

随机推荐

  1. Oracle 10G 中的"回收站"

    在Oracle 10g数据库中,引入了一个回收站(Recycle Bin)的数据库对象. 回收站,从原理上来说就是一个数据字典表,放置用户Drop掉的数据库对象信息.用户进行Drop操作的对象并没有被 ...

  2. 【转】In ASP.NET using jQuery Uploadify upload attachment

    Upload Uploadify is a JQuery plug-in, achieve the effect is very good, with progress display. Upload ...

  3. Class工具类

    Class工具类,提供操作class类的方法,源码如下: import java.io.File; import java.io.FileFilter; import java.io.IOExcept ...

  4. Python3没有dict.has_key方法

    最近开始学习Python,安装上最新的Python3.3.3照书敲了一个小程序结果报错 'dict' object has no attribute 'has_key' 上网查也找不到解决办法,后来发 ...

  5. 关于iscroll.js插件的使用

    iscroll 作用: 可以让区域滚动效果好看一些 使用: 1. html结构 外面必须包一层盒子,切内部的元素要尽量简单,不然会影响滚动效果 <div id="wrapper&quo ...

  6. hdu2282 Chocolate 完美匹配 + 拆点

    题意: N个箱子排成一个圈,所有的箱子里的巧克力的数量加起来不大于N,每次可以把箱子里的巧克力向旁边的箱子转移(两个方向),问要让每个箱子里的巧克力不大于1的最小步数. 分析: 把巧克力大于1的箱子拆 ...

  7. equal height

    https://css-tricks.com/the-perfect-fluid-width-layout/ http://nicolasgallagher.com/multiple-backgrou ...

  8. SQL数据查询2

    USE h CREATE TABLE zy1( empno INT, ename ), job ), mgr INT, hiredate DATE, sal DOUBLE, COOM DOUBLE, ...

  9. JS 100节楼梯,0-49节 分数等于节数 50以后(包括50)每节10分输入节数 得出分数

    var n = parseInt(prompt("请输入数值")); ; ; ){ ; i<n; i++) { sum = sum + i; } alert(sum); } ...

  10. hibernate简单集合映射和获取

    简单集合映射(可以直接获取) // javabean设计 public class User { private int userId; private String userName; // 一个用 ...