permutation 2(HDU-6630)
Problem Description
You are given three positive integers N,x,y.
Please calculate how many permutations of 1~N satisfies the following conditions (We denote the i-th number of a permutation by pi):
1. p1=x
2. pN=y
3. for all 1≤i<N, |pi?pi+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.
* 1≤T≤5000
* 2≤N≤105
* 1≤x<y≤N
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
題意:給出 t 組數據,每組給出一個 n,代表有 1~n 的數,再給出 x、y,代表 p[1]=x,p[n]=y,現已知 |p[i]-p[i+1]|<=2,問有多少中排列方式
思路:
打個表,可以發現規律為,對于 y-x,有:dp[0]=0,dp[1]=1,dp[2]=1,dp[3]=1,dp[i]=dp[i-1]+dp[i-3]
然后特判一下 x=1&&y=n、x=1、y=n 的三種情況即可?
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #include<unordered_map> #include<bitset> #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long #define Pair pair<int,int> LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; } LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;} LL quickPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;} LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); } LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); } LL LCM(LL x,LL y){ return x/GCD(x,y)*y; } const double EPS = 1E-10; const int MOD = 998244353; const int N = 100000+5; const int dx[] = {-1,1,0,0,1,-1,1,1}; const int dy[] = {0,0,-1,1,-1,1,-1,1}; using namespace std;LL dp[N]; int init() {dp[0] = 0;dp[1] = 1;dp[2] = 1;dp[3] = 1;for (int i = 4; i <= 100000; i++)dp[i] = ((dp[i - 1] + dp[i - 3]) % MOD); } int main() {init();int t;scanf("%d", &t);while (t--) {LL n, x, y;scanf("%lld%lld%lld", &n, &x, &y);if (x == 1 && y == n)printf("%d\n", dp[y - x + 1]);else if (x == 1)printf("%d\n", dp[y - x]);else if (y == n)printf("%d\n", dp[y - x]);elseprintf("%d\n", dp[y - x - 1]);}return 0; }?
總結
以上是生活随笔為你收集整理的permutation 2(HDU-6630)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Game(HDU-6669)
- 下一篇: 图论 —— 弦图 —— LexBFS 算