排列组合——hdu 4151

特殊数

排列组合联系好题

如:9876543

先算特殊数有6位数有几种

在统计有7位时有几个

View Code
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stack>
using namespace std;

bool hash[10];

__int64 p(__int64 a,__int64 b)
{
if(a>b)return 0;
__int64 all=1;
while(a--)
{
all=all*b;
b--;
}
return all;
}

__int64 sum(__int64 n)
{
__int64 add=0,i;
for(i=0;i<n;i++)
{
if(hash[i])add++;
}
return add;
}

int main()
{
__int64 n,temp;
while(scanf("%I64d",&n)!=EOF)
{
if(n<0)
{
printf("0\n");
continue;
}
memset(hash,0,sizeof(hash));

__int64 len=0,i;
temp=n;
while(temp)
{
len++;
temp=temp/10;
}

__int64 all=0;
for(i=0;i<len-1;i++)
{
all+=9*p(i,9);
}


temp=n;
__int64 add=9,tlen=len-1;

stack<int>ss;
while(temp)
{
ss.push(temp%10);
temp=temp/10;
}

__int64 first=1;
while(!ss.empty())
{
temp=ss.top();
ss.pop();

if(temp!=0)
{
if(first==1)
{
all+=(temp-1-sum(temp))*p(tlen,add);
first=0;
}else
all+=(temp-sum(temp))*p(tlen,add);
}
else
{
// hash[0]=1;
}

if(hash[temp]==0)
hash[temp]=1;
else
break;
tlen--;
add--;
}
printf("%I64d\n",all);
}

return 0;
}



原文地址:https://www.cnblogs.com/huhuuu/p/2345281.html