Timus 1209. 1, 10, 100, 1000... 要求给出特定序列的指定位置的值。
Problem Author: Alexey Lakhtin
Problem Source: USU Open Collegiate Programming Contest October'2002 Junior Session
返回目录
1209. 1, 10, 100, 1000...
Time Limit: 1.0 second
Memory Limit: 16 MB
Memory Limit: 16 MB
Let's consider an infinite sequence of digits constructed of ascending powers of 10 written one after another. Here is the beginning of the sequence: 110100100010000… You are to find out what digit is located at the definite position of the sequence.
Input
There is the only positive integer number N in the first line, N < 65536. The i-th of N left lines contains the positive integer Ki — the number of position in the sequence. It's given that Ki < 231.
Output
You are to output N digits 0 or 1 separated with a space. More precisely, the i-th digit of output is to be equal to the Ki-th digit of described above sequence.
Sample
input | output |
---|---|
4 3 14 7 6 |
0 0 1 0 |
Problem Source: USU Open Collegiate Programming Contest October'2002 Junior Session
这道题目是说,让我们考虑依次由10的非负整数次方连接在一起构成的无限序列:110100100010000…。你的任务是给出该序列指定位置的值。
通过仔细观察,可以看出,该序列中第 (x + 1) 个 1 位于第 N = 1 + (1 + 2 + ... x) = 1 + x(x + 1) / 2 个位置。解这个关于 x 的一元二次方程,得到它的非负根为:x = (√8N - 7 - 1) / 2。于是,当 x 是整数时,该序列中对应的第 N 个位置的值是 1,其余位置的值是 0。
下面就是相应的 C# 程序:
1 using System;
2
3 // http://acm.timus.ru/problem.aspx?space=1&num=1209
4 class T1209
5 {
6 static void Main()
7 {
8 for (int i = int.Parse(Console.ReadLine()); i > 0; i--)
9 {
10 double x = (Math.Sqrt(8.0 * int.Parse(Console.ReadLine()) - 7) - 1) / 2;
11 Console.Write(((x == (int)x) ? 1 : 0) + " ");
12 }
13 }
14 }
2
3 // http://acm.timus.ru/problem.aspx?space=1&num=1209
4 class T1209
5 {
6 static void Main()
7 {
8 for (int i = int.Parse(Console.ReadLine()); i > 0; i--)
9 {
10 double x = (Math.Sqrt(8.0 * int.Parse(Console.ReadLine()) - 7) - 1) / 2;
11 Console.Write(((x == (int)x) ? 1 : 0) + " ");
12 }
13 }
14 }
返回目录