6-N-Cryption

Dev Skill archived problems can be discussed here.

6-N-Cryption

by arabin » Sun Sep 18, 2016 4:23 am

Code: Select all
class MyClass {
   
    private static Scanner kbScanner;
    private static Set<Long>ekSet;
    public static void main(String []args)throws java.lang.Exception{
       
        kbScanner = new Scanner(System.in);
       
        int testCase = kbScanner.nextInt();
        if (testCase>0) {
            long []answer = new long[testCase];
            for (int i = 0; i < testCase; i++) {
                long eNval = kbScanner.nextLong();
                int nEk = kbScanner.nextInt();
                long finalAns = getFinalAns(nEk,eNval);
                if (finalAns != -1) {
                    answer[i] = finalAns;
                }
                else
                    return;
                System.out.println("");
            }
           
            for (int i = 0; i < answer.length; i++) {
                System.out.println("Output: "+answer[i]);
            }
           
        }
       
    }

    private static long getFinalAns(int eK,long eNval) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   
        ekSet = new TreeSet<>();
        for (int i = 0; i <eK; i++) {
            long number = kbScanner.nextLong();
            if (number>0) {
                ekSet.add(number);
            }
            else
                return -1;
        }
        Iterator<Long>iter = ekSet.iterator();
        long primeselector = iter.next();
       
        long bigPrime = getPrime(primeselector);
       
        long sum = primeselector/bigPrime;
       
        for (int i = 1; i < ekSet.size(); i++) {
           
            sum += iter.next()/bigPrime;
           
        }
       
        return eNval-sum;
    }

    private static long getPrime(long primeselector) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   
        boolean check;
        long prime = 0;
       
        if (primeselector <= 2) {
            prime = primeselector;
            return prime;
        }
        else{
            for (int i = 2; i <=primeselector;) {
                if (primeselector%i==0) {
                    long testprime = primeselector/i;
                    check = primeCheck(testprime);
                    if (check) {
                        prime = testprime;
                        break;
                    }
                    else
                        i++;
                }
                else
                    i++;
            }
            return prime;
        }
    }

    private static boolean primeCheck(long prime) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

        if (prime <= 2) {
            return true;
        }
        else
        {
            for (long i = 2; i <prime; i++) {
                if (prime%i == 0) {
                    return false;
                }
            }
            return true;
        }
    }
   
}



this code shows runtime error input mismatch why?
 
Posts: 28
Joined: Sat Aug 06, 2016 11:02 pm

Re: 6-N-Cryption

by devskill » Wed Sep 21, 2016 5:44 am

First of all, again please always format your code when you post.

Try replacing your nextInt using nextLong and try again.
User avatar
Site Admin
 
Posts: 118
Joined: Tue Feb 02, 2016 10:24 pm

Re: 6-N-Cryption

by arabin » Fri Nov 04, 2016 3:15 pm

where is the problem in my code it shows wrong answer . why?

Code: Select all
#include <stdio.h>

void sort(long long *, int);
int isPrime(long long);
void answer(long long *, int , long long, long long);
int main() {
   
    long long eNcrypted;
   
    scanf("%lld",&eNcrypted);
   
    int numberOfEk;
    scanf("%d",&numberOfEk);
   
    long long eK[numberOfEk];
   
    long long ekN;
    for (int i = 0; i<numberOfEk; i++) {
        scanf("%lld",&ekN);
        eK[i] = ekN;
    }
   
   
    sort(eK,numberOfEk);
   
    long long bigPrimeFinder = eK[0];
   
    long long bigPrimeDivisor=1;
    long long bigPrime;
    do {
        int ans;
        long long prime;
        if (bigPrimeFinder % bigPrimeDivisor == 0) {
            prime = bigPrimeFinder/bigPrimeDivisor;
            ans = isPrime(prime);
            if (ans != 0) {
                bigPrime = prime;
                break;
            }
        }
            bigPrimeDivisor++;
    } while (bigPrimeDivisor<bigPrimeFinder);
   
    answer(eK,numberOfEk,eNcrypted,bigPrime);
   
    return 0;
}

void sort(long long *array, int size){
   
    int loopSomeMore;
   
    do {
        loopSomeMore =0;
        for (int i =0; i<size; i++) {
            if (array[i]>array[i+1]) {
                long long temp = array[i];
                array[i] = array[i+1];
                array[i+1] = temp;
                loopSomeMore = 1;
            }
        }
    } while (loopSomeMore!=0);
   
}
int isPrime(long long number){
   
    if(number < 2) return 0;
    if(number == 2) return 1;
    if(number % 2 == 0) return 0;
    for(int i=3; (i*i)<=number; i+=2){
        if(number % i == 0 ) return 0;
    }
    return 1;

}
void answer(long long *array, int numberOfEk, long long eNcrypted, long long bigPrime){
   
    long long sum=0;
   
    for (int i = 0; i<numberOfEk; i++) {
        sum += (array[i]/bigPrime);
    }
   
    long long answer = eNcrypted - sum;
   
    printf("Output: %lld\n", answer);
}
 
Posts: 28
Joined: Sat Aug 06, 2016 11:02 pm

Re: 6-N-Cryption

by froghramar » Fri Nov 04, 2016 9:49 pm

Use udebug where I have added some cases for this problem.
https://www.udebug.com/DS/2
User avatar
 
Posts: 33
Joined: Thu Jul 28, 2016 8:14 am
Location: Amar Ekushey Hall, University of Dhaka

Re: 6-N-Cryption

by arabin » Sat Nov 05, 2016 2:12 pm

Thank you so much. ya my code fails for this kind of large input. thank you for the input set.
 
Posts: 28
Joined: Sat Aug 06, 2016 11:02 pm


Who is online
Users browsing this forum: No registered users and 1 guest
cron