CodeForces_1267K Key Storage(组合数学,数论)

Key Storage

time limit per test:3 seconds
memory limit per test:256 megabytes
Problem Description

Karl is developing a key storage service. Each user has a positive integer key.

Karl knows that storing keys in plain text is bad practice. So, instead of storing a key, he decided to store a fingerprint of a key. However, using some existing fingerprint algorithm looked too boring to him, so he invented his own one.

Karl’s fingerprint is calculated by the following process: divide the given integer by 2, then divide the result by 3, then divide the result by 4, and so on, until we get a result that equals zero (we are speaking about integer division each time). The fingerprint is defined as the multiset of the remainders of these divisions.

For example, this is how Karl’s fingerprint algorithm is applied to the key 11: 11 divided by 2 has remainder 1 and result 5, then 5 divided by 3 has remainder 2 and result 1, and 1 divided by 4 has remainder 1 and result 0. Thus, the key 11 produces the sequence of remainders [1,2,1] and has the fingerprint multiset {1,1,2}.

Ksenia wants to prove that Karl’s fingerprint algorithm is not very good. For example, she found that both keys 178800 and 123456 produce the fingerprint of {0,0,0,0,2,3,3,4}. Thus, users are at risk of fingerprint collision with some commonly used and easy to guess keys like 123456.

Ksenia wants to make her words more persuasive. She wants to calculate the number of other keys that have the same fingerprint as the keys in the given list of some commonly used keys. Your task is to help her.

Input

The first line contains an integer t (1 t 50000) — the number of commonly used keys to examine. Each of the next t lines contains one integer ki ( 1 ki 1018 ) — the key itself.

Output

For each of the keys print one integer — the number of other keys that have the same fingerprint.

Sample Input

3
1
11
123456

Sample Output

0
1
127

题意

一个数k,将其先对2取余,然后除2,然后对3取余,再除3,一直到它为0为止,运算时所有的余数可以组成一个集合(只保证无序,可以不互异),问有多少个不同的数,进行同样操作后得到的数会和k得到的集合一样。

题解:

假设k除以i余数为ai,那么k实际上就可以表示为 1 ! ∗ a 2 + 2 ! ∗ a 3 + . . . + ( i − 1 ) ! ∗ a i 1!*a_2+2!*a_3+...+(i-1)!*a_i 1!a2+2!a3+...+(i1)!ai (最后一个位置余数大于0)。可以先求出k的余数多集,然后算出该集合能得到的不同的数有多少个。设数组bbi 代表余数集合中余数为i的数有多少个。那么余数i可以放在除数大于i的位置。可以考虑,从大到小分配所有余数的位置,将所有可以考虑的位置数量相乘即可。
例如: 123456的余数集合为{0,0,0,0,2,3,3,4},除数分别为2,3,4,5,6,7,8,9。余数4可以放在除数5,6,7,8,9的位置,共有 C ( 5 , 1 ) C(5,1) C(5,1)种。接下来余数3可以放在除数4,5,6,7,8,9的位置,但之前已经放过一个余数4了,所以共有 C ( 5 , 2 ) C(5,2) C(5,2)种。余数2可以在除数3,4,5,6,7,8,9位置,但已放过余数3,3,4,所以共有 C ( 4 , 1 ) C(4,1) C(4,1)种。剩下4个位置还有4个0,共有 C ( 4 , 4 ) C(4,4) C(4,4)种可能,所有共有 C ( 5 , 1 ) ∗ C ( 5 , 2 ) ∗ C ( 4 , 1 ) ∗ C ( 4 , 4 ) C(5,1)*C(5,2)*C(4,1)*C(4,4) C(5,1)C(5,2)C(4,1)C(4,4)个不同的数。
但因为上面没有考虑除数9的位置对应为0的情况,所以需要减去这种情况,将一个0放到最高,然后再进行一遍上述操作即可。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctype.h>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-6
 
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 100100;
const int mod = 10007;
int a[24];
LL solve(int num);
LL C(int n, int m);

int main()
{
	int t, n, i, j, num;
	LL k;
	scanf("%d", &t);
	while(t--)
	{
		scanf("%I64d", &k);
		i = 2, num = 0;
		memset(a, 0, sizeof(a));
		while(k)
		{
			a[k%i]++;
			k /= i;
			i++;
			num++;
		}
		LL ans = solve(num);
		//减去最高位余数为0的情况。
		if(a[0]){
			a[0]--;
			ans -= solve(num-1);
		}
		printf("%I64d\n", ans-1);
	}
	return 0;
}

LL solve(int num)
{
	LL res = 1;
	//sum:已用过的位置数量
	int sum = 0;
	for(int i=num+1;i>=1;i--)
	{
		//num+1-i-sum:余数i可以放的位置数量
		if(num+1-i-sum < a[i])return 0LL;
		res *= C(num+1-i-sum, a[i]);
		sum += a[i];
	}
	return res;
}
//求C(n,m)
LL C(int n, int m)
{
	LL c = 1LL;
	for(int i=0;i<m;i++)
		c = c*(n-i)/(i+1);
	return c;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值