結果
問題 | No.271 next_permutation (2) |
ユーザー |
![]() |
提出日時 | 2015-06-28 20:11:54 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 23 ms / 2,000 ms |
コード長 | 6,489 bytes |
コンパイル時間 | 934 ms |
コンパイル使用メモリ | 94,192 KB |
実行使用メモリ | 6,820 KB |
最終ジャッジ日時 | 2024-11-26 07:45:50 |
合計ジャッジ時間 | 2,235 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
#undef NDEBUG#include <string>#include <vector>#include <algorithm>#include <numeric>#include <set>#include <map>#include <queue>#include <iostream>#include <sstream>#include <cstdio>#include <cmath>#include <ctime>#include <cstring>#include <cctype>#include <cassert>#include <limits>#include <functional>#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))#if defined(_MSC_VER) || __cplusplus > 199711L#define aut(r,v) auto r = (v)#else#define aut(r,v) __typeof(v) r = (v)#endif#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)#define all(o) (o).begin(), (o).end()#define pb(x) push_back(x)#define mp(x,y) make_pair((x),(y))#define mset(m,v) memset(m,v,sizeof(m))#define INF 0x3f3f3f3f#define INFL 0x3f3f3f3f3f3f3f3fLLusing namespace std;typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }#ifdef NDEBUG#error assert is disabled!#endiftemplate<typename T>T readNatural(T lo, T up) {assert(0 <= lo && lo <= up);T x = 0;while(1) {int d = getchar();if(!('0' <= d && d <= '9')) {ungetc(d, stdin);break;}d -= '0';assert(d <= up && x <= (up - d) / 10);x = x * 10 + d;}assert(lo <= x && x <= up);return x;}void readSpace() { int c = getchar(); assert(c == ' '); }static bool read_eof = false;void readEOL() {int c = getchar();if(c == EOF) {assert(!read_eof);read_eof = true;}else {assert(c == '\n');}}template<int MOD>struct ModInt {static const int Mod = MOD;unsigned x;ModInt(): x(0) { }ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }int get() const { return (int)x; }ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }ModInt operator+(ModInt that) const { return ModInt(*this) += that; }ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }ModInt inverse() const {signed a = x, b = MOD, u = 1, v = 0;while(b) {signed t = a / b;a -= t * b; std::swap(a, b);u -= t * v; std::swap(u, v);}if(u < 0) u += Mod;ModInt res; res.x = (unsigned)u;return res;}};typedef ModInt<1000000007> mint;struct FenwickTree {typedef int T;vector<T> v;void init(int n) { v.assign(n, 0); }void add(int i, T x) {for(; i < (int)v.size(); i |= i+1) v[i] += x;}T sum(int i) const { //[0, i)T r = 0;for(-- i; i >= 0; i = (i & (i+1)) - 1) r += v[i];return r;}T sum(int left, int right) const { //[left, right)return sum(right) - sum(left);}};//0 <= res[i] < n - ivoid permutationToFactorialNumberSystem(const vector<int> &p, vector<int> &res) {int n = p.size();FenwickTree ft;ft.init(n);res.resize(n);for(int i = n - 1; i >= 0; -- i) {res[i] = ft.sum(p[i]);ft.add(p[i], 1);}}long long integerToFactorialNumberSystem(int n, long long x, vector<int> &res) {assert(x >= 0);res.resize(n);for(int i = n - 1; i >= 0; -- i) {res[i] = x % (n - i);x /= n - i;}return x;}int addFactorialNumberSystem(const vector<int> &x, const vector<int> &y, vector<int> &res) {int n = x.size();assert(y.size() == n);res.resize(n);int carry = 0;for(int i = n - 1; i >= 0; -- i) {int z = x[i] + y[i] + carry;res[i] = z % (n - i);carry = z / (n - i);}return carry;}//sum [l,u)ll arithsum(int l, int u) {if(l >= u) return 0;int w = u - l;return (ll)l * w + (ll)w * (w-1) / 2;}struct DP {vector<pair<mint,mint> > dp;DP(int n): dp((n+1) * 2) { }pair<mint,mint> &operator()(int i, bool lt) { return dp[i * 2 + lt]; }};// p < x となる置換のinvの和//= p < x となるfactorial number systemの桁の和の和//(i, lt)に対して(cnt, sum)を持つ桁DPをするmint solve(const vector<int> &x) {int n = x.size();DP dp(n);dp(n, true) = mp(mint(1), mint(0));for(int i = n-1; i >= 0; -- i) rep(lt, 2) {mint cnt, sum;int d = x[i];/* //以下と同じことをまとめて数えてやるrep(e, n - i) if(lt || e <= d) {auto p = dp(i+1, lt || e < d);cnt += p.first, sum += p.second;sum += p.first * e;}*///e < d の場合{auto p = dp(i+1, true);cnt += p.first * d, sum += p.second * d;sum += p.first * arithsum(0, d);}//e = d の場合{auto p = dp(i+1, lt != 0);cnt += p.first, sum += p.second;sum += p.first * d;}//e > d の場合if(lt) {auto p = dp(i+1, true);int t = n - i - d - 1;cnt += p.first * t, sum += p.second * t;sum += p.first * arithsum(d+1, n - i);}dp(i, lt != 0) = mp(cnt, sum);}return dp(0, false).second;}int main() {int N; long long K;N = readNatural(1, (int)1e5);readSpace();K = readNatural(0LL, (ll)1e18);readEOL();vector<int> p(N);rep(i, N) {p[i] = readNatural(1, N);if(i < N-1) readSpace(); else readEOL();}assert(!read_eof);{ vector<bool> vis(N);rep(i, N) {assert(!vis[p[i]-1]);vis[p[i]-1] = true;}}rep(i, N) -- p[i];//p, f^K(p) を factorial number system に変換する//キャリーは覚えておいて、それは別に足すvector<int> x, y, z;permutationToFactorialNumberSystem(p, x);long long cycles = 0;cycles += integerToFactorialNumberSystem(N, K, y);cycles += addFactorialNumberSystem(x, y, z);//factorial number systemでの要素ごとのinvへの付与を考えると、// \sum_{p is a permutation of [1..n]} inv(p)//= \sum_{i=2}^n \sum{j=0}^{i-1} (n! / i) j//= n! \sum_{i=1}^{n-1} i / 2//= n! n (n-1) / 4mint allinvsum = 1;rep(i, N) allinvsum *= i+1;allinvsum *= mint(N) * (N-1) / 4;mint ans;ans += allinvsum * cycles;ans += solve(z);ans -= solve(x);printf("%d\n", ans.get());return 0;}