Luogu5367 【模板】康托展开 (康拓展开)
2019-07-23 12:09
169 查看
原文链接:http://www.cnblogs.com/bingoyes/p/11231013.html
\(n^2\)暴力
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int mod = 998244353;
int fac[1000007];
int n;
inline int Cantor(int *a, int n){
int ans = 1, sum = 0;
R(i,1,n){
R(j,i+1,n){
sum += (a[i] > a[j]);
if(sum > mod) sum -= mod;
}
ans = (ans + 1ll * sum * fac[n - i] % mod) % mod;
sum = 0;
}
return ans;
}
int a[1000007];
int main(){
int n;
io >> n;
fac[0] = 1;
R(i,1,n){
io >> a[i];
fac[i] = 1ll * fac[i - 1] * i % mod;
}
printf("%d", Cantor(a, n));
return 0;
}
[/code]
BIT优化
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG
#ifdef ON_DEBUG
#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);
#else
#define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ;
#endif
struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std;
const int N = 1000007;
const int mod = 998244353;
int n;
int t
;
inline void Updata(int x, int w){
for(; x <= n; x += x&-x) t[x] += w;
}
inline int Query(int x){
int sum = 0;
for(; x; x -= x&-x) sum += t[x];
return sum;
}
int a
;
int main(){
//FileOpen();
io >> n;
int fac = 1;
nR(i,n,1){
io >> a[i];
}
int ans = 0;
R(i,1,n){
int sum = Query(a[i]);
ans = (ans + 1ll * fac * sum % mod) % mod;
fac = 1ll * fac * i % mod;
Updata(a[i], 1);
}
printf("%d\n", ans + 1);
return 0;
}
[/code]
转载于:https://www.cnblogs.com/bingoyes/p/11231013.html
相关文章推荐
- [模板]康拓展开和他的逆运算
- 康托展开模板(树状数组维护)O (n log n)
- [Luogu] 模板题-最近公共祖先
- luogu P3368 【模板】树状数组 2
- QDU第二次月赛 排在第几个(康拓展开)
- 贴个JAVA写的康拓展开算法;效率一般,思想有了就行;....
- NYOJ 题目143 第几是谁?(康拓展开)
- 【算法系列学习三】[kuangbin带你飞]专题二 搜索进阶 之 A-Eight 反向bfs打表和康拓展开
- 康托展开和逆展开
- 康拓展开及应用
- Luogu-P3372 (Lazy_tag 线段树模板)
- Codeforces Round #285 (Div. 1) problem B. Misha and Permutations Summation 康拓展开。
- vijos - P1092全排列 (康托展开 + 康托展开的逆运算)
- 【BFS拓展】康拓展开
- 康托展开和逆康托展开
- HDU3567:Eight II(康拓展开+预处理) (B)
- luogu3375 kmp算法模板
- 蓝桥杯 2017模拟赛-本科组 排列序数(康拓展开)
- 康拓展开,康拓展开逆运算
- hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)