C. Sequence (64 Mb, 1 sec / test)
Integer sequences are very interesting mathematical objects. Let us examine a sequence generated with the use of two operations: doubling and “digit sorting”. The latter operation consists in ascending-order sort of the individual digits in the decimal representation of the argument. For example, “digit sorting” of number 5726 gives 2567. The first member of the considered sequence is 1. To generate a member of the sequence from the previous member, double the previous one and apply “digit sorting” to the result. The first 15 members of the sequence are as follows: 1, 2, 4, 8, 16, 23, 46, 29, 58, 116, 223, 446, 289, 578, 1156, … Write a program to determine the value of the n-th member of this sequence.
Limitations 1 ≤ n ≤ 2 147 483 647. Input The first line contains an integer n, the number of sequence member to be calculated. Output The output file should contain a single integer k, the value of the n-th member of the sequence. Example
Input.txt Output.txt 1 1
Input.txt Output.txt 6 23
题意:1, 2, 4, 8, 16, 23, 46, 29, 58, 116, 223, 446, 289, 578, 1156.....这样的数字,每一个数字
是由前一个数字乘以2,然后将每位数字由小到大排序得到的,问第n个数字是多少,n<=1e9;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
int a[500]={
0,1,2,4,8,16,23,46,29,58,116,223,446,289,578,1156,1223,2446,2489,
4789,5789,11578,12356,12247,24449,
48889,77789,155578,111356,122227,244445
};
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);//文件输入
int n;
while(~scanf("%d",&n))
{
if(n<=30) printf("%d
",a[n]);
else {
n-=24;
if(n%6==0) printf("%d
",a[30]);
else printf("%d
",a[n%6+24]);
};
}
fclose(stdin);
fclose(stdout);
return 0;
}
分析:这题还是很好的,首先看到n<=1e9,这么大的算法,打表O(n)什么的是肯定不行了,那么考虑一下直接构造,会发现
比较复杂,而且还得借助前一个数字,复杂度又是O(n),那么考虑一下循环性质,找规律,多写出几个数字,就发现规律了