1

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

N. younes
  • 101
  • 4
  • 1
    not trying to calculate the number of sub strings but the number of substrings that are coprime with another number – N. younes May 17 '17 at 06:49
  • What algorithms have you looked at? – Kayaman May 17 '17 at 06:58
  • i didn't get what you mean . – N. younes May 17 '17 at 07:04
  • You need an efficient algorithm for solving those problems. A naive brute-force solution will never pass within the required time/space limits. That's the whole point of those competitions, so you should really have some computer science education or be a really avid reader. – Kayaman May 17 '17 at 07:17
  • Could you please give us a link to the problem statement? – DAle May 17 '17 at 08:35
  • And first of all you could try not to build BigDecimal on every step, you can multiply previous number by 10 and add next digit to it. – DAle May 17 '17 at 08:46
  • I have added the problem asked . for the question link http://candidate.scorify.me/challenges/details/RwrAXx – N. younes May 17 '17 at 09:40
  • #DAIe can you explain more ;p – N. younes May 17 '17 at 09:40
  • @N.younes: The challenge is that you find out or know such rather basic algorithms and combine them into something clever and fast. If you can't, then such a challenge is not for you (yet). – Rudy Velthuis May 17 '17 at 12:54

0 Answers0