0
点赞
收藏
分享

微信扫一扫

UPC——训练赛 :卡片

時小白 2022-03-25 阅读 272

问题 F: 卡片

时间限制: 1.000 Sec  内存限制: 128 MB
提交 状态

题目描述

牛牛有n张卡片,每i张卡片上有一个数字ai。牛牛在里面选出了k张,按照某种顺序依次排列成一个数。
比如牛牛选出了3,13,1这三张卡片,牛牛就可以排列成3131,3113,1331,1313,1133这五个数。
你需要帮牛牛求出对于所有选出k张卡片的方案,牛牛总共能拼成多少种不同的数字。

输入

第一行两个整数n,k,表示卡片的个数和选出卡片的张数。
接下来一行n个空格分隔的整数a1,…,an,表示卡片上的数字。

输出

输出一行一个整数,表示牛牛总共能拼成多少种不同的数字。

样例输入 Copy

【样例1】 
3 3 
3 13 1 
【样例2】 
6 4 
12 23 13 1 2 3 
【样例3】 
5 3 
11 11 11 11 11 

样例输出 Copy

【样例1】 
5 
【样例2】 
314 
【样例3】 
1 

提示

对于20%的数据,有1≤n≤6,k=1。
对于40%的数据,有1≤n≤6。
对于另20%的数据,有a1=a2=⋯=an。
对于100%的数据,有1≤n≤10,1≤k≤4,1≤ai≤99。

 解题方法:哈希字符串 + 一点点暴力。

Code:

#include <iostream>
#include <stdio.h>
#include <set>

using namespace std;

typedef unsigned long long ULL;

const int N = 100010, P = 131;;

int n, k, hh;
string s[N] , t;
ULL p[N], h[N];
ULL a[N];
set<ULL> w;

ULL get(int l, int r)
{
return h[r] - h[l - 1] * p[r - l + 1];
}

signed main()
{
cin >> n >> k;
for(int i = 1; i <= n; i ++ ) cin >> s[i];
p[0] = 1;
for(int i = 1; i <= 10; i ++ ) p[i] = p[i - 1] * P;
if(k == 3)
{
for(int i = 1; i <= n; i ++ )
{
for(int j = 1; j <= n; j ++ )
{
if(i == j) continue;
for(int o = 1; o <= n; o ++ )
{
if(o == j || o == i) continue;
t.clear();
t += s[i] + s[j] + s[o];
for(int q = 1; q <= t.size(); q ++ ) h[q] = h[q - 1] * P + t[q - 1];
ULL x = get(1, t.size());
w.insert(x);
}
}
}
cout << w.size() << endl;
}
else if(k == 4)
{
for(int i = 1; i <= n; i ++ )
{
for(int j = 1; j <= n; j ++ )
{
if(i == j) continue;
for(int o = 1; o <= n; o ++ )
{
if(o == i || o == j) continue;
for(int g = 1; g <= n; g ++ )
{
if(g == o || g == j || g == i) continue;
t.clear();
t += s[i] + s[j] + s[o] + s[g];
for(int q = 1; q <= t.size(); q ++ ) h[q] = h[q - 1] * P + t[q - 1];
ULL x = get(1, t.size());
w.insert(x);
}
}
}
}
cout << w.size() << endl;
}
else if(k == 2)
{
for(int i = 1; i <= n; i ++ )
{
for(int j = 1; j <= n; j ++ )
{
if(j == i) continue;
t.clear();
t += s[i] + s[j];
for(int q = 1; q <= t.size(); q ++ ) h[q] = h[q - 1] * P + t[q - 1];
ULL x = get(1, t.size());
w.insert(x);
}
}
cout << w.size() << endl;
}
else if(k == 1)
{
for(int i = 1; i <= n; i ++ )
{
t.clear();
t += s[i];
for(int q = 1; q <= t.size(); q ++ ) h[q] = h[q - 1] * P + t[q - 1];
ULL x = get(1, t.size());
w.insert(x);
}
cout << w.size() << endl;
}
}
举报

相关推荐

0 条评论