秦九韶算法—— HDU 1111 Secret Code
2015-09-09 19:30
671 查看
对应HDU题目:点击打开链接
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 748 Accepted Submission(s): 133
Problem Description
The Sarcophagus itself is locked by a secret numerical code. When somebody wants to open it, he must know the code and set it exactly on the top of the Sarcophagus. A very intricate mechanism then opens the cover. If an incorrect code is entered, the tickets
inside would catch fire immediately and they would have been lost forever. The code (consisting of up to 100 integers) was hidden in the Alexandrian Library but unfortunately, as you probably know, the library burned down completely.
But an almost unknown archaeologist has obtained a copy of the code something during the 18th century. He was afraid that the code could get to the ``wrong people'' so he has encoded the numbers in a very special way. He took a random complex number B that
was greater (in absolute value) than any of the encoded numbers. Then he counted the numbers as the digits of the system with basis B. That means the sequence of numbers an, an-1, ..., a1, a0 was encoded as the number X = a0 + a1B + a2B2 + ...+ anBn.
Your goal is to decrypt the secret code, i.e. to express a given number X in the number system to the base B. In other words, given the numbers X and Byou are to determine the ``digit'' a0 through an.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case consists of one single line containing four integer numbers Xr, Xi, Br, Bi (|Xr|,|Xi| <= 1000000, |Br|,|Bi| <= 16). These numbers indicate
the real and complex components of numbers X and B, i.e. X = Xr + i.Xi, B = Br + i.Bi. B is the basis of the system (|B| > 1), X is the number you have to express.
Output
Your program must output a single line for each test case. The line should contain the ``digits'' an, an-1, ..., a1, a0, separated by commas. The following conditions must be satisfied:
for all i in {0, 1, 2, ...n}: 0 <= ai < |B|
X = a0 + a1B + a2B2 + ...+ anBn
if n > 0 then an <> 0
n <= 100
If there are no numbers meeting these criteria, output the sentence "The code cannot be decrypted.". If there are more possibilities, print any of them.
Sample Input
Sample Output
题意:
给出两个复数X = a + bi;B = c + di。其中X与B的关系为:
X = a0 + a1*B + a2 * B^2 + a3 * B^3 + ... + an * B^n
且满足 0 <= ai < |B| (0 <= i <= n) 且n <= 100;求任意一组{a0, a1, a2, a3, ... , an} 并反向输出。如果没有就按要求输出。
思路:
首先是|B| 表示复数的模;如B = c + di,则|B| = sqrt(c * c + d * d);
X = a0 + a1*B + a2 * B^2 + a3 * B^3 + ... + an * B^n
= a0 + B*(a1 + B*(a2 + B*(a3 + ... B*(a_n-1 + B*an)...)))
写成秦九韶形式就枚举ai了,每次用(X - ai) / B 直到 X的实部与虚部都为0或者n > 100。注意X = 0 + 0i 特例。。。
Secret Code
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 748 Accepted Submission(s): 133
Problem Description
The Sarcophagus itself is locked by a secret numerical code. When somebody wants to open it, he must know the code and set it exactly on the top of the Sarcophagus. A very intricate mechanism then opens the cover. If an incorrect code is entered, the tickets
inside would catch fire immediately and they would have been lost forever. The code (consisting of up to 100 integers) was hidden in the Alexandrian Library but unfortunately, as you probably know, the library burned down completely.
But an almost unknown archaeologist has obtained a copy of the code something during the 18th century. He was afraid that the code could get to the ``wrong people'' so he has encoded the numbers in a very special way. He took a random complex number B that
was greater (in absolute value) than any of the encoded numbers. Then he counted the numbers as the digits of the system with basis B. That means the sequence of numbers an, an-1, ..., a1, a0 was encoded as the number X = a0 + a1B + a2B2 + ...+ anBn.
Your goal is to decrypt the secret code, i.e. to express a given number X in the number system to the base B. In other words, given the numbers X and Byou are to determine the ``digit'' a0 through an.
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case consists of one single line containing four integer numbers Xr, Xi, Br, Bi (|Xr|,|Xi| <= 1000000, |Br|,|Bi| <= 16). These numbers indicate
the real and complex components of numbers X and B, i.e. X = Xr + i.Xi, B = Br + i.Bi. B is the basis of the system (|B| > 1), X is the number you have to express.
Output
Your program must output a single line for each test case. The line should contain the ``digits'' an, an-1, ..., a1, a0, separated by commas. The following conditions must be satisfied:
for all i in {0, 1, 2, ...n}: 0 <= ai < |B|
X = a0 + a1B + a2B2 + ...+ anBn
if n > 0 then an <> 0
n <= 100
If there are no numbers meeting these criteria, output the sentence "The code cannot be decrypted.". If there are more possibilities, print any of them.
Sample Input
4 -935 2475 -11 -15 1 0 -3 -2 93 16 3 2 191 -192 11 -12
Sample Output
8,11,18 1 The code cannot be decrypted. 16,15
题意:
给出两个复数X = a + bi;B = c + di。其中X与B的关系为:
X = a0 + a1*B + a2 * B^2 + a3 * B^3 + ... + an * B^n
且满足 0 <= ai < |B| (0 <= i <= n) 且n <= 100;求任意一组{a0, a1, a2, a3, ... , an} 并反向输出。如果没有就按要求输出。
思路:
首先是|B| 表示复数的模;如B = c + di,则|B| = sqrt(c * c + d * d);
X = a0 + a1*B + a2 * B^2 + a3 * B^3 + ... + an * B^n
= a0 + B*(a1 + B*(a2 + B*(a3 + ... B*(a_n-1 + B*an)...)))
写成秦九韶形式就枚举ai了,每次用(X - ai) / B 直到 X的实部与虚部都为0或者n > 100。注意X = 0 + 0i 特例。。。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #define N 105 typedef long long LL; int br, bi, k; bool Dfs(LL xr, LL xi, LL *a, int &cnt) { if(cnt > 100) return false; if(0 == xr && 0 == xi) return true; int i; LL n_xr, n_xi; for(i = 0; i * i < k; i++){ a[cnt++] = i; n_xr = (xr - i) * br + xi * bi; n_xi = xi * br - (xr - i) * bi; if(n_xr % k == 0 && n_xi % k == 0) if(Dfs(n_xr / k, n_xi / k, a, cnt) == true) return true; cnt--; } return false; } int main() { //freopen("in.txt","r",stdin); int T; LL a ; scanf("%d", &T); while(T--){ int xr, xi; int i, cnt = 0; scanf("%d%d%d%d", &xr, &xi, &br, &bi); if(0 == xr && 0 == xi){ printf("0\n"); continue; } k = br * br + bi * bi; bool ok = Dfs((LL)xr, (LL)xi, a, cnt); if(ok == false) printf("The code cannot be decrypted.\n"); else{ for(i = cnt - 1; i >= 0; i--){ printf("%I64d", a[i]); if(i) printf(","); } printf("\n"); } } return 0; }
相关文章推荐
- emacs的查找和替换
- 重启oracle数据库
- Service IntentService Android中的线程 MediaPlayer
- Android关于Theme.AppCompat相关问题的深入分析
- char (*(*p[3])( int ))[5] 等等一系列 左右法则
- 什么是J2EE
- C++条件及循环语句的综合运用实例
- 网络传输笔记 二
- Urimoo做试卷
- latex 删除标记
- 重新认识二级指针(Pointers to Pointers)
- NOIP 能量项链
- lintcode-验证查找二叉树-95
- LeetCode -- Remove Nth Node from End of List
- 设计模式:观察者模式
- 系统崩溃、重装后 Hyper-V 虚拟机配置的恢复
- The Factor(hdu5428)
- error C2872: “Font”: 不明确的符号
- 0909对操作系统的认识
- 【白书之路】 232 - Crossword Answers 字符串单词提取