Topic:
exponentiation
| Time Limit: 500MS |
|
Memory Limit: 10000K |
| Total Submissions: 139077 |
|
Accepted: 33994 |
Description
Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt are a taxing experience for many computer systems.
This problem requires so you write a program to compute the exact value of Rn where R is a real number (0.0 < R < 99.999) and n is a integer such that 0 < n <= 25.
Input
The input would consist of a set of pairs of values for R and N. The R value would occupy columns 1 through 6, and the N value would be in columns 8 and 9.
Output
The output would consist of one line for each line of input giving the exact value of r^n. Leading zeros should is suppress Ed in the output. Insignificant trailing zeros must not being printed. Don ' t print the decimal point if the result was an integer.
Sample Input
95.123 120.4321 205.1234 156.7592 998.999 101.0100 12
Sample Output
548815620517731830194541.899025343415715973535967221869852721.00000005148554641076956121994511276767154838481760200726351 20383542976301346240143992025569.92857370126648804114665499331870370751166629547672049395302429448126.7641210216181644302 0690903717327667290429072743629540498.1075960194566517745610440100011.126825030131969720661201
Ideas:
Convert all inputs into integers, eliminate decimal points, and then add decimal points by integer high precision arithmetic
Code:
21:10-23:11#include <stdio.h> #include <string.h>char res[10006], sa[10006], sb[10006], tsb[10006]; void Show (char *s) {char *p;if (*s = = 0 && * (s + 1) = =-2) ++s;if (*s = = 2 && * (s + 1) = = 1) {printf ("0"); r Eturn;} p = s;while (*++p! =-1);--p;while (*p! =-2 && *p = 0)--p;if (*p = =-2)--p;++p;while (s! = p) {if (*s = = 2) putch Ar ('. '); Elseputchar (' 0 ' + *s); ++s;}} void Pre (char *s) {while (*s! = ')} {if (*s = = '. ') *s = -2;else*s-= ' 0 '; ++s;} *s =-1;} void owncpy (char *s, char *p) {while (*p! =-1) {*s++ = *p++;} *s =-1;} char* multiple (char *s, Char *a, char *b) {memset (s, 0, sizeof (char) * 10006); int I, flags = 0, Lena = 0, LenB = 0, EA, EB; for (i = 0; A[i]! =-1; ++i) {if (flags = = 1) {a[i-1] = a[i];lena++;} if (a[i] = =-2) {flags = 1;lena = 0;}} EA = flags? I-2: i-1;flags = 0;for (i = 0; B[i]! =-1; ++i) {if (flags = = 1) {b[i-1] = b[i];lenb++;} if (b[i] = =-2) {flags = 1;lenb = 0;}} EB = flags? I-2: I-1;int x, y, c;s[10001] = -1;for (x = ea; X >= 0; --x) {for (y = eb; y >= 0;--y) {c = 10000-(ea-x + eb-y); S[c] + = a[x] * b[y];if (S[c] > 9) {int TP = S[c]/10;s[ C]%= 10;s[c-1] + = TP;}}} int first = 10000-(EA + EB + 5), while (first <= 10000 && s[++first] = = 0), int f = 0, ploc;if (Lena + LenB = 10000-first + 1) {s[f++] = 0;s[f++] = -2;int k = Lena + LenB-(10000-first + 1); while (k--) s[f++] = 0;} Else{ploc = 10000-lena-lenb;while (First <= ploc) s[f++] = s[first++];s[f++] =-2;} while (first <= 10000) s[f++] = s[first++];s[f] = -1;return s;} void Ownpow (int n) {while (--n) {Multiple (res, SA, SB); owncpy (SA, res); owncpy (SB, TSB);}} int main () {int n;while (~SCANF ("%s%d", SA, &n)) {pre (SA); owncpy (SB, SA); owncpy (TSB, SA); owncpy (res, SA); Ownpow (n); Show (RES);p rintf ("\ n");} return 0;}
"POJ" 1001-exponentiation