poj3226

高精度组合数学

View Code
import java.io.*;
import java.util.*;
import java.math.*;

public class Main {
public static void main(String[] args) throws FileNotFoundException
{
Scanner cin = new Scanner(new BufferedInputStream(System.in));
//Scanner cin = new Scanner(new FileInputStream("t.txt"));
int n = cin.nextInt();
while (n != 0)
{
String st = cin.next();
BigInteger a = new BigInteger("1");
BigInteger ans = new BigInteger("0");
int[] f = new int[30];
for (int i = 0; i < n; i++)
f[i] = st.charAt(i) - 'A';
for (int i = n - 1; i >= 0; i--)
{
int x = 0;
for (int j = 0; j < i; j++)
if (f[j] < f[i])
x++;
f[i] -= x;
}
for (int i = n - 1; i >= 0; i--)
{
int x = f[i];
ans = ans.add(BigInteger.valueOf(x).multiply(a));
a = a.multiply(BigInteger.valueOf(26 - i));
}
System.out.println(ans);
n = cin.nextInt();
}
}
}

你可能感兴趣的:(poj)