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 ...
随机推荐
- jQuery Multi-TouchWipe / Multi-TouchZoom
jQuery Multi-TouchWipe / Multi-TouchZoom是小弟参照WipeTouch扩展出来的针对多点触屏划动而改写出来的Jquery插件,可以为dom上的两手指触屏划动拨入( ...
- Open CV 图像显示(1)
演示:读入一张图片,并显示 #include "stdafx.h" #include <opencv2/core/core.hpp> #include ...
- JavaScript无缝滚动
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- javaScript入门第一天
JavaScript提供七种不同的data types(数据类型),它们是undefined(未定义), null(空), boolean(布尔型), string(字符串), symbol(符号), ...
- BZOJ1001 [BeiJing2006]狼抓兔子(平面图最小割转最短路)
..和HDU3870类似..注意n=1和m=1的情况. #include<cstdio> #include<cstring> #include<queue> #in ...
- sqlSQL2008如何创建定时作业
SQL2008如何创建定时作业?此方法也适应于Sql Server2005数据库,有兴趣的可以来看下! 1.打开[SQL Server Management Studio],在[对象资源管理器]列表中 ...
- ural 1072. Routing
1072. Routing Time limit: 1.0 secondMemory limit: 64 MB There is a TCP/IP net of several computers. ...
- .htaccess
一.在Apache配置中启用Rewrite 打开配置文件httpd.conf: 1.启用rewrite# LoadModule rewrite_module modules/mod_rewrite.s ...
- Windows下查看端口冲突的进程
在tomcat部署中,经常遇到80端口被占用,下面总结了两条查看端口进程的方法. 查看端口方法: netstat -aon|findstr "80" 如图,使用80端口的进程列 ...
- topcoder SRM 618 DIV2 MovingRooksDiv2
一开始Y1,Y2两个参数看不懂,再看一遍题目后才知道,vector<int>索引代表是行数,值代表的是列 此题数据量不大,直接深度搜索即可 注意这里深度搜索的访问标识不是以前的索引和元素, ...