結果

問題 No.271 next_permutation (2)
ユーザー 👑 kmjpkmjp
提出日時 2015-08-21 23:44:14
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,657 bytes
コンパイル時間 1,513 ms
コンパイル使用メモリ 162,324 KB
実行使用メモリ 8,324 KB
最終ジャッジ日時 2023-09-25 15:35:58
合計ジャッジ時間 5,586 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 1 ms
4,380 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of ‘V BIT<V, ME>::add(int, V) [with V = int; int ME = 18]’:
main.cpp:43:19:   required from here
main.cpp:19:70: warning: no return statement in function returning non-void [-Wreturn-type]
  V add(int e,V v) { val[e++]+=v; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
                                                                      ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

template<class V, int ME> class BIT {
public:
	V bit[1<<ME],val[1<<ME];
	V total(int e) {V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	V add(int e,V v) { val[e++]+=v; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
	V set(int e,V v) { add(e,v-val[e]);}
	int lower_bound(V val) {
		V tv=0; int i,ent=0;
		for(i=ME-1;i>=0;i--) if(tv+bit[ent+(1<<i)-1]<val) tv+=bit[ent+(1<<i)-1],ent+=(1<<i);
		return ent;
	}
};


ll array_inv(vector<ll> V) {
	static BIT<int,18> bt;
	ZERO(bt.bit);
	ZERO(bt.val);
	int x=0,i;
	map<ll,int> M;
	vector<int> V2;
	
	M[-1LL<<60]=0;
	FORR(r,V) M[r]=0;
	FORR(r,M) r.second=x++;
	ll ret=0;
	for(i=V.size()-1;i>=0;i--) {
		ret += bt.total(M[V[i]]);
		bt.add(M[V[i]],1);
	}
	return ret;
}

int N;
ll K;
vector<ll> P;
ll fact[101010], totinv[101010];
ll ret;
ll mo=1000000007;

ll getsmaller(int n) {
	static BIT<int,18> bt;
	ZERO(bt.bit);
	ZERO(bt.val);
	int i;
	ll tot=0;
	for(i=n+1;i<N;i++) bt.add(P[i],1);
	FOR(i,n+1) ret+=bt.total(P[i]);
	return ret%mo;
}

ll factinv(int n,ll k) {
	vector<ll> v;
	ll r=0;
	int i;
	FOR(i,n) v.push_back(i);
	while(k--) {
		r+=array_inv(v)%mo;
		next_permutation(v.begin(),v.end());
	}
	
	return r%mo;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	
	
	cin>>N>>K;
	if(K==0) return _P("0\n");
	
	P.push_back(0);
	FOR(i,N) cin>>x, P.push_back(x);
	N++;
	
	fact[0]=1;
	totinv[0]=0;
	for(i=1;i<=N;i++) {
		fact[i]=fact[i-1]*i;
		if(fact[i]<fact[i-1]) fact[i]=1LL<<62;
		totinv[i]=fact[i]%mo*i%mo;
		(totinv[i]*=i-1)%=mo;
		(totinv[i]*=(mo+1)/2)%=mo;
		(totinv[i]*=(mo+1)/2)%=mo;
	}
	
	ret=(K/fact[N-1])%mo*totinv[N-1]%mo;
	K%=fact[N-1];
	
	for(i=N-2;i>=0;i--) {
		if(fact[N-1-i]>=K) {
			out:
			ret += factinv(N-1-i,K);
			break;
		}
		if(P[i]>P[i+1]) {
			ll smaller=getsmaller(i);
			for(x=i;x<N;x++) {
				if(P[i]<=P[x]) {
					ll smaller2=fact[N-1-i]%mo*smaller%mo;
					(ret += totinv[N-1-i]+smaller2)%=mo;
					K -= fact[N-1-i];
					if(K<=fact[N-1-i]) goto out;
					smaller++;
				}
			}
			sort(P.begin()+i,P.end());
		}
	}
	cout<<ret%mo<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0