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. Invalid escape sequence(valid ones are \b \t \n \f \r \" \' \\)

    Invalid escape sequence(valid ones are \b \t \n \f \r \" \' \\) 在运行eclipse的相关程序代码时遇到了报错信息,查看控制台 ...

  2. osgearth各个例子功能概述

    osgearth各个例子功能概述 转自:http://blog.csdn.net/wl198302/article/details/21177309 最近在学习osgearth,对其还不是很理解,有些 ...

  3. Linux文件/目录权限设置命令:chmod

    文件/目录权限设置命令:chmod 这是Linux系统管理员最常用到的命令之一,它用于改变文件或目录的访问权限.该命令有两种用法: 用包含字母和操作符表达式的文字设定法 其语法格式为:chmod [w ...

  4. 配置Nginx支持ThinkPHP的URL重写和PATHINFO

    ThinkPHP支持通过PATHINFO和URL rewrite的方式来提供友好的URL,只需要在配置文件中设置 'URL_MODEL' => 2 即可.在Apache下只需要开启mod_rew ...

  5. [LintCode] Remove Linked List Elements 移除链表元素

    Remove all elements from a linked list of integers that have value val. Have you met this question i ...

  6. sbt %%

    在依赖库选项中会看到其中有的是 %%,而有的是一个%. 这表示 :“要求sbt寻找用当前你配置的scala版本编译出来的jar包.” 因为scala不同版本编译出来的结果会不兼容.

  7. request获取请求头和请求数据

    package cn.itcast.request; import java.io.IOException; import java.io.InputStream; import java.io.Pr ...

  8. js == 判断

    根据阮一峰介绍的算法文章分12部分可判断: 如果x不是正常值(比如抛出一个错误),中断执行. 如果y不是正常值,中断执行. 如果Type(x)与Type(y)相同,执行严格相等运算x === y. 如 ...

  9. ExtJS笔记 Tree

    The Tree Panel Component is one of the most versatile Components in Ext JS and is an excellent tool ...

  10. [Ubuntu][Linux]更改PATH路径

    1.什么是环境变量(PATH) 在Linux中,在执行命令时,系统会按照PATH的设置,去每个PATH定义的路径下搜索执行文件,先搜索到的文件先执行. 我们知道查阅文件属性的指令ls 完整文件名为:/ ...