Why this compilation error in this? - java

import java.io.*;
static int MOD = 1000000007;
public class power {
public static long power(long num, int p) {
if (p == 0) return 1;
if (p == 1) return num;
long number = num;
for (int i = 2; i <= p; i++) {
num *= number;
num %= MOD;
}
return num;
}
};
public class Solution {
public static void main(String[] args) throws Exception {
int N, M;
long[] T = new long[1001];
long[] S = new long[1001];
long[] P = new long[1001];
T[0] = T[1] = 1;
T[2] = 2;
T[3] = 4;
T[4] = 8;
P[0] = P[1] = 1;
for (int i = 5; i <= 1000; i++)
T[i] = (T[i - 1] + T[i - 2] + T[i - 3] + T[i - 4]) % MOD;
S[0] = 1;
S[1] = 1;
long sum;
int Tt;
BufferedReader br = new BufferedReader(new InputStreamReader(System. in ));
String line = br.readLine();
Tt = Integer.parseInt(line);
for (int t = 0; t < Tt; t++) {
line = br.readLine();
String[] inputStr = line.split(" ");
N = Integer.parseInt(inputStr[0]);
M = Integer.parseInt(inputStr[1]);
power p = new power();
for (int i = 0; i <= M; i++)
P[i] = p.power(T[i], N);
for (int i = 2; i <= M; i++) {
sum = 0;
for (int j = 1; j < i; j++) {
sum += (S[j] * P[i - j]) % MOD;
sum %= MOD1;
}
S[i] = (P[i] - sum);
S[i] = S[i] % MOD;
}
while (S[M] < 0)
S[M] += MOD;
System.out.println(S[M]);
}
}
}
On compilation, it gives an error that,
class,interface, or enum expected
static int MOD = 100000007;
Why is it so? Also, is their any other error that can lead to compilation error? How can I correct it? I am new to Java. I don't understand why is their a error in the first place.

static int MOD = 1000000007;
written out side class.
Include it inside class power then use power.MOD
Code should be :
public class power {
static int MOD = 1000000007;
//rest of your code
}

The declaration of the field must be inside of the class:
Change to :
public class power {
static int MOD = 1000000007;

Declare static int MOD = 1000000007; inside the power class and import package to access from main method.
import static com.regex.power.MOD;
public class power {
static int MOD = 1000000007;

Declare it this way
public class power {
public static int MOD = 1000000007;
And you can access it through class name in your main method.
power.MOD
Note:
You should not have method name as your class name (in your case power).
I have removed all compilation error. Added comments for the changes which I have done.
public class Power {
public static int MOD = 1000000007;
public static long findPower(long num, int p) { // changed method name as it was same as class name
if (p == 0)
return 1;
if (p == 1)
return num;
long number = num;
for (int i = 2; i <= p; i++) {
num *= number;
num %= MOD;
}
return num;
}
}
class Solution { // removed public. If you want to use public then you will have to create another java file name Solution.java and paste Solution class code inside that java file.
public static void main(String[] args) throws Exception {
int N, M;
long[] T = new long[1001];
long[] S = new long[1001];
long[] P = new long[1001];
T[0] = T[1] = 1;
T[2] = 2;
T[3] = 4;
T[4] = 8;
P[0] = P[1] = 1;
for (int i = 5; i <= 1000; i++)
T[i] = (T[i - 1] + T[i - 2] + T[i - 3] + T[i - 4]) % Power.MOD; // access using Power.MOD
S[0] = 1;
S[1] = 1;
long sum;
int Tt;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
Tt = Integer.parseInt(line);
for (int t = 0; t < Tt; t++) {
line = br.readLine();
String[] inputStr = line.split(" ");
N = Integer.parseInt(inputStr[0]);
M = Integer.parseInt(inputStr[1]);
// Power p = new Power();
for (int i = 0; i <= M; i++)
P[i] = Power.findPower(T[i], N); // No need to use object, you can directly access static methods using class
for (int i = 2; i <= M; i++) {
sum = 0;
for (int j = 1; j < i; j++) {
sum += (S[j] * P[i - j]) % Power.MOD;
sum %= Power.MOD;
}
S[i] = (P[i] - sum);
S[i] = S[i] % Power.MOD;
}
while (S[M] < 0)
S[M] += Power.MOD;
System.out.println(S[M]);
}
}
}

Related

How do I fix the resource leak in my RSA algorithm (java)?

I am attempting to make an RSA algorithm and i'm not sure how to close my scanner. I have attempted to use .close(); for instance trying to place it at the end of my RSA class but to no avail. Most likley I am just not putting it in the correct place or because i keep getting the following error in Visual Studio on my line 19:
Help much appreciated.
import java.util.*;
public class RSA {
static int gcd(int m, int n) {
while (n != 0) {
int r = m % n;
m = n;
n = r;
}
return m;
}
public static void main(String args[]) {
int p = 0, q = 0, n = 0, e = 0, d = 0, phi = 0;
int nummes[] = new int[100];
int encrypted[] = new int[100];
int decrypted[] = new int[100];
int i = 0, j = 0, nofelem = 0;
Scanner sc = new Scanner(System.in);
String message;
System.out.println("Enter the Message to be encrypted:");
message = sc.nextLine();
System.out.println("Enter value of p and q\n");
p = sc.nextInt();
q = sc.nextInt();
n = p * q;
phi = (p - 1) * (q - 1);
for (i = 2; i < phi; i++)
if (gcd(i, phi) == 1) break;
e = i;
for (i = 2; i < phi; i++)
if ((e * i - 1) % phi == 0) break;
d = i;
for (i = 0; i < message.length(); i++) {
char c = message.charAt(i);
nummes[i] = c - 96;
}
nofelem = message.length();
for (i = 0; i < nofelem; i++) {
encrypted[i] = 1;
for (j = 0; j < e; j++)
encrypted[i] = (encrypted[i] * nummes[i]) % n;
}
System.out.println("\n Encrypted message\n");
for (i = 0; i < nofelem; i++) {
System.out.print(encrypted[i]);
System.out.print((char) (encrypted[i] + 96));
}
for (i = 0; i < nofelem; i++) {
decrypted[i] = 1;
for (j = 0; j < d; j++)
decrypted[i] = (decrypted[i] * encrypted[i]) % n;
}
System.out.println("\n Decrypted message\n ");
for (i = 0; i < nofelem; i++)
System.out.print((char) (decrypted[i] + 96));
return;
}
}

static string weird behavior Diamond Star Pattern problem

I'm trying to solve this problem given a number 5, we display:
*
*****
*********
*****
*
And so on. So you give it a number and it format it for you like above. I tried to solve it using this code bellow and I can't see where the problem is in my code.
public class Exe01 {
public static String space = "";//global space var
public static String ast = "";//global * var
public static String adjustAst(int numOfAst) {
Exe01.ast = "";
for (int i = numOfAst; i > 0; i--) {
Exe01.ast+="*";
}
return Exe01.ast;
}
public static String adjustSpaces(int numOfSpaces) {
Exe01.space = "";
for (int i = numOfSpaces; i > 0; i--) {
Exe01.space = Exe01.space + " ";
}
return Exe01.space;
}
public static void showAst(int num) {
if (num <= 0 || num % 2 == 0)
System.out.println("arg to the function need to be positive and odd");
else if (num == 1)
System.out.println("*");
else {
int mid = (int) (num / 2);
int numberOfSpaces = num - 1;
for (int i = 0; i < num; i++) {
int k = 0;
if (i < mid) {
k = k * 2 + 1;
System.out.println(Exe01.adjustSpaces(numberOfSpaces) + Exe01.adjustAst(k));
numberOfSpaces = numberOfSpaces - 2;
} else if (i == mid) {
numberOfSpaces = 0;
k = k * 2 + 1;
System.out.println(Exe01.adjustSpaces(numberOfSpaces) + Exe01.adjustAst(k));
numberOfSpaces = numberOfSpaces + 2;
} else {
k = k - 4;
System.out.println(Exe01.adjustSpaces(numberOfSpaces) + Exe01.adjustAst(k));
numberOfSpaces = numberOfSpaces + 2;
}
}
}
}
public static void main(String args[]) {
Exe01.showAst(5);
}
}
At compilation time it gives me this:
*
*
*

I am trying to cancel factorials in Java

I'm trying to calculate the number of rCombinations for a project for school and I can't seem to get my method to return the correct values.
I talked to my professor and he recommended canceling the common factors in factorials. Such that
35!/32! = 35*34*33.
This is what I have so far.
public static long rCombinations(int n, int r) {
int q = n-r;
long x = 1;
for(int i = r; i <= r; i ++)
{
x = n*(n-i);
}
return x/factorial(r);
}
You can use this implementation for calculating large factorial of numbers without BigInteger as follows :
import java.util.Scanner;
public class N_Faktorial {
public static void main(String[] args) {
int u = 1, A[] = new int[9999999];
Scanner scan = new Scanner(System.in);
System.out.print("n=");
int n = scan.nextInt();
A[1] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
A[j] *= i;
}
for (int j = 1; j <= n; j++) {
if (A[j] > 9) {
A[j + 1] += A[j] / 10;
A[j] %= 10;
}
if (A[u + 1] != 0) {
u++;
}
}
}
for (int i = u; i >= 1; i--) {
System.out.print(A[i]);
}
//when n>=24 count of digit of n! is equal to n+1.
System.out.println("\n Result : " + n + " count of digit " + u);
}
}
After this you need some solution for doing division operation.
Hope it helps!.
For n!/m!, where n >= m
int out = 1;
for(int i = n; i <= m; i++)
out *= i;
For n!/m!, where n <= m
double out = 1;
for(int i = n; i <= m; i++)
out /= i;
In both cases, out = n!/m!
Note that it's still easy to overflow an int, 55!/49! is too big

Getting NZEC in Java for PARTSUM on SPOJ

Array T[] has been used to store the sum of all possible contiguous combinations. And at each step, I am checking whether it is atleast k or not and that value is stored in the variable val. Below is the Java Code:
import java.util.Arrays;
import java.util.Scanner;
class Codechef {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
long t = sc.nextLong();
while (t > 0) {
int n = sc.nextInt();
long k = sc.nextLong();
long p = sc.nextLong();
long a[] = new long[n];
long T[] = new long[((n * (n + 1)) / 2)];
Arrays.fill(T, -1);
long sum = 0, x = 0, val = p, f = 0;
int i = 0;
for (i = 0; i < n; i++) {
a[i] = sc.nextLong();
sum = sum + a[i];
x = sum % p;
T[i] = x;
if (T[i] == k)
f = 1;
else if (T[i] > k && T[i] < val)
val = T[i];
}
if (f != 1) {
for (int j = i - 1; j > 0; j--) {
for (int m = 0; m < j; m++) {
x = T[j] - T[m];
if (x < 0)
x += p;
T[i] = x;
if (T[i] == k) {
f = 1;
break;
}
else if (T[i] > k && T[i] < val)
val = T[i];
i++;
}
if (f == 1)
break;
}
}
if (f == 1)
System.out.println(k);
else
System.out.println(val);
t--;
}
}
}
The above is the code for PARTSUM- Partial Sums on SPOJ. What could be the possible error?
Previously I was getting Runtime error NZEC on submit. Now that has been corrected and now I am getting TLE. Any optimization?

multidimensional array sorting by column in java

i need some help in this problem, there are three columns namely phase#, block# and lot#, i needed to sort the phase# in ascending order while the other two will be sorted accordingly to its phase#:
Problem:
Phase# 1-2-1-3-1-2-1
Block# 1-1-2-1-2-1-1
Lot# 1-2-2-2-3-1-1
What it should be like:
Phase# 1-1-1-1-2-2-3
Block# 1-2-2-1-1-1-1
Lot# 1-2-3-1-2-1-2
here's what I've got so far:
import java.io.*;
import java.util.*;
public class test{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x = 7;
int y = 3;
int[][] phase= new int[x][y];
int swap = 0, temp, i, min = 0;
phase[0][0] = 1;
phase[1][0] = 2;
phase[2][0] = 1;
phase[3][0] = 3;
phase[4][0] = 1;
phase[5][0] = 2;
phase[6][0] = 1;
phase[0][1] = 1;
phase[1][1] = 1;
phase[2][1] = 2;
phase[3][1] = 1;
phase[4][1] = 2;
phase[5][1] = 1;
phase[6][1] = 1;
phase[0][2] = 2;
phase[1][2] = 2;
phase[2][2] = 2;
phase[3][2] = 2;
phase[4][2] = 3;
phase[5][2] = 1;
phase[6][2] = 1;
System.out.println("UNSORTED: \n");
System.out.println("Phase#\tBlock#\tLot#");
for(i = 0; i < phase.length; i++){
System.out.print(phase[i][0] + "\t" + phase[i][1] + "\t"+phase[i][2]);
System.out.print("\n");
}
System.out.println("\nSORTED:\n");
for(i = 0; i <= phase.length- 1; i++){
min = i;
for(int a = i+1; a < phase.length; a++ ){
if(phase[a][0]<phase[a][0]){
temp=phase[i][0];
phase[i][0]=phase[a][0];
phase[a][0]=temp;
}
}
}
System.out.println("Phase#\tBlock#\tLot#");
for(int j = 0; j < phase.length; j++){
System.out.print(phase[j][0] + "\t" + phase[j][1] + "\t"+phase[j][2]);
System.out.print("\n");
}
}
}
Implement Comparator interface as (int [] left, int[] right) -> left[0]-right[0] and pass it to Arrays.sort() method:
Arrays.sort(phase, (int [] left, int[] right) -> left[0]-right[0]);

Categories

Resources