http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2609

A-Number and B-Number

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

Tom is very interested in number problem. Nowadays he is thinking of a problem about A-number and B-number.
    A-number is a positive integer whose decimal form contains 7 or it can be divided by 7. We can write down the first 10 A-number ( a[i] is the ith A-number) 
         {a[1]=7,a[2]=14,a[3]=17,a[4]=21,a[5]=27,a[6]=28,a[7]=35,a[8]=37,a[9]=42,a[10]=47};
    B-number is Sub-sequence of A-number which contains all A-number but a[k] ( that k is a  A-number.)  Like 35, is the 7th A-number and 7 is also an A-number so the 35 ( a[7] ) is not a B-number. We also can write down the first 10 B-number.

{b[1]=7,b[2]=14,b[3]=17,b[4]=21,b[5]=27,b[6]=28,b[7]=37,b[8]=42,b[9]=47,b[10]=49};
    Now Given an integer N, please output the Nth B-number

输入

The input consists of multiple test cases.

For each test case, there will be a positive integer N as the description.

输出

For each test case, output an integer indicating the Nth B-number.

You can assume the result will be no more then 2^63-1.

示例输入

1
7
100

示例输出

7
37
470

提示

 

来源

 2013年山东省第四届ACM大学生程序设计竞赛

示例程序

分析:

一开始想到打表,结果超时咯,后来看了标程用了搜索写的,,,orz

超时代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
bool fun(int n){
if(n%==) return true;
while(n) {
if(n%==) return true;
n/=;
}
return false;
}
long long a[];
long long b[];
int main(){
int i,c=,cc=;
//freopen("in.txt","r",stdin);
for(i=;i<=;i++)
if(fun(i)) a[++c]=i;
for(i=;i<c;i++)
if(!fun(i))
{ b[++cc]=a[i];}
long long n;
while(cin>>n){
if(n>cc-) cout<<"??";
else
cout<<b[n]<<endl;}
return ;
}

官方标程:

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define ULL unsigned long long
const ULL Maxn=((1uLL<<64uLL)-);
ULL dp[][][];
int digit[];
ULL dfs(int pos,int pre,int flag,bool limit){
if(pos==-) return flag||pre==;
if(!limit&&dp[pos][pre][flag] != -) return dp[pos][pre][flag];
int end = limit?digit[pos]:;
int fflag,ppre;
ULL ans=;
for(int i = ;i <= end;i++){
fflag = (i==)||flag;
ppre=(pre*+i)%;
ans+=dfs(pos-,ppre,fflag,limit&&i==end);
}
if(!limit) dp[pos][pre][flag]=ans;
return ans;
}
ULL solve(ULL n){//找到n以下有几个A-number
int pos = ;
while(n){
digit[pos++] = n%;
n /= ;
if(!n) break;
}
return dfs(pos-,,,)-;
}
ULL find(ULL n){//找到n以下有几个B-number
ULL t = solve(n);//表示n包括n以下在A中有t个
ULL tt = t - solve(t);//表示t(包括第t个)个A_number中有几个是符合在B中的,一直找到tt刚好等于n为止
return tt;
}
int main()
{
memset(dp,-,sizeof(dp));
ULL n;
while(cin>>n){
ULL l = , r= Maxn,mid;
while(l <= r){
mid = ((r-l)>>)+l;
if(find(mid)<n) l = mid+;
else r = mid-;
}
ULL ans = r+;
cout<<ans<<endl;
}
return ;
}

sdutoj 2609 A-Number and B-Number的更多相关文章

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

  2. [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 ...

  3. odd number、 even number

    odd number 奇数 even number 偶数

  4. JavaScript Number() Vs new Number()

    最近在优化一个页面时候.IDEA 提示我错误的使用了包装类.当时感觉很诧异. 随后.我上Stack Overflow上面查了一下,终于发现了问题所在. new Number('123') 与 Numb ...

  5. 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()用来检查 ...

  6. JS由Number与new Number的区别引发的思考

    在回答园子问题的时候发现了不少新东西,写下来分享一下 == 下面的图就是此篇的概览,另外文章的解释不包括ES6新增的Symbol,话说这货有包装类型,但是不能new... 基于JS是面向对象的,所以我 ...

  7. 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 如何在不将数字转换 ...

  8. python 利用位移法将ip转为number以及将number转为ip

    简介: 使用位移法将ip转为number型以及将number型转为ip,使用语言为python2.7 #!/usr/bin/env python # coding:utf-8 def ip2num(i ...

  9. Number()和new Number()的区别以及一种简单实现

    看MDN Beginners文档的时候注意到了这种用法 var n1 = Number(123); , 冒出的第一个疑问就是和 var n2 = new Number(123); 有什么区别呢? 首先 ...

随机推荐

  1. Android -- TextView、button方法详解(1)

    1.TextView常规方法 TextView myTextView=null; //声明变量 myTextView=(TextView)findViewById(R.id.myTextView); ...

  2. 在Windows7下启动MongoDB服务的解决方案

    1:首先去官网下载程序,我用的是1.4.3版本,地址: http://downloads.mongodb.org/win32/mongodb-win32-i386-1.4.3.zip 2:创建一个DB ...

  3. js判断微信浏览器

    function is_weixin(){ //检查是否是微信浏览器 var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMes ...

  4. jquery过滤器之:contains()、.filter()

    :contains 选择器选取包含指定字符串的元素. 该字符串可以是直接包含在元素中的文本,或者被包含于子元素中. 经常与其他元素/选择器一起使用,来选择指定的组中包含指定文本的元素,如: $(&qu ...

  5. JAVA双向链表

    1.链表是一种重要的数据结构,在程序设计中占有很重要的地位 2.我们可以用类List来实现链表结构,用变量Head.Tail.Length.Pointer来实现表头.存储当前结点的指针时有一定的技 巧 ...

  6. Android查询系统的音频(音乐播放器的核心)

    //查询系统的音频库 public static List<MusicBean> getMusicInfo(Context context){ List<MusicBean> ...

  7. thinkphp条件查询和模糊查询的一些方法

    #文章管理 public function adminArticle(){ $adminArticle=M("article"); $arr_seach=$this->sea ...

  8. window常用命令(持续更新)

    1.netstat -ano 查看端口占用情况  可以根据PID到任务管理器中找到对应的进程 2.tasklist|findstr 进程号(pid) 查看pid是哪个程序启用的 3.taskkill ...

  9. LINUX数据库的备份,以及远程授权登陆

    mysql dump -u root -p juhui > /data/juhui.sql   //备份数据库 grant all privileges on *.* to xf111@loca ...

  10. NLP用CNN分类Mnist,提取出来的特征训练SVM及Keras的使用(demo)

    用CNN分类Mnist http://www.bubuko.com/infodetail-777299.html /DeepLearning Tutorials/keras_usage 提取出来的特征 ...