lightOJ 1067 - Combinations

纯粹的lucas定理,因为n,m的数据比较小,所以可以直接用公式:
c(n,m) = ( (a! * ((a-b)!)^(p-2) % mod) * ((b!)^(p-2)) ) % mod;
详见代码:

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <cctype>
#include <cmath>
#include <cstring>
#include <climits>
#include <complex>
#include <set>
#include <deque>
#define DEBUG(x) cerr<<"line:"<<__LINE__<<", "<<#x" == "<<(x)<<endl;
#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define FOR(it,s) for(__typeof(s.begin()) it=s.begin();it!=s.end();it++)
#define ALL(a) a.begin(),a.end()
#define RI(x) scanf("%d",&(x))
#define RII(x,y) scanf("%d%d",&(x),&(y))
#define RIII(x,y,z) scanf("%d%d%d",&(x),&(y),&(z))
#define DRI(x) int (x);scanf("%d",&(x))
#define DRII(x,y) int (x),(y);scanf("%d%d",&(x),&(y))
#define DRIII(x,y,z) int (x),(y),(z);scanf("%d%d%d",&(x),&(y),&(z))
#define MS0(a) memset((a),0,sizeof((a)))
#define MS1(a) memset((a),-1,sizeof((a)))
#define MS(a,b) memset((a),(b),sizeof((a)))
using namespace std;

typedef long long LL;
typedef unsigned int uint;
typedef unsigned long long ULL;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<vi> vvi;

#define INF 1000000000
const double eps = 1e-10;
int dcmp(double x){
  if(fabs(x) < eps) return 0;
  else return x < 0 ? -1 : 1;
}
// ------------------
// author : onehrxn
// ------------------
const LL mod = 1000003;
const int maxn = 1000000+10;
LL fac[maxn];
void init(void){
  fac[1] = fac[0] = 1;
  for(int i = 2; i <= maxn - 2; i++) fac[i] = fac[i-1]*i % mod;
}
LL qpow(LL a, LL b){
  LL ans;
  for(ans = 1; b ; b >>= 1, a = (a * a) % mod)
    if(b & 1) ans = (ans * a) % mod;
  return ans;
}
inline LL lucas(LL a, LL b, LL p){
  return (((fac[a] * qpow(fac[b],p-2))%mod) * qpow(fac[a-b],p-2) )% mod;
}

int main(void)
{
    // ios::sync_with_stdio(false);
    // cin.tie(0);
#ifdef LOCAL
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
#endif
  init();
  DRI(t);
  for(int kase = 1; kase <= t; kase++){
    DRII(n,k);
    printf("Case %d: %lld\n", kase, lucas(n,k,mod));
  }
#ifdef LOCAL
        cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值