i'm new to competitive programming world and i came across a problem that tries to calculate the number of substrings of a big string that contains only digits . how ever when i try to run the program on scorify platform it gives me the TimeLimitExceeded error . so if their is any help in minimize the code complexity i will be grate full
import java.util.Scanner;
import java.math.BigInteger;
public class test {
coprime is method that detects if the two numbers are coprime or not .
private static Boolean coprime(long a, long b) {
long t;
while(b!=0){
t = b;
b = a%b;
a = t;
}
if(a==1)
return true;
return false;
}
Our main :)
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
//t is the number of test cases
int t =sc.nextInt();
String s;
// n is the length of the string that we are going to scan .
int n;
//subs is the number of substrings found .
int subs;
// m is the long that we are going to compare the substrings with .
long m;
for(int i=0;i<t;i++){
subs = 0;
n=sc.nextInt();
m=sc.nextLong();
sc.nextLine();
s = sc.nextLine();
// I separated the cases of the length of s to optimize a little bit because BigIntegers are pain on the memory i think
//case 1: if the string(number s) entered is greater then the long max value (not so specific)
if(s.length()>=19){
BigInteger a;
// looping through all the substrings
for(int j=0;j<n;j++){
for(int p=j;p<n;p++){
a = new BigInteger(s.substring(j,p+1));
if(a.gcd(BigInteger.valueOf(m)).compareTo(BigInteger.ONE)==0){
subs++;
}
}
}
}
// case 2 : if the string(number entered is in the range of long numbers.
else{
for(int j=0;j<n;j++){
for(int p=j;p<n;p++){
if(coprime(m,Long.parseLong(s.substring(j,p+1)))){
subs++;
}
}
}
}
//printing the number of substrings.
System.out.println(subs);
}
}
Notes: Input Specifications:
The first line of input contains the number of test cases T (1 ≤ T ≤ 10).
The first line of each test case contains two integers: N (1 ≤ N ≤ 1000), the length of the string and M (1 ≤ M ≤ 1000000000).
The second line contains a string S of length N. It's guaranteed that S contains digits only.
Exemple :
input :
1
10 324567
9207289341
output :
40
Also:
if you have notes about organizing my code i will be grateful to hear
thank you .
for people who ask for the probleme .
Problem C. Co-prime Sub-strings
Two positive integers are said to be co-prime if their greatest common divisor is equal to 1. Now given a string of digits S and an integer M, we are interested in counting the number of sub-strings of S that are co-prime with M.
Input Specifications:
The first line of input contains the number of test cases T (1 ≤ T ≤ 10).
The first line of each test case contains two integers: N (1 ≤ N ≤ 1000), the length of the string and M (1 ≤ M ≤ 1000000000).
The second line contains a string S of length N. It's guaranteed that S contains digits only.
Output Specifications:
For each test case print, on a single line, the number of sub-strings of S that are co-prime with M.
STDIN
This is the content of the STDIN.
1
10 324567
9207289341
STDOUT
Your solution should produce a similar result.
40