// Errichto
// Div2-Easy
public class BearSong {
public int countRareNotes(int[] notes) {
final int lim = 1005;
int cnt[] = new int[lim];
int n = notes.length;
for(int i = 0; i < n; ++i)
cnt[notes[i]]++;
int ans = 0;
for(int i = 0; i < lim; ++i)
if(cnt[i] == 1)
++ans;
return ans;
}
}
// Div2-Med
public class BearSlowlySorts {
boolean isSorted(int[] w) {
for(int i = 0; i < w.length - 1; ++i)
if(w[i] > w[i+1]) return false;
return true;
}
public int minMoves(int[] w) {
int n = w.length;
int memo[] = new int[n];
for(int i = 0; i < n; ++i) memo[i] = w[i];
int ans = -1;
for(int i = 0; i <= 1; ++i) {
for(int j = 0; j < n; ++j) w[j] = memo[j];
int which = i;
int a = 0;
while(!isSorted(w)) {
if(which
== 1) Arrays.
sort(w,
0, n
- 1); ++a;
which = 1 - which;
}
if(a < ans || ans == -1) ans = a;
}
return ans;
}
}
// Div2-Hard
public class BearPermutations2 {
public int getSum(int N, int mod) {
long bin[][] = new long[N+1][N+1];
long fac[] = new long[N+1];
fac[0] = 1;
for(int i = 0; i <= N; ++i) {
if(i > 0) fac[i] = fac[i-1] * i % mod;
bin[i][0] = 1;
for(int j = 1; j <= i; ++j)
bin[i][j] = (bin[i-1][j-1] + bin[i-1][j]) % mod;
}
long dp[] = new long[N+1];
for(int n = 1; n <= N; ++n) {
// where the smallest/biggest number goes
for(int where = 1; where <= n; ++where) {
if(where == 1 || where == n) {
dp[n] += dp[n-1];
dp[n] %= mod;
}
else {
int n1 = where - 1;
int n2 = n - where;
for(int rep = 0; rep <= 1; ++rep) {
int tmp = n1; n1 = n2; n2 = tmp;
dp[n] += dp[n1] * bin[n1+n2][n1] % mod * fac[n2];
dp[n] %= mod;
long s = n1 * (n1 + 1) / 2;
dp[n] += s * bin[n1+n2][n1] % mod * fac[n2] % mod * fac[n1-1];
dp[n] %= mod;
}
}
}
}
return (int) dp[N];
}
}