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); 有什么区别呢? 首先 ...
随机推荐
- iOS9 tableVIewCell的分割线不显示,只有在滑动的时候才显示?
1.如果用6plus模拟器的话,电脑分辨率达不到那么高,因此就看不到分割线. 2.把模拟器换成6s 或 5s,就没问题了.
- tomcat、Linux服务器
tomcat.Linux服务器 用到的命令 解压命令: tar -zxvf 文件名 配置 : vi /etc/profile 按 i 进入 ...
- JAVA_DES 加密 解密 生成随机密钥
package com.test; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.In ...
- Dependency Injection
Inversion of Control - Dependency Injection - Dependency Lookup loose coupling/maintainability/ late ...
- 1063. Set Similarity (25)
1063. Set Similarity (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given ...
- 使用C#向ACCESS中插入数据(仅供参考)
1.创建并打开一个OleDbConnection对象 string strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ...
- django基于正则的url匹配
url.py views.py index.html detail.html 访问:
- 【HDU3721】枚举+最长路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3721 题意:给你一颗n个节点n-1条边的树,每条边都有一个权值,现在让你任意移动一条边然后把这条边连接 ...
- select的5中子句where,group by, havaing, order by, limit的使用顺序及实例
-- 语法: SELECT select_list FROM table_name [ WHERE search_condition ] [ GROUP BY group_by_expression ...
- php for循环嵌套
<?php //2.打印一个50*50的 'o' 的正方形方整, 使用for的嵌套 // oooooo // oooooo // oooooo //for循环嵌套 ...