POJ 1496 1850

来源:互联网 发布:阿里云分布式文件系统 编辑:程序博客网 时间:2024/04/30 20:35

组合数学额

买一送一额

//============================================================================

// Name        : hello.cpp
// Author      : key
// Version     : ∞
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================



#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <algorithm>
using namespace std;


//POJ 1496 1850

int com(int n, int r)// return C(n, r)
{
    if( n-r > r )
        r = n-r; // C(n, r) = C(n, n-r)
    
    int i, j, s = 1;
    
    for( i=0, j=1; i < r; ++i )
    {
        s *= (n-i);
        for( ; j <= r && s%j == 0; ++j )
            s /= j;
    }
    return s;
}
char str[15];
int main()
{
    int i,j;
    int flat = 0;
    while(scanf("%s",str)!=EOF){
        flat = 0;
        int len=strlen(str);
        for(i=0;str[i];i++)
        {
            if(flat) break;
            for(j=i+1;str[j];j++)
            {
                if(flat) break;
                if(str[i]>str[j])
                {
                    printf("0\n");
                    flat = 1;
                }
            }
        }
        if(flat) continue;
        
        int res = 0;   
        
        for( i = 1; i<=len; i ++)
            res += com(26, i);   
        
        for( i=0; i<len; i ++ )   
            res -= com(26-(str[i]-'a'+1), len-i);   
        
        printf("%d\n",res);  
    }
    return 0;
}