Problem A. Password Attacker

Problem A. Password Attacker

This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
Small input
8 points
Judge's response for last submission: Correct.
Large input
13 points
Judge's response for last submission: Correct.

Problem

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 Mkeys. 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 
 

Output 
 
4
1 1
3 4
5 5
15 15
Case #1: 1
Case #2: 36
Case #3: 120
Case #4: 674358851

第一次看到自己寫的代碼比別人的短,而且思路直接,紀念一下。
題意:m個不同字母,填充長度爲n的字符串,m個字符必須都用上,不夠的部分可以用這m個字符中的任意字符重複填充,問可能有多少種
字符串。
思路:開始想用排列組合一步搞定,發現不行,然後就想到可以遞推解決,對並不是動態規劃。
遞推狀態如下定義:f[i][j]表示長度爲 i的字符串,用j種字符填充的方法數,
遞推關係如下:
f[i][j] = f[i-1][j]×j + f[i-1][j-1]×j


f[i-1][j]×j表示在長度爲i-1的字符串後面再加一個位置,那麼這個位置可以選j種字符中的任意一種填充。
所以這樣得到的字符串有f[i-1][j]×j種,


f[i-1][j-1]×j 表示長度爲i-1的字符串再添一個位置,這個位置需要用之前沒有用過的顏色,就是j種
顏色裏面選一種,有j種選法所以要 乘 j
代碼如下:


#include <bits/stdc++.h>
const int N = 110;
typedef long long ll;
const int mod = 1e9 + 7;
using namespace std;
ll f[N][N];
void run()
{
  int n, m;
  scanf("%d%d", &m, &n);
  printf("%lld\n", f[n][m]);
}
int main()
{
#ifndef ONLINE_JUDGE  
    freopen("in.txt","r" ,stdin);
#endif 
    int T, cas = 1;
    for (int i = 1; i <= 100; i++)
      f[i][1] = 1;

    for (int i = 2; i <= 100; i++)
      for (int j = 1; j <= 100; j++)
      {
        f[i][j] = f[i-1][j] * j % mod;
        f[i][j] += f[i-1][j-1] * j %mod;
        f[i][j] %= mod;
      }
    scanf("%d", &T);
    while (T--)
    {
      printf("Case #%d: ", cas++);
      run();
    }
    return 0; 
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章