Hdoj Ztr loves lucky numbers 5676 (DFS emulation)

Source: Internet
Author: User

ZTR loves lucky NumbersTime limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 918 Accepted Submission (s): 389


Problem Descriptionztr loves lucky numbers. Everybody knows that positive integers is lucky if their decimal representation doesn ' t contain digits other than 4 and 7 . For example, numbers, 744, 4 is lucky and 5, and 467 is not.

Lucky number is super Lucky if it ' s decimal representation contains equal amount of digits 4 and 7. For example, numbers, 7744, 474477 is super lucky and 4, 744, 467 is not.

One day ZTR came across a positive integer n. help him to find the least super lucky number which are not less than N.

Inputthere is T (1≤ n≤ ten 5 )  Cases

For each cases:

The only line contains a positive integer  n(1≤ n≤ ten ) . This number doesn ' t has leading zeroes.

Outputfor each cases
Output the answer
Sample Input
2450047

Sample Output
474747

Sourcebestcoder Round #82 (Div.2) Description of the problem
Ztr likes lucky numbers, he has two requirements for lucky numbers 1: The decimal notation contains only 4, 72: The decimal notation 4 and 7 are equal in number, such as 47,474477, and 4,744,467 is not now ZTR want to know the smallest but not less than n lucky number is what
Enter a description
YesT (1\leq\; T\LEQ\;10^{5})T(1≤T≤1 0? 5  ??)Group data, a positive integer for each set of data nn ,1\leq\;n\leq\;10^{18}1≤N≤1 0?  18   ??
Output description
There is a  t  -line, each line is the answer
Input sample
2450047
Output sample
474747
Hint
Please optimize the algorithm as much as possible, considering the full
Nothing to say, is a DFS simulation, but pay attention to a special sentence, see the code
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> #define INF 0x3f3f3f3f#define ll long long#define N 100010using namespace Std;ll a[n];int cnt;void dfs (int l,int r,int len,ll num) {if ( l>len/2| | R>LEN/2) return, if (l+r==len&&l==r) {A[cnt++]=num;return;} if (L<=LEN/2) DFS (L+1,R,LEN,NUM*10+4); if (R<=LEN/2) DFS (l,r+1,len,num*10+7);} int main () {cnt=0;for (int i=2;i<=18;i+=2) DFS (0,0,i,0), int t;ll n;scanf ("%d", &t), while (t--) {scanf ("%lld", &N); if (N>777777777444444444LL)   //Special sentence printf ("44444444447777777777\n"); elseprintf ("%lld\n", A[lower_ Bound (A,a+cnt,n)-A]);} return 0;}

Hdoj Ztr loves lucky numbers 5676 (DFS emulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.