sdutoj 2609 A-Number and B-Number
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
输入
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
提示
来源
示例程序
分析:
一开始想到打表,结果超时咯,后来看了标程用了搜索写的,,,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的更多相关文章
- 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); 有什么区别呢? 首先 ...
随机推荐
- BZOJ4542: [Hnoi2016]大数
Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P.现在,小 B 提出了 M 个询问,每个 ...
- BZOJ3294: [Cqoi2011]放棋子
Description Input 输入第一行为两个整数n, m, c,即行数.列数和棋子的颜色数.第二行包含c个正整数,即每个颜色的棋子数.所有颜色的棋子总数保证不超过nm. Output 输出 ...
- 《1Q84》--[日]村上春树
<1Q84>,作者是:村上春树(村长) 故事梗概: 1984年,青豆与天吾皆为30岁,青豆为健身教练但另一面则是暗杀者,将受到极度暴力 的妇女们的丈夫们送至死亡的世界.天吾的职业为升大学的 ...
- 深入浅出 - Android系统移植与平台开发(二) - 准备Android开发环境
作者:唐老师,华清远见嵌入式学院讲师. 编译Android源码 关于android系统的编译,Android的官方网站上也给出了详细的说明.http://source.android.com/sour ...
- MySQL 服务无法启动。服务没有报告任何错误。
MySQL数据库在升级到5.7版本后,和之前的版本有些不一样,没有data文件夹,我们都知道MySQL数据库文件是保存在data文件夹中的,网上有人说把5.6版本的data文件夹拷贝一个,这种说法听听 ...
- [LintCode] Surrounded Regions 包围区域
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- for循环练习
1.输入一个整数,计算从1加到这个数的结果int sum = 0;Console.WriteLine("请输入一个正整数");int a = int.Parse(Console.R ...
- 关于VS打包程序无法弹出主界面的问题
代码中的程序很正常,VS打包之后,无法弹出主界面的问题. 这种问题,一般是缺少程序加载所必须的东西,包括dll,配置文件等. (1)程序主界面使用的是DevExpress,DevExpress的相关类 ...
- 单页Web应用:
概念: Web应用程序: WEB应用程序一般是B(浏览器)/S(服务器)模式.Web应用程序首先是“应用程序”,和用标准的程序语言,如C.C++等编写出来的程序没有什么本质上的不同.然而Web应用程序 ...
- sqlmap 1.0.21 tamper 总结
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...