Google-APAC2015-"Password Attacker"
2014-11-05 01:57
344 查看
昨天在焦虑中度过了大部分时间,然后刷了10+集火影来弥补被狗吃掉的童年。前天去了De Young美术馆,下一篇blog再po一点图吧。
反正我觉得焦虑是最没有意义的事情。加油啊!
========================================
原题:password attacker
# 1 题目理解:
假设用户使用M个不同的key,去组成长度为N的密码(M<=N),那么有多少种不同的密码。
来看一下test cases。
输入是:
1 1
3 4
5 5
输出是:
Case #1: 1
Case #2: 36
Case #3: 120
比如说M=3, N=4,那么就有36种密码组合。
注意,因为有可能数字太大会溢出,所以所有结果要对1000000007求余。
========================================
#2 算法:
使用dynamic programing。dp[i][j]表示使用i个不同的key去排列长度为j的密码的所有排列方法。
状态转移方程是:
dp[i][j] = i * (dp[i-1][j-1] * j + dp[i][j-1])
这样理解:
考虑长度为j的密码,最后一位肯定是i个key里面的一员,因此有i种情况;
最后一位决定之后,考虑前j-1位,有可能前面没有用过这个key,那么有dp[i-1][j-1]种情况;有可能前面已经使用过这个key,那么有dp[i][j-1]种情况。
========================================
#3 数据结构:
二维数组dp[M+1][N+1]
========================================
#4 代码:
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
File inFile = new File("A-large-practice.in");
File outFile = new File("A-large-practice.out");
try {
BufferedReader br = new BufferedReader(new FileReader(inFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
int T = Integer.parseInt(br.readLine());
for (int i = 1; i <= T; i++) {
String s = br.readLine();
String[] parts = s.split("\\s");
int M = Integer.parseInt(parts[0]);
int N = Integer.parseInt(parts[1]);
bw.write("Case #" + i + ": " + solve(M, N) + "\n");
}
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/* dynamic programing */
public static long solve(int M, int N) {
long mod = 1000000007;
long[][] dp = new long[M+1][N+1];
// base case
for (int j = 1; j <= N; j++) {
dp[1][j] = 1;
}
// dp
for (int i = 2; i <= M; i++) {
for (int j = i; j <= N; j++) {
dp[i][j] = i * (dp[i-1][j-1] + dp[i][j-1]);
dp[i][j] %= mod;
}
}
return dp[M]
;
}
}
反正我觉得焦虑是最没有意义的事情。加油啊!
========================================
原题:password attacker
# 1 题目理解:
假设用户使用M个不同的key,去组成长度为N的密码(M<=N),那么有多少种不同的密码。
来看一下test cases。
输入是:
1 1
3 4
5 5
输出是:
Case #1: 1
Case #2: 36
Case #3: 120
比如说M=3, N=4,那么就有36种密码组合。
注意,因为有可能数字太大会溢出,所以所有结果要对1000000007求余。
========================================
#2 算法:
使用dynamic programing。dp[i][j]表示使用i个不同的key去排列长度为j的密码的所有排列方法。
状态转移方程是:
dp[i][j] = i * (dp[i-1][j-1] * j + dp[i][j-1])
这样理解:
考虑长度为j的密码,最后一位肯定是i个key里面的一员,因此有i种情况;
最后一位决定之后,考虑前j-1位,有可能前面没有用过这个key,那么有dp[i-1][j-1]种情况;有可能前面已经使用过这个key,那么有dp[i][j-1]种情况。
========================================
#3 数据结构:
二维数组dp[M+1][N+1]
========================================
#4 代码:
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
File inFile = new File("A-large-practice.in");
File outFile = new File("A-large-practice.out");
try {
BufferedReader br = new BufferedReader(new FileReader(inFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(outFile));
int T = Integer.parseInt(br.readLine());
for (int i = 1; i <= T; i++) {
String s = br.readLine();
String[] parts = s.split("\\s");
int M = Integer.parseInt(parts[0]);
int N = Integer.parseInt(parts[1]);
bw.write("Case #" + i + ": " + solve(M, N) + "\n");
}
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/* dynamic programing */
public static long solve(int M, int N) {
long mod = 1000000007;
long[][] dp = new long[M+1][N+1];
// base case
for (int j = 1; j <= N; j++) {
dp[1][j] = 1;
}
// dp
for (int i = 2; i <= M; i++) {
for (int j = i; j <= N; j++) {
dp[i][j] = i * (dp[i-1][j-1] + dp[i][j-1]);
dp[i][j] %= mod;
}
}
return dp[M]
;
}
}
相关文章推荐
- Google-APAC2015-"New Years Eve"
- Google-APAC2015-"Seven-segment display"
- CODE JAM APAC 2015 round B: Problem A. Password Attacker(小球放进盒子的计数问题)
- Google-APAC2015-"super 2048"
- Google-APAC2015-"cut tiles"
- Google APAC test 2015 Round B Problem A - Password Attacker
- Google APAC test 2015 Round B Problem B - New Years Eve
- [Google APAC 2015]Round A.Problem B.Super 2048
- Google APAC test 2015 Round B Problem C - Card Game
- Google中国2015校园招聘笔试Round D APAC Test Problem A. Cube IV
- Google APAC test 2015 Round B Problem D-Parentheses Order
- Google中国2015校园招聘笔试Round D APAC Test Problem B. GBus count
- Google-APAC2015-Addition (2)
- Google中国2015校园招聘笔试Round D APAC Test Problem C. Sort a scrambled itinerary
- Google中国2015校园招聘笔试Round D APAC Test Problem D. Itz Chess
- N球M盒问题 & Password Attacker google code jam
- Catalan数和应用 & 2015 google APAC round 2 problem D 括号配对问题
- Google-APAC2015-Addition (1)
- Google 2015 APAC Round A Problem C - Addition
- [ Google APAC 2015 University Graduates Test ] Round C APAC Test