Time Complexity time limit: +Ms | Memory Limit:65535KB Difficulty:3
-
-
Describe
-
in the ACM inside, computational complexity is a very important thing, and common complexity formats are three kinds:
-
O (N)
O (LG (N))
O (sqrt (n))
An algorithm often has a number of solutions, the complexity of each solution has the common complexity of the above combinations, such as two algorithms for sequencing:
Quick Sort: time complexity is O (N*LG (n))
Bubble Sort: time complexity is O (n*n)
now given you aN,mThe complexity of the algorithm, determine if the complexity will time out. If the complexity calculation result is greater than100000000, it is timed out(TLE), otherwise the output calculates the complexity of the output and retains two decimal places for the result.
(LG (N)represents the2as the base,Nis the value of the true number)
-
-
Input
-
The
-
first line is input n (1≤n≤10000), M (1≤m≤100), where n is the number of the topic description, and M is the number of algorithm complexity.
Next m line, each behavior a string, each string contains O () the data inside any parentheses is guaranteed to be composed and valid only by N,LG (), sqrt (), * *. As shown in sample input.
-
-
Output
-
-
for each string, if the calculated complexity is greater than 100000000, the output tle, otherwise the calculation of the complexity of the output number
-
-
Sample input
-
-
10000 6O (n*n) O (n*n*n) o (sqrt (n)) O (LG (n)) O (N*LG (n)) O (N*lg (N*LG (n))
-
-
Sample output
-
-
100000000.00tle100.0013.29132877.12170197.33
-
-
Tips
-
-
the C language code for LG (n) can be written like this
Log (n)/log (2)
-
-
-
-
Code:
-
#include <stdio.h> #include <string.h> #include <math.h> #define N 100000000char stack[1005];int k,n; Double eval () {if (k<0) return 0;if (stack[k]== ' n ') {K--;return n;} if (stack[k]== ' * ') {K--;return eval () *eval ();} if (stack[k]== ' G ') {K--;return log (eval ())/log (2);} if (stack[k]== ' t ') {K--;return sqrt (eval ());}} int main () {int m,i,j,top,len,s;double Result;char str[1005],ch[1005];while (~scanf ("%d%d", &n,&m)) {for (i=1;i <=m;i++) {scanf ("%s", str), Top=-1;s=-1;len=strlen (str), for (j=len-1;j>=1;j--) {if (str[j]== ' n ') {top++;stack[ TOP]=STR[J];} if (j<1) break;if (str[j]== ') ' | | str[j]== ' * ') {s++;ch[s]=str[j];} else if (str[j]== ' (') {while (s>=0&&ch[s]!= ') ') {top++;stack[top]=ch[s];s--;} s--;} else if (str[j]== ' t ') {top++;stack[top]=str[j];j=j-3;} else if (str[j]== ' G ') {top++;stack[top]=str[j];j=j-1;}} while (s>=0) {if (ch[s]!= ') ') {top++;stack[top]=ch[s];s--;}} top++;stack[top]= ' + '; K=strlen (Stack) -1;result=eval (); if (result>n) printf ("tle\n"); elseprintf ("%.2f\n", result);}} return 0;}
-
-
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Complexity of Time