Skip to content

Instantly share code, notes, and snippets.

@chermehdi
Created October 30, 2016 19:54
Show Gist options
  • Save chermehdi/17722db2b0888c30bc88bcccb50d7488 to your computer and use it in GitHub Desktop.
Save chermehdi/17722db2b0888c30bc88bcccb50d7488 to your computer and use it in GitHub Desktop.
Acpc2014 problem C Solution
package problems;
/**
* @Author Mehdi Maick
* Created on 30/10/2016.
*/
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class scorifyC {
static long[][] memo;
static ArrayList<Pair> tab;
static long S;
static int n;
static long f(int i, int sum) {
if (i < 0)
return 0L;
if (memo[i][sum] != -1)
return memo[i][sum];
long ans = 0L;
if (S % (sum + tab.get(i).s) == 0 && tab.get(i).f >= (S / (sum + tab.get(i).s))) {
ans = 1L;
}
ans += f(i - 1, sum);
if (sum + tab.get(i).s <= S) {
ans += f(i - 1, sum + tab.get(i).s);
}
memo[i][sum] = ans;
return ans;
}
public static void solve(FastReader fs, PrintWriter pw) {
int t = fs.nextInt();
for (int ii = 1; ii <= t; ii++) {
S = fs.nextLong();
n = fs.nextInt();
memo = new long[n + 1][(int) S + 1];
tab = new ArrayList<Pair>();
for (int i = 0; i < n; i++) {
tab.add(new Pair(fs.nextInt(), fs.nextInt()));
}
for (int i = 0; i <= n; i++) {
Arrays.fill(memo[i], -1);
}
Collections.sort(tab);
long ans = f(n - 1, 0);
pw.println("Case " + ii + ": " + ans);
}
}
public static void main(String[] args) throws Exception {
FastReader fs = new FastReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
solve(fs, pw);
fs.close();
pw.close();
}
static class Pair implements Comparable<Pair> {
int f, s;
public Pair(int f, int s) {
this.f = s;
this.s = f;
}
public String toString() {
return f + " " + s;
}
@Override
public int compareTo(Pair o) {
if (f == o.f) {
return s - o.s;
}
return f - o.f;
}
}
static class FastReader {
BufferedReader reader;
StringTokenizer st;
FastReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
st = null;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char nextChar() {
return next().charAt(0);
}
int[] nextIntArray(int n) {
int[] arr = new int[n];
int i = 0;
while (i < n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArray(int n) {
long[] arr = new long[n];
int i = 0;
while (i < n) {
arr[i++] = nextLong();
}
return arr;
}
int[] nextIntArrayOneBased(int n) {
int[] arr = new int[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextInt();
}
return arr;
}
long[] nextLongArrayOneBased(int n) {
long[] arr = new long[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = nextLong();
}
return arr;
}
void close() {
try {
reader.close();
} catch (IOException e) {
System.err.println("There's been an error trying closing the reader ");
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment