Password Attacker
Passwords are widely used in our lives: for ATMs, online forum logins, mobile device unlock and door access. Everyone cares about password security. However, attackers always find ways to steal our passwords. Here is one possible situation:
Assume that Eve, the attacker, wants to steal a password from the victim Alice. Eve cleans up the keyboard beforehand. After Alice types the password and leaves, Eve collects the fingerprints on the keyboard. Now she knows which keys are used in the password. However, Eve won't know how many times each key has been pressed or the order of the keystroke sequence.
To simplify the problem, let's assume that Eve finds Alice's fingerprints only occurs on M keys. And she knows, by another method, that Alice's password contains N characters. Furthermore, every keystroke on the keyboard only generates a single, unique character. Also, Alice won't press other irrelevant keys like 'left', 'home', 'backspace' and etc.
Here's an example. Assume that Eve finds Alice's fingerprints on M=3 key '3', '7' and '5', and she knows that Alice's password is N=4-digit in length. So all the following passwords are possible: 3577, 3557, 7353 and 5735. (And, in fact, there are 32 more possible passwords.)
However, these passwords are not possible:
1357 // There is no fingerprint on key '1'
3355 // There is fingerprint on key '7',
so '7' must occur at least once.
357 // Eve knows the password must be a 4-digit number.
With the information, please count that how many possible passwords satisfy the statements above. Since the result could be large, please output the answer modulo 1000000007(109+7).
Input
The first line of the input gives the number of test cases, T.
For the next T lines, each contains two space-separated numbers M and N, indicating a test case.
Output
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the total number of possible passwords modulo 1000000007(109+7).
Limits
Small dataset
T = 15.
1 ≤ M ≤ N ≤ 7.
Large dataset
T = 100.
1 ≤ M ≤ N ≤ 100.
Sample
Input
4
1 1
3 4
5 5
15 15
Output
Case #1: 1
Case #2: 36
Case #3: 120
Case #4: 674358851
google在线笔试题。这题一直没做出来。有人说有公式,看到大牛们提交的代码又觉得像是dp。后来学了生成函数之后,觉得应该是一道指数型生成函数的题。
#include <iostream>
#include <cstdio>
#include <vector> using namespace std;
const double epi = 0.000001;
int frac(int k) {
int ans = ;
for (int i = ; i <= k; ++i) {
ans *= i;
}
return ans;
} int enumPassword(int n, int m) {
vector<vector<double> > params(, vector<double>(n + , ));
params[][] = ;
int cur = , next = ; for (int i = ; i < m; ++i) {
params[next].assign(n + , );
for (int j = ; j <= n; ++j) {
if (params[cur][j] < epi) continue;
for (int k = ; k + j <= n; ++k) {
params[next][k + j] = params[next][k + j] + params[cur][j] * / frac(k);
}
}
cur = !cur; next = !next;
} return params[cur][n] * frac(n);
} int main(int argc, char** argv) {
if (argc < ) return -;
freopen(argv[], "r", stdin);
int test;
scanf("%d", &test);
for (int i = ; i < test; ++i) {
int m, n;
scanf("%d%d", &m, &n);
cout << "Case #" << i + << ": " << enumPassword(n, m) << endl;
} return ;
}
但是数太大,要取模。除操作不能直接除模。
在网上搜到一个定理:
定理12.2:设$a_n$,$b_n$的指数生成函数分别为f(x)和g(x),则:
$f(x)*g(x) = \sum_{n=0}^{\infty}c_n\frac{x^n}{n!}, c_n = \sum_{k=0}^{n}C(n,k)a_kb_{n-k}$。
对应到我们这里,Line 25里就变成params[cur][j]*1*C(j+k, k),params[cur][j]对应的是$\frac{x^j}{j!}$的系数,1对应的是$\frac{x^k}{k!}$,所以乘以的就是C(j+k, k)了。
代码如下:
#include <iostream>
#include <cstdio>
#include <vector> using namespace std;
enum {MOD = };
typedef long long llong;
llong combination[][];
void getCombination() {
for (int i = ; i <= ; ++i) {
for (int j = ; j <= i; ++j) {
if (j == ) {
combination[i][j] = ;
} else {
combination[i][j] = (combination[i - ][j] + combination[i - ][j - ]) % MOD;
}
}
}
} llong enumPassword(int n, int m) {
vector<vector<llong> > params(, vector<llong>(n + , ));
params[][] = ;
int cur = , next = ; for (int i = ; i < m; ++i) {
params[next].assign(n + , );
for (int j = ; j <= n; ++j) {
if (params[cur][j] == ) continue;
for (int k = ; k + j <= n; ++k) {
params[next][k + j] = (params[next][k + j] + params[cur][j] * combination[j + k][k]) % MOD;
}
}
cur = !cur; next = !next;
} return params[cur][n];
} int main(int argc, char** argv) {
if (argc < ) return -;
freopen(argv[], "r", stdin);
if (argc >= ) freopen(argv[], "w", stdout);
getCombination();
int test;
scanf("%d", &test);
for (int i = ; i < test; ++i) {
int m, n;
scanf("%d%d", &m, &n);
//cout << "Case #" << i + 1 << ": " << enumPassword(n, m) << endl;
printf("Case #%d: %lld\n", i + , enumPassword(n, m));
} return ;
}
注意这里求组合数要用递推公式来求,这样可以在运算中取模,避免溢出。
$C(n, m) = C(n - 1, m) + C(n - 1, m - 1)$。
以后指数型生成函数的题都可以这么做。get!
Password Attacker的更多相关文章
- [GCJ]Password Attacker
https://code.google.com/codejam/contest/4214486/dashboard#s=p0 排列组合.DP递推式,如下代码.dp[m][n]表示长度为n的字符串里有m ...
- 【DP】组合数字
Password Attacker 题意就是给 M 个关键字,组合成 N 字符长度的结果,每一个关键字都必须在 N 位的字符中出现,有多少种可能结果. 范围 1 ≤ M ≤ N ≤ 100. 举例假设 ...
- hdu3625
hdu3625 题意: 酒店发生一起谋杀案.作为镇上最好的侦探,您应该立即检查酒店的所有N个房间.但是,房间的所有门都是锁着的,钥匙刚锁在房间里,真是个陷阱!您知道每个房间里只有一把钥匙,并且所有可能 ...
- A complex 16-Level XSS Challenge
A complex 16-Level XSS Challenge, held in summer 2014 (+1 Hidden Level) Index Level 0 Level 1 Level ...
- bitbar 网站攻击实验
实验环境 https://github.com/TouwaErioH/security/tree/master/web1 Windows10 Oracle VM VirtualBox Ubuntu16 ...
- Spring Security(三十三):10.3 Password Encoding
Spring Security’s PasswordEncoder interface is used to support the use of passwords which are encode ...
- ASP.NET OAuth Authorization - Difference between using ClientId and Secret and Username and Password
What I don't fully understand is the use of ClientId and Secret vs Username and Password. The code ...
- MetInfo Password Reset Poisoning By Host Header Attack
if we know some user's email, the we will can reset the user's email by host header attack. The atta ...
- 打开程序总是会提示“Enter password to unlock your login keyring” ,如何成功关掉?
p { margin-bottom: 0.1in; line-height: 120% } 一.一开始我是按照网友所说的 : rm -f ~/.gnome2/keyrings/login.keyrin ...
随机推荐
- hdu 2102 A计划-bfs
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- jquery.color.js的使用
Jquery本身不支持变色,Jquery Color.js弥补了这缺陷并为animate动画赋予变色效果,如下红变蓝后执行回调再由蓝变红. <!DOCTYPE html> <html ...
- UpdatePanel的使用
一.UpdatePanel的结构 <asp:ScriptManager ID="ScriptManager1" runat="server" > & ...
- linux内存分配
在linux的内存分配机制中,优先使用物理内存,当物理内存还有空闲时(还够用),不会释放其占用内存,就算占用内存的程序已经被关闭了,该程序所占用的内存用来做缓存使用,对于开启过的程序.或是读取刚存取过 ...
- soapui中文操作手册(六)----创建REST Testing
首先,通过选择文件菜单中的“新建REST项目”选项创建从文件菜单中一个新的REST项目: 指定服务端点场下谷歌地图API网址: http://maps.googleapis.com/maps/api/ ...
- 建立php开发环境(XAMPP + Xdebug+Zend Studio)
1. 安装XAMPP和Zend Studio Zend Studio下载地址: http://pan.baidu.com/s/1o6BjvAE XAMPP 下载地址: http://pan.baidu ...
- BZOJ 1925[Sdoi2010]地精部落 题解
题目大意: 1~n的排列中,要任意一个数要么比它左右的数都大或小,求所有的方案数. 思路: 主要思路:离散. 三个引理: ①在n->n-1的转化过程中,我们删除了一个点后,我们可以将n-1个点视 ...
- BZOJ1858[Scoi2010]序列操作 题解
题目大意: 有一个01序列,现在对于这个序列有五种变换操作和询问操作: 0 a b 把[a, b]区间内的所有数全变成0:1 a b 把[a, b]区间内的所有数全变成1:2 a b 把[a,b]区间 ...
- CDOJ 1431 不是图论 Label:Tarjan || Kosarajn
Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Description 给出一个nn个点, ...
- 【BZOJ】2086: [Poi2010]Blocks
题意 \(n(1 \le n \le 1000000)\)个数\(a_i(a_i \le 10^9)\).\(m(1 \le m \le 50)\)次询问,每次给出一个\(k(k \le 10^9)\ ...