結果
| 問題 |
No.117 組み合わせの数
|
| コンテスト | |
| ユーザー |
warachia_
|
| 提出日時 | 2019-05-22 18:23:29 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 691 ms / 5,000 ms |
| コード長 | 3,677 bytes |
| コンパイル時間 | 1,882 ms |
| コンパイル使用メモリ | 77,740 KB |
| 実行使用メモリ | 128,336 KB |
| 最終ジャッジ日時 | 2024-09-17 09:37:15 |
| 合計ジャッジ時間 | 3,717 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 1 |
ソースコード
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int t = sc.nextInt();
Combinatrics c = new Combinatrics(2000000);
for(int i=0;i<t;i++){
String s = sc.next();
int n = Integer.parseInt(s.substring(2,s.indexOf(',')));
int k = Integer.parseInt(s.substring(s.indexOf(',')+1,s.indexOf(')')));
switch(s.charAt(0)) {
case 'C': System.out.println(c.combination(n,k)); break;
case 'P': System.out.println(c.permutation(n,k)); break;
case 'H': System.out.println(c.homogeneous(n,k)); break;
}
}
}
}
class Combinatrics {
static long mod = 1000000007;
static long[] fact; //階乗のテーブル
static long[] inv; //逆元のテーブル
static long[] finv; //階乗の逆元のテーブル
public Combinatrics(int n){
fact = new long[n+1];
inv = new long[n+1];
finv = new long[n+1];
fact[0] = fact[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for(int i=2;i<n+1; i++){
fact[i] = fact[i-1] * i % mod;
inv[i] = mod - inv[(int)mod%i] * (mod / i) % mod;
finv[i] = finv[i-1] * inv[i] % mod;
}
}
//nCr(n個からr個選ぶ)
int combination(int n, int r) {
if(n<r|| r<0){
return 0;
}
return (int)(fact[n]*finv[r]%mod*finv[n-r]%mod);
}
//nPr (=n*(n-1)*...*(n-r+1))(n個からr個選んで並べる)
int permutation(int n, int r) {
if(n<r || r<0){
return 0;
}
return (int)(fact[n]*finv[n-r]%mod);
}
//nHr (=n+r-1Cr)(n個から重複を許してk個とる)
//(テーブルがn+r-1まで必要な事に注意!)
int homogeneous(int n, int r) {
if(n==0 && r==0){
return 1;
}
return combination(n+r-1,r);
}
}
class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {
ptr = 0;
try {
buflen = in.read(buffer);
} catch (IOException e) {
e.printStackTrace();
}
if (buflen <= 0) {
return false;
}
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[ptr++];
else
return -1;
}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr]))
ptr++;
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
n *= 10;
n += b - '0';
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
warachia_