Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
预处理建表,二分查找,不如朴素的快以下是我写的相当丑陋的代码,不要鄙视 #include <stdio.h> typedef unsigned long long ull; static const int arrsize = 32000; unsigned long long skCount[arrsize]; unsigned long long totalCount[arrsize]; int findDigitNo(int n) { int count = 0; if (n == 0) return 1; while (n) { ++count; n /= 10; } return count; } int findLimit() { skCount[0] = totalCount[0] = 0; skCount[1] = 1; totalCount[1] = 1; int i; for (i = 2; i < arrsize; ++i) { int digitNo = findDigitNo(i); skCount[i] = skCount[i-1] + digitNo; totalCount[i] = totalCount[i-1] + skCount[i]; if (totalCount[i] >= 2147483647) break; } return i; } //TODO: arr add 0 // left <= x < right int binarySearchTotal(int l, int h, ull x) { h = h + 1; while (l + 1 < h) { int mid = (l+h) / 2; if (x < totalCount[mid]) h = mid; else l = mid; } return l; } int binarySearchSk(int l, int h, ull x) { h = h + 1; while (l + 1 < h) { int mid = (l+h) / 2; if (x < skCount[mid]) h = mid; else l = mid; } return l; } int digitArr[8]; int findDigitsNo(int n) { int count = 0; if (n == 0) return 1; while (n) { digitArr[count++] = n % 10; n /= 10; } return count; } int main(int argc, char *argv[]) { int T; int highLimit = findLimit(); char digitch; while (scanf("%d", &T) == 1) { while (T--) { ull pos; scanf("%llu", &pos); int idx = binarySearchTotal(1, highLimit, pos); if (pos == totalCount[idx]) digitch = '0' + idx % 10; else { ull remain = pos - totalCount[idx]; int skidx = binarySearchSk(1, idx+1, remain); if (remain == skCount[skidx]) digitch = '0' + skidx % 10; else { ull rr = remain - skCount[skidx]; int digitcount = findDigitsNo(skidx+1); digitch = '0' + digitArr[digitcount-rr]; } } printf ( "%c\n", digitch); } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator