sdut 2609 A-Number and B-Number
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2609
数位DP 以前没怎么做过,自己憋了半天,还是看了标程,标程写的就是好呀。
相关注释见代码:
#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<set>
#include<map> #define ll long long
#define ull unsigned long long
using namespace std;
const int INF=0x3f3f3f3f;
ll dp[22][10][2];
int a[22];//把这一个数按位分解
ll dfs(int n,int x,int flag,int limit)
//这里表示的意思应该是 到第n位的时候,前面构成的数的余数是x
//flag代表是否有 7 limit代表是否限制大小
{
if(n==-1)//边界
return (flag||x==0);
if(!limit&&dp[n][x][flag]!=-1)
return dp[n][x][flag];
ll ans=0;
int up=(limit)?a[n]:9;
for(int i=0;i<=up;++i)//枚举当前位的取值
{
ans+=dfs(n-1,(x*10+i)%7,(flag||i==7),(limit&&i==a[n]));
}
if(!limit)//当没有限制的时候 就是记忆化搜索 有限制就是普通的dfs
dp[n][x][flag]=ans;
return ans;
}
ll solve(ull x)
{
int len=0;
while(x)
{
a[len++]=x%10;
x=x/10;
}
return dfs(len-1,0,0,1)-1;
}
ll fNum(ull x)
{
ull tmp=solve(x);
return tmp-solve(tmp);
}
int main()
{
//freopen("data.in","r",stdin);
memset(dp,-1,sizeof(dp));
ll k;
while(cin>>k)
{
ull l=1,r=(ull)(pow(2.0,63)-1.0);
while(l<=r)//由于没有思路,根本没想到二分
{
ull mid=(l+r)>>1;
if(fNum(mid)>=k)
r=mid-1;
else
l=mid+1;
}
cout<<l<<endl;
}
return 0;
}
sdut 2609 A-Number and B-Number的更多相关文章
- Find n‘th number in a number system with only 3 and 4
这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in th ...
- [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III
Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...
- odd number、 even number
odd number 奇数 even number 偶数
- JavaScript Number() Vs new Number()
最近在优化一个页面时候.IDEA 提示我错误的使用了包装类.当时感觉很诧异. 随后.我上Stack Overflow上面查了一下,终于发现了问题所在. new Number('123') 与 Numb ...
- es6 Number.isFinite()、Number.isNaN()、Number.isInteger()、Math.trunc()、Math.sign()、Math.cbrt()、Math.fround()、Math.hypot()、Math 对数方法
ES6在Number对象上,新提供了Number.isFinite()和Number.isNaN()两个方法,用来检查Infinite和NaN这两个特殊值. Number.isFinite()用来检查 ...
- JS由Number与new Number的区别引发的思考
在回答园子问题的时候发现了不少新东西,写下来分享一下 == 下面的图就是此篇的概览,另外文章的解释不包括ES6新增的Symbol,话说这货有包装类型,但是不能new... 基于JS是面向对象的,所以我 ...
- how to convert a number to a number array in javascript without convert number to a string
how to convert a number to a number array in javascript without convert number to a string 如何在不将数字转换 ...
- python 利用位移法将ip转为number以及将number转为ip
简介: 使用位移法将ip转为number型以及将number型转为ip,使用语言为python2.7 #!/usr/bin/env python # coding:utf-8 def ip2num(i ...
- Number()和new Number()的区别以及一种简单实现
看MDN Beginners文档的时候注意到了这种用法 var n1 = Number(123); , 冒出的第一个疑问就是和 var n2 = new Number(123); 有什么区别呢? 首先 ...
随机推荐
- 2014 Multi-University Training Contest 4
1006 hdu4902 #include <iostream> #include<stdio.h> #include<vector> #include<qu ...
- Oracle分析函数的项目实践实例
SELECT * FROM SSE2_FLOW_EXPENSE T1 JOIN (SELECT SFEL.*, ROW_NUMBER() OVER(PARATITION BY SFEL.FE_EXPE ...
- css+js实现兼容性select的样式
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- android 回调函数的使用
// activity 之间方法调用的桥梁 public class ActivityCallBridge { static ActivityCallBridge mBridge; private O ...
- java技术知识点
1 自我介绍 2 做过的项目 (Java 基础) 3 Java的四个基本特性(抽象.封装.继承,多态),对多态的理解(多态的实现方式)以及在项目中那些地方用到多态 Java的四个基本特性 ◦ ...
- Centos7 PHP7 编译安装 开机自启动
1.PHP7.0.13下载 wget http://cn2.php.net/get/php-7.0.13.tar.gz/from/this/mirror 2.解压 .tar.gz 3. 进入目录 cd ...
- Memcached使用入门
转:http://www.linuxidc.com/Linux/2011-12/49516.htm http://blog.csdn.net/wxwzy738/article/details/2370 ...
- Java 两个变量交换值
package test; public class Test { public static void main(String[] args) { int a, b; ...
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)
在介绍之前,先简单说一说ASP.NET服务端GZIP压缩模块的作用及工作原理,很多人编写网页的时候页面因为使用了大量的JS特效又或者放置很多大型动态广告导致了页面或脚本体积庞大,通常都会使用一些压缩工 ...
- Hbase之进行批处理操作
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...