(UVA) sqrt log sin - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=655&page=show_problem&problem=2750
The solution below used Dynamic Programming to solve this problem.
For this problem, it is important to pay attention to the mathematical formulas.
import java.io.*;
import java.util.*;
class Main {
public int number;
public int[] array;
public int rec(int index) {
if (index == 0) {
array[0] = 1;
return array[0];
}
if (array[index] != -1) {
return array[index];
}
array[index] = (rec((int)Math.floor((double)index-Math.sqrt(index))) + rec((int)Math.floor(Math.log(index))) + rec((int)Math.floor((double)index*Math.sin(index)*Math.sin(index))));
return array[index]%1000000;
}
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
array = new int[1000001];
for (int i = 0; i <= 1000000; i++) {
array[i] = -1;
}
number = sc.nextInt();
while (number != -1) {
int result = rec(number);
bw.write(result%1000000+"\n");
number = sc.nextInt();
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
The solution below used Dynamic Programming to solve this problem.
For this problem, it is important to pay attention to the mathematical formulas.
import java.io.*;
import java.util.*;
class Main {
public int number;
public int[] array;
public int rec(int index) {
if (index == 0) {
array[0] = 1;
return array[0];
}
if (array[index] != -1) {
return array[index];
}
array[index] = (rec((int)Math.floor((double)index-Math.sqrt(index))) + rec((int)Math.floor(Math.log(index))) + rec((int)Math.floor((double)index*Math.sin(index)*Math.sin(index))));
return array[index]%1000000;
}
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
array = new int[1000001];
for (int i = 0; i <= 1000000; i++) {
array[i] = -1;
}
number = sc.nextInt();
while (number != -1) {
int result = rec(number);
bw.write(result%1000000+"\n");
number = sc.nextInt();
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment