2019HDU多校训练第五场1007-permutation 2

本文详细解析了Permutation2算法,介绍了如何计算1至N的所有排列中满足特定条件的数量,并提供了一个简洁的C++实现代码。算法通过动态规划计算答案模998244353的结果。

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

permutation 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

 
Problem Description
You are given three positive integers N,x,y.
Please calculate how many permutations of 1N satisfies the following conditions (We denote the i-th number of a permutation by pi):

1. p1=x

2. pN=y

3. for all 1i<N|pipi+1|2
 
Input
The first line contains one integer T denoting the number of tests.

For each test, there is one line containing three integers N,x,y.

1T5000

2N105

1x<yN
 

 

Output
For each test, output one integer in a single line indicating the answer modulo 998244353.
 
Sample Input
3
4 1 4
4 2 4
100000 514 51144
 
Sample Output
2
1
253604680
 
 emmm代码很短,应该很容易看懂。。。。
 1 #include<bits/stdc++.h>
 2 #define ll long long
 3 using namespace std;
 4 #define maxn 100005
 5 #define mod 998244353
 6 ll fac[maxn];
 7 int main()
 8 {
 9     fac[0]=0;
10     fac[1]=1;
11     fac[2]=1;
12     for(int i=3; i<=maxn-2; i++)
13     {
14         fac[i]=(fac[i-1]+fac[i-3])%mod;
15     }
16     int t;
17     scanf("%d",&t);
18     while(t--)
19     {
20         int n,a,b;
21         scanf("%d%d%d",&n,&a,&b);
22         int ans=b-a;
23         if(a==1&&b==n)ans++;
24         else if(a!=1&&b!=n)ans--;
25         printf("%lld\n",fac[ans]);
26     }
27     return 0;
28 }
29 /*
30 3
31 4 1 4
32 4 2 4
33 100000 514 51144
34 */

 

转载于:https://www.cnblogs.com/CharlieWade/p/11305247.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值