UVa 12400 - 3, 2, 1, 0 (数学想法题&高精度 or 打表)
2014-02-12 21:02
423 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=3831
思路:
按照这个思路,其实这个序列是唯一的,如下:(1000位数,打表代码见文章最后)
3233232323222233222323222322322222332322232332333223232332222233232232233232223322232222233322223233323332333332323332333233333332233233222232223222222322322333322322222332323222322333232222222232223322332333223232233232222222323333323323232232333322333332223223322232323333232333332323233333323322223332223322222222232223332332332333233333233232333333233322223322222333333223232223232232223323232323333222323333332222333332222323323332323233332233323233233232332232223323223323222222323322333333323232322232222333233332232232233233332232223322233233232222222323333322322222222322323322333233333222323222222223222223233333223323232333223233222333232233333222233333322332322233332222333222323232223223233323223232332222332323232233333223333232232323322222233222333332223222233322323233233233232332323332222222332323322232332233323222222322333223322323322332232332222333222233333323332322232232232222233232332323332232222233323332233323223222232232232232332222333332332333333332223233333323323232223232
所以O(1)复杂度的代码如下:
打表代码:(输入 999 999 999 即可得到)
思路:
按照这个思路,其实这个序列是唯一的,如下:(1000位数,打表代码见文章最后)
3233232323222233222323222322322222332322232332333223232332222233232232233232223322232222233322223233323332333332323332333233333332233233222232223222222322322333322322222332323222322333232222222232223322332333223232233232222222323333323323232232333322333332223223322232323333232333332323233333323322223332223322222222232223332332332333233333233232333333233322223322222333333223232223232232223323232323333222323333332222333332222323323332323233332233323233233232332232223323223323222222323322333333323232322232222333233332232232233233332232223322233233232222222323333322322222222322323322333233333222323222222223222223233333223323232333223233222333232233333222233333322332322233332222333222323232223223233323223232332222332323232233333223333232232323322222233222333332223222233322323233233233232332323332222222332323322232332233323222222322333223322323322332232332222333222233333323332322232232232222233232332323332232222233323332233323223222232232232232332222333332332333333332223233333323323232223232
所以O(1)复杂度的代码如下:
/*0.015s*/ #include<cstdio> #include<cstring> const int mx = 1000; const char s[mx + 1] = "233232323222233222323222322322222332322232332333223232332222233232232233232223322232222233322223233323332333332323332333233333332233233222232223222222322322333322322222332323222322333232222222232223322332333223232233232222222323333323323232232333322333332223223322232323333232333332323233333323322223332223322222222232223332332332333233333233232333333233322223322222333333223232223232232223323232323333222323333332222333332222323323332323233332233323233233232332232223323223323222222323322333333323232322232222333233332232232233233332232223322233233232222222323333322322222222322323322333233333222323222222223222223233333223323232333223233222333232233333222233333322332322233332222333222323232223223233323223232332222332323232233333223333232232323322222233222333332223222233322323233233233232332323332222222332323322232332233323222222322333223322323322332232332222333222233333323332322232232232222233232332323332232222233323332233323223222232232232232332222333332332333333332223233333323323232223232"; int cnt[2][mx]; int main() { for (int i = 1; i < mx; ++i) { ++cnt[s[mx - i - 1] - '2'][i]; cnt[0][i] += cnt[0][i - 1]; cnt[1][i] += cnt[1][i - 1]; } int n, m, k; while (~scanf("%d%d%d", &n, &m, &k)) { if (k && n >= cnt[1][k] && m >= cnt[0][k]) puts(s + mx - 1 - k); else puts("Impossible."); } return 0; }
打表代码:(输入 999 999 999 即可得到)
/*1.802s*/ import java.io.BufferedInputStream; import java.math.BigInteger; import java.util.*; public class Main { static Scanner cin = new Scanner(new BufferedInputStream(System.in)); public static void main(String[] args) { int N, M, K, kk, i; BigInteger MASK, ten, cur; char ans[]; while (cin.hasNextInt()) { N = cin.nextInt(); M = cin.nextInt(); K = cin.nextInt(); if (K == 0) { System.out.println("Impossible."); continue; } if (K > M + N) { System.out.println("Impossible."); continue; } MASK = BigInteger.valueOf(2).pow(K).subtract(BigInteger.ONE); // MASK=2^k-1 ans = new char[K]; kk = K; ten = BigInteger.ONE; cur = BigInteger.ZERO; for (i = 0; i < K; ++i) { if (cur.and(BigInteger.ONE.shiftLeft(i)).equals(BigInteger.ZERO)) { // cur二进制的第i位为0 ans[--kk] = '2'; if (--M < 0) { System.out.println("Impossible."); break; } cur = cur.add(ten.multiply(BigInteger.valueOf(2))); if (cur.compareTo(MASK) > 0) { cur = cur.and(MASK); // 取模,以防止cur过大减慢运算速度 } } else {// cur二进制的第i位为1 ans[--kk] = '3'; if (--N < 0) { System.out.println("Impossible."); break; } cur = cur.add(ten.multiply(BigInteger.valueOf(3))); if (cur.compareTo(MASK) > 0) { cur = cur.and(MASK); // 取模,以防止cur过大减慢运算速度 } } ten = ten.multiply(BigInteger.valueOf(10)); } if (i == K) System.out.println(ans); } } }
相关文章推荐
- UVa 10157 Expressions (组合数学&高精度)
- UVa 10025 The ? 1 ? 2 ? ... ? n = k problem (数学&想法题&常数算法)
- UVa 10213 How Many Pieces of Land ? (数学&欧拉公式&高精度)
- NEERC 2006 / UVa 1388 Graveyard (数学&想法题&找最近整数)
- uva - 10041 - Vito's Family(数学)
- ACM: 数学题 uva_11137
- Uva 305 Joseph(数学+打表)
- [HDU 2608] 0 or 1 · 数学
- Uva1151——Buy or Build && POJ2784——Buy or Build
- POJ2247 HDU1058 UVA443 ZOJ1095 Humble Numbers【数学计算+打表+水题】
- UVa 11464 Even Parity (想法题&部分枚举)
- hdoj 天气情况 1437 (打表&数学)
- UVa 10254 The Priest Mathematician (组合数学&规律发现&高精度)
- UVA - 10561 Treblecross (博弈数学&SG函数)
- UVa 11489 Integer Game (博弈&想法题)
- UVa 10773 Back to Intermediate Math (数学&速度的分解&陷阱)
- UVa 10023 Square root (高精度&开平方公式)
- HDU 4791 & ZOJ 3726 Alice's Print Service (数学 打表)
- UVA_485 Pascal's Triangle of Death_高精度
- UVALive 2037 Digital Rivers 【打表&二分】