K. 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 ≤ 10的18次方 ) — the key itself.
题意:一个数,将其对2取余,除2后再对3取余,除3后对四取余,以次类推直到k为0。每次
余数组成一个集合(余数可以重复),问有多少个不同数,操作后得到的数与k得到
的余数集合一样,
eg 11-(1,2,1) ,(1,1,2)也是可以的 对应15,是一个。
但是(2,1,1)不可能 因为第一个数对2取余,怎么可能余数为2!
思路:
实际上题目转化为:像1223456-(0,0,0,0,2,3,3,4)的集合排列有多少种呢;
余数结: 上面对2,3,4,5,6,7,8,9 取余得到的结果
那么有了些约束条件:
- 集合数组 a[i]<=i+1; 每个数取余都要小于本身。
- 数组最后一个数不能为0,因为最后一个数对k除之后为0,那么余数不能为0.(可以对11推下,就明白了。)
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<ll> a;
void zw(ll n){
a.clear();
ll ans=2;
while(n){
a.push_back(n%ans);
n/=ans;
ans++;
} //将余数集合存入a数组(集合可以重复)
sort(a.begin(),a.end());
}
ll solve (){
ll sum=1;
for(int i=0;i<a.size();i++){
ll ans=0;
for(int j=0;j<a.size();j++){
if(a[j]<=i+1) ++ans;
else
break;
}
sum*=(ans-i);
//如集合: 0 0 0 0 2 3 3 4
//除k 2 3 4 5 6 7 8 9
//可以取的个数 4 4 5 5 4 3 2 1 (ans-i)
}
//cout<<sum<<endl<<endl;
ll ans =1;
for(int i=1;i<a.size();i++){
if(a[i-1]==a[i]){
ans++;
sum/=ans; //去重复,四个0很多排序方式,但只要一种
}
else
ans =1;
}
return sum;
}
ll n;
int main (){
ll T;
cin>>T;
while(T--){
cin>>n;
zw(n);
//for(int i=0;i<a.size();i++){cout<<a[i];}
ll sum = solve();
if(a[0]==0){
a.erase(a.begin());
sum-=solve();
}
cout<<sum-1<<endl;
}
return 0;
}