[Train of Thought] spoj 11354 amusing numbers

Source: Internet
Author: User

Question:

To K (1 <= k <= 10 ^ 15), ask the number of numbers 5 and 6 in the k-th section.

For example, 1 is 5, 3 is 55, and 4 is 56.

Ideas:

First, we can find that there are two digits in one digit, four digits in two digits, and eight digits in three digits ..

Then we can use statistics to determine the number of digits in the k-th order.

Through the tired sum, there are 0 digits below one digit, 2 digits below two digits, and 6 digits below three digits

Below n digits is 2 ^ N-2

Then, for K, search for the first bit [I] smaller than K from big to bottom, and then there will be the number of I digits.

Then there is the number of I-bits, which is the nth number.

For example, if K is 70, it is 6 digits.

Then 70-bit [6]-1 = 7, which is 7th of the six-digit number.

Then let's think about it. Only 5 and 6 are actually binary,

0 is 5, 1 is 6.

For 7 (10) = 111 (2)

Then fill in 6 digits, Which is 000111 (2), that is, 555666.

Code:

# Include "cstdlib" # include "cstdio" # include "cstring" # include "cmath" # include "queue" # include "algorithm" # include "iostream" using namespace STD; # define ll long longll bit [66]; int main () {bit [1] = 0; For (INT I = 2; I <= 51; I ++) bit [I] = (1ll <I)-2; // Add ll int t; CIN> T; while (t --) {ll K; int I; scanf ("% LLD", & K); for (I = 51; I> = 1; I --) if (bit [I] <k) break; // confirm the number of digits ll TEP = k-bit [I]-1; // The number of digits int num [66], CNT = 0; while (TEP) {num [++ CNT] = TEP % 2; TEP/= 2 ;}for (Int J = 0; j <I-CNT; j ++) printf ("5"); // Add the preceding 0 for (Int J = CNT; j> = 1; j --) {If (Num [J]) printf ("6"); else printf ("5");} puts ("");} return 0 ;}


[Train of Thought] spoj 11354 amusing numbers

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.