티스토리 뷰
13977번: 이항 계수와 쿼리
\(M\)개의 자연수 \(N\)과 정수 \(K\)가 주어졌을 때 이항 계수 \(\binom{N}{K}\)를 1,000,000,007로 나눈 나머지를 구하는 프로그램을 작성하시오.
www.acmicpc.net
백준 소스코드 [C++] 13977 이항 계수와 쿼리
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
typedef long long ll;
using namespace std;
#define P 1000000007LL
ll fac[4000001],inverse[4000001];
ll power(ll x, ll y) {
if (y == 0) return 1;
if (y % 2 ==1) return (power(x, y- 1) * x) % P;
ll half = power(x, y / 2) % P;
return half*half % P;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int n, k, t;
fac[0] = 1;
for (int i = 1; i <= 4000000; i++)
fac[i] = fac[i - 1] * i % P;
inverse[4000000] = power(fac[4000000], P - 2);
for (int i = 4000000 - 1; 0<=i; i--)
inverse[i] = inverse[i + 1] * (i + 1LL) % P;
cin >> t;
while (t--) {
cin >> n >> k;
ll A = fac[n];
ll B = (inverse[n - k] * inverse[k]) % P;
cout << A * B % P<<'\n';
}
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 16134 조합 (Combination) (0) | 2020.11.08 |
---|---|
백준 소스코드 [C++] 15791 세진이의 미팅 (0) | 2020.11.08 |
백준 소스코드 [C++] 11401 이항 계수 3 (0) | 2020.11.08 |
백준 소스코드 [C++] 1074 Z (0) | 2020.11.01 |
백준 소스코드 [C++] 14495 피보나치 비스무리한 수열 (0) | 2020.10.30 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- MeTal
- WWDC19
- WWDC17
- observeOn
- 벨만포드 시간복잡도
- 포드 풀커슨 알고리즘
- mach-o
- 코딩대회
- 벨만포드 알고리즘
- 최대 매칭
- WWDC21
- test coverage
- 네트워크 유량
- State Restoration
- IOS
- WWDC16
- Testable
- 에드몬드 카프 알고리즘
- CompositionalLayout
- 네트워크 플로우
- CPU와 Memory
- 컴퓨터 추상화
- HIG
- rxswift
- 최단경로문제
- 최단경로 알고리즘
- 강한 순환 참조
- 부스트캠프 6기
- 다익스트라 시간복잡도
- 최단경로 문제
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
글 보관함