快速幂+费马小定理 690D2 - The Wall (medium)CF

本文介绍了一个计算特定条件下可能建造的不同墙壁数量的算法。通过给定的砖块数量和墙宽,利用组合数学的方法来计算模10^6 + 3后的结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<bits/stdc++.h>
using namespace std ;

typedef long long LL ;
const LL MOD = 1000003 ;

LL POW(LL a ,LL n){
	LL re=1 ;
	while (n>0){
		if (n&1)re=re*a%MOD ;
		a=a*a%MOD ;
		n>>=1 ;
	}
	return re ;
}

LL C(int n ,int m ){
	m = min(m,n-m) ;
	
	LL  a=1 ,b=1 ;
	for (int i=1 ;i<=m ;i++ ){
		a=a*i%MOD ;
		b=b*(n+1-i)%MOD ;
	}
	a = POW(a,MOD-2) ;
	return a*b%MOD ;
}

int main(){
	int n ,m ;
	cin >> n >> m ;
	cout << (C(n+m,m)+MOD-1)%MOD ;
}

<div class="header"><div class="title">D2. The Wall (medium)</div><div class="time-limit"><div class="property-title">time limit per test</div>2 seconds</div><div class="memory-limit"><div class="property-title">memory limit per test</div>256 megabytes</div><div class="input-file"><div class="property-title">input</div>standard input</div><div class="output-file"><div class="property-title">output</div>standard output</div></div><div><p>Heidi the Cow is aghast: cracks in the northern Wall? Zombies gathering outside, forming groups, preparing their assault? This must not happen! Quickly, she fetches her HC<span class="tex-span"><sup class="upper-index"><span style="font-size:12px;">2</span></sup></span> (Handbook of Crazy Constructions) and looks for the right chapter:</p><p><span class="tex-font-style-it">How to build a wall:</span></p><ol><li><span class="tex-font-style-it">Take a set of bricks.</span> </li><li><span class="tex-font-style-it">Select one of the possible wall designs. Computing the number of possible designs is left as an exercise to the reader.</span> </li><li><span class="tex-font-style-it">Place bricks on top of each other, according to the chosen design.</span> </li></ol><p>This seems easy enough. But Heidi is a Coding Cow, not a Constructing Cow. Her mind keeps coming back to point 2b. Despite the imminent danger of a zombie onslaught, she wonders just how many possible walls she could build with up to <span class="tex-span"><em>n</em></span> bricks.</p><p>A <span class="tex-font-style-it">wall</span> is a set of wall segments as defined in the easy version. How many different walls can be constructed such that the wall consists of at least <span class="tex-span">1</span> and at most <span class="tex-span"><em>n</em></span> bricks? Two walls are different if there exist a column <span class="tex-span"><em>c</em></span> and a row <span class="tex-span"><em>r</em></span> such that one wall has a brick in this spot, and the other does not.</p><p>Along with <span class="tex-span"><em>n</em></span>, you will be given <span class="tex-span"><em>C</em></span>, the width of the wall (as defined in the easy version). Return the number of different walls modulo <span class="tex-span">10<sup class="upper-index"><span style="font-size:12px;">6</span></sup> + 3</span>.</p></div><div class="input-specification"><div class="section-title">Input</div><p>The first line contains two space-separated integers <span class="tex-span"><em>n</em></span> and <span class="tex-span"><em>C</em></span>, <span class="tex-span">1 ≤ <em>n</em> ≤ 500000</span>, <span class="tex-span">1 ≤ <em>C</em> ≤ 200000</span>.</p></div><div class="output-specification"><div class="section-title">Output</div><p>Print the number of different walls that Heidi could build, modulo <span class="tex-span">10<sup class="upper-index"><span style="font-size:12px;">6</span></sup> + 3</span>.</p></div><div class="sample-tests"><div class="section-title">Examples</div><div class="sample-test"><div class="input"><div class="title">Input</div><pre>5 1
Output
5
Input
2 2
Output
5
Input
3 2
Output
9
Input
11 5
Output
4367
Input
37 63
Output
230574
Note

The number 106 + 3 is prime.

In the second sample case, the five walls are:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值