Mad Scientist: Difference between revisions

From programming_contest
Jump to navigation Jump to search
imported>Azd2
No edit summary
imported>Aam47
No edit summary
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:


1, 1, 2, 2, 2, 2, 2, 4, 5, 5, 5, 5, 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)$.


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:
==Solutions==
===Idea===


2, 7, 7, 8, 12, 13
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.\\


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.
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
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
\]


==Solutions==
Now that we know how to compute $S_{j}$ for each prime $p_{j}$, it is easy to compute $i$ by taking
===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.
i = \min_{j} \lfloor \frac{S_{j}}{\alpha_{j}} \rfloor
\]


===Runtime===
===Runtime===
n, where n is the number of phases of the experiment (the largest number given in the input list)
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$.


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


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


    public static void main (String args[]){
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
long caseNum=1;
        while(true){
long a = in.nextLong();
            int k = in.nextInt();
for(long i = 0; i<a; i++){
            if(k == 0){
long n = in.nextLong();
                break;
long k = in.nextLong();
            }
long ans = largestPower(n,k);
            ArrayList<Integer> experiment = new ArrayList<Integer>();
System.out.printf("%d\n", ans);
            int counter = 0;
caseNum++;
            for(int i = 0; i<k; i++){
}
                counter++;
}
                int numberOfExperiments = in.nextInt();
public static long largestPower(long n, long k){
                for(int j=experiment.size(); j<numberOfExperiments; j++) {
ArrayList<Long> primes = primeFactorization(k);
                    experiment.add(counter);
long minAppearance = Long.MAX_VALUE;
                }
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:12, 3 December 2015

Introduction

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)$.

Solutions

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.\\

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 \[ 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 \[ i = \min_{j} \lfloor \frac{S_{j}}{\alpha_{j}} \rfloor \]

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$.

Code

Solution - Java

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

public class DividingPowers {
	static Scanner in = new Scanner(System.in);

	public static void main(String[] args) {
		long caseNum=1;
			long a = in.nextLong();
			for(long i = 0; i<a; i++){
				long n = in.nextLong();
				long k = in.nextLong();
				long ans = largestPower(n,k);
				System.out.printf("%d\n", ans);
				caseNum++;
			}
	}
	public static long largestPower(long n, long k){
		ArrayList<Long> primes = primeFactorization(k);
		long minAppearance = Long.MAX_VALUE;
		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;
	}
	
	

}