Mad Scientist: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Aam47
No edit summary
imported>Kmk21
m Reverted edits by Aam47 (talk) to last revision by Azd2
Line 1: Line 1:
==Introduction==
==Introduction==
A mad scientist performed a series of experiments, each having n phases. During each phase, a measurement was taken, resulting in a positive integer of magnitude at most k. The scientist knew that an individual experiment was designed in a way such that its measurements were monotonically increasing, that is, each measurement would be at least as big as all that precede it. For example, here is a sequence of measurements for one such experiment with n=13 and k=6:


Given two positive integers $n$ and $k$, find the biggest integer $i$ such that $k^{i}$ divides $n!$. Note that we are given the following bounds: $(2 ≤ n ≤ 1018, 2 ≤ k ≤ 1012)$.
1, 1, 2, 2, 2, 2, 2, 4, 5, 5, 5, 5, 6


==Solutions==
It was also the case that n was to be larger than k, and so there were typically many repeated values in the measurement sequence. Being mad, the scientist chose a somewhat unusual way to record the data. Rather than record each of n measurements, the scientist recorded a sequence P of k values defined as follows. For 1 ≤ j ≤ k, P(j) denoted the number of phases having a measurement of j or less. For example, the original measurements from the above experiment were recorded as the P-sequence:
===Idea===


The main idea is to take advantage of unique prime factorization to write $k=p_{1}^{\alpha_{1}} ... p_{n}^{\alpha_{n}}$. After explicitly computing this factorization, the task is just to find the $p_{j}^{\alpha_{j}}$ which appears in $n!$ the least number of times $i$, and this $i$ will be the answer. To do this, it clearly suffices to compute the number of times $p_{i}$ appears in $n!$. To do this, we can just be careful about how we count. In other words, we do not have to explicitly compute $n!$ or its prime factorization. The rest of this section will deal with this counting.\\
2, 7, 7, 8, 12, 13


Let $S$ be the number of times a given prime $p$ appears in $n!$, and let $l$ be the largest $l$ such that $p^{l}\le n$. Then I claim that
as there were two measurements less than or equal to 1, seven measurements less than or equal to 2, seven measurement less than or equal to 3, and so on.
\[
Unfortunately, the scientist eventually went insane, leaving behind a notebook of these P-sequences for a series of experiments. Your job is to write a program that recovers the original measurements for the experiments.
S = \sum_{i=1}^{l} \lfloor \frac{n}{p^{i}} \rfloor
\]
To see this, let $n_{i}$ be the number of integers $m \le n$ such that $i$ is the highest power of $p$ dividing $m$. Then
\[
S = \sum_{i=1}^{l}in_{i} = n_{1} + n_{2} + n_{2} + \dots + n_{l} + \dots + n_{l} = \sum_{i=1}^{l}\sum_{k=i}^{l}n_{k} = \sum_{i=1}^{l}\lfloor \frac{n}{p^{i}} \rfloor
\]


Now that we know how to compute $S_{j}$ for each prime $p_{j}$, it is easy to compute $i$ by taking
==Solutions==
\[
===Idea===
i = \min_{j} \lfloor \frac{S_{j}}{\alpha_{j}} \rfloor
Each successive integer in the input corresponds to the number of measurements at that value, where the value is defined to be the index in the input list. The general idea is to keep a counter that corresponds to the current measurement value. Then, depending on how many measurements are at that counter value, add that many number of measurements to a list of all measurements. At this point, all you have to do is print out the contents of the list holding your measurements.
\]


===Runtime===
===Runtime===
Prime factorization can be done in $\sqrt{k}$, and computing $S$ takes $log_{p_{j}} n$ time for each $p_{j}$. An extremely rough upper bound on $j$ is $\sqrt{k}$, so that the overall runtime is bounded roughly by $\sqrt{k} log n$.
n, where n is the number of phases of the experiment (the largest number given in the input list)


===Code===
===Code===
Line 31: Line 24:
import java.util.Scanner;
import java.util.Scanner;


public class DividingPowers {
/**
static Scanner in = new Scanner(System.in);
* Created by alex on 9/17/15.
*/
public class MadScientist {


public static void main(String[] args) {
    public static void main (String args[]){
long caseNum=1;
        Scanner in = new Scanner(System.in);
long a = in.nextLong();
        while(true){
for(long i = 0; i<a; i++){
            int k = in.nextInt();
long n = in.nextLong();
            if(k == 0){
long k = in.nextLong();
                break;
long ans = largestPower(n,k);
            }
System.out.printf("%d\n", ans);
            ArrayList<Integer> experiment = new ArrayList<Integer>();
caseNum++;
            int counter = 0;
}
            for(int i = 0; i<k; i++){
}
                counter++;
public static long largestPower(long n, long k){
                int numberOfExperiments = in.nextInt();
ArrayList<Long> primes = primeFactorization(k);
                for(int j=experiment.size(); j<numberOfExperiments; j++) {
long minAppearance = Long.MAX_VALUE;
                    experiment.add(counter);
for(int i = 0; i <(primes.size()+1)/2; i++){
                }
long p = primes.get(i*2);
            }
long temp = numAppearances(n,p);
if(minAppearance> temp/primes.get(i*2+1)){
minAppearance = temp/primes.get(i*2+1);
}
}
return minAppearance;
}
private static long numAppearances(long n, Long p) {
long numAppear=0;
int i = 0;
while(Math.pow(p, i)<=n){
i++;
}
i--;
for(int j = 1; j<=i; j++){
numAppear+=n/((long)Math.pow(p, j));
}
return numAppear;
}
private static ArrayList<Long> primeFactorization(long k) {
ArrayList<Long> primes = new ArrayList<Long>();
for(long i = 2; i<=Math.sqrt(k); i++){
long j = 0;
while(k%((long)Math.pow(i, j))==0){
j++;
}
if(j>1){
primes.add(i);
primes.add(j-1);
ArrayList<Long> restPrimes = primeFactorization((long)(k/(Math.pow(i, j-1))));
i=k;
for(Long a : restPrimes){
primes.add(a);
}
}
}
if(primes.size()==0&&k!=1){
primes.add(k);
primes.add((long) 1);
}
return primes;
}


            //output
            for(int m = 0; m<experiment.size()-1; m++){
                System.out.print(experiment.get(m) + " ");
            }
            System.out.print(experiment.get(experiment.size()-1));
            System.out.println();
        }
    }
}
}
</syntaxhighlight>
</syntaxhighlight>



Revision as of 19:54, 13 January 2016

Introduction

A mad scientist performed a series of experiments, each having n phases. During each phase, a measurement was taken, resulting in a positive integer of magnitude at most k. The scientist knew that an individual experiment was designed in a way such that its measurements were monotonically increasing, that is, each measurement would be at least as big as all that precede it. For example, here is a sequence of measurements for one such experiment with n=13 and k=6:

1, 1, 2, 2, 2, 2, 2, 4, 5, 5, 5, 5, 6

It was also the case that n was to be larger than k, and so there were typically many repeated values in the measurement sequence. Being mad, the scientist chose a somewhat unusual way to record the data. Rather than record each of n measurements, the scientist recorded a sequence P of k values defined as follows. For 1 ≤ j ≤ k, P(j) denoted the number of phases having a measurement of j or less. For example, the original measurements from the above experiment were recorded as the P-sequence:

2, 7, 7, 8, 12, 13

as there were two measurements less than or equal to 1, seven measurements less than or equal to 2, seven measurement less than or equal to 3, and so on. Unfortunately, the scientist eventually went insane, leaving behind a notebook of these P-sequences for a series of experiments. Your job is to write a program that recovers the original measurements for the experiments.

Solutions

Idea

Each successive integer in the input corresponds to the number of measurements at that value, where the value is defined to be the index in the input list. The general idea is to keep a counter that corresponds to the current measurement value. Then, depending on how many measurements are at that counter value, add that many number of measurements to a list of all measurements. At this point, all you have to do is print out the contents of the list holding your measurements.

Runtime

n, where n is the number of phases of the experiment (the largest number given in the input list)

Code

Solution - Java

import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by alex on 9/17/15.
 */
public class MadScientist {

    public static void main (String args[]){
        Scanner in = new Scanner(System.in);
        while(true){
            int k = in.nextInt();
            if(k == 0){
                break;
            }
            ArrayList<Integer> experiment = new ArrayList<Integer>();
            int counter = 0;
            for(int i = 0; i<k; i++){
                counter++;
                int numberOfExperiments = in.nextInt();
                for(int j=experiment.size(); j<numberOfExperiments; j++) {
                    experiment.add(counter);
                }
            }

            //output
            for(int m = 0; m<experiment.size()-1; m++){
                System.out.print(experiment.get(m) + " ");
            }
            System.out.print(experiment.get(experiment.size()-1));
            System.out.println();
        }
    }
}