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); 有什么区别呢? 首先 ...
随机推荐
- [转发] 老叶观点:MySQL开发规范之我见
原文: http://imysql.com/2015/07/23/something-important-about-mysql-design-reference.shtml 老叶观点:MySQL开发 ...
- mysql 编码测试
insert into t1(v1) values('cn中国'); select * from t1; 1.输入gbk,交互latin1,数据库latin1 insert,客户端把gbk的输入当成l ...
- Forbidden You don't have permission to access / on this server. You don't have permission to access /phpmyadmin/ on this server. 解决办法
Forbidden You don't have permission to access / on this server. 解决办法 打开 httpd.conf 文件, 将 # onli ...
- The specified child already has a parent错误
10-05 23:39:48.187: E/AndroidRuntime(12854): Caused by: java.lang.IllegalStateException: The specifi ...
- iphone 使用技巧
http://www.app111.com/doc/100147120_1.html(隐藏某个图标) (3)传视频 moliplayer 和itunes ---应用(在下部)找到moliplaye ...
- Spring源码分析心得-Bean的读取和注册
Spring广泛的被使用足以证明它的价值,成功的东西自然值得我们深入学习.有很多人说的熟悉Spring也只是简单的使用经验,当我发现我也是其中一员时我很恐惧,所以开始扒源码,准备深入了解,话不多说,直 ...
- 使用==比较String类型
String类型的比较 public class StringDemo { public static void main(String[] args) { String s1 = "abc ...
- Python核心编程-闭包
百度搜了一下闭包的概念:简而言之,闭包的作用就是在外部函数执行完并返回后,闭包使得收机制不会收回函数所占用的资源,因为内部函数的执行需要依赖外函数中的变量.这是对闭包作用的非常直白的描述,不专业也不严 ...
- electron小例子
说明:该例子主要实现把输入框中的文字保存到本地的文本文档中. 在main中添加几句代码 const ipcMain = electron.ipcMain; const dialog = electro ...
- 5.3.2 Eclipse集成开发环境的使用技巧
Eclipse具有强大的编辑.调试.编译和打包功能,本节仅讲解Eclipse中最常用的功能. 1.将程序代码和注释字体变大 (1)启动Eclipse,选择“Windows”->“Preferen ...