結果
| 問題 |
No.271 next_permutation (2)
|
| コンテスト | |
| ユーザー |
kmjp
|
| 提出日時 | 2015-08-22 11:13:22 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 2,100 bytes |
| コンパイル時間 | 1,433 ms |
| コンパイル使用メモリ | 167,452 KB |
| 実行使用メモリ | 9,748 KB |
| 最終ジャッジ日時 | 2024-07-18 12:38:22 |
| 合計ジャッジ時間 | 6,100 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | RE * 21 |
コンパイルメッセージ
main.cpp: In instantiation of ‘V BIT<V, ME>::add(int, V) [with V = int; int ME = 20]’:
main.cpp:34:40: required from here
main.cpp:19:64: warning: no return statement in function returning non-void [-Wreturn-type]
19 | V add(int e,V v) { while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
| ^
ソースコード
#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];
V operator()(int e) {V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
V add(int e,V v) { while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
};
ll mo=1000000007;
int N;
ll K;
vector<int> P,P2;
vector<int> toFNS(vector<int> v) { // factorial number system
static BIT<int,20> bt;
ZERO(bt.bit);
vector<int> ret;
reverse(v.begin(),v.end());
FORR(r,v) ret.push_back(bt(r)), bt.add(r,1);
reverse(ret.begin(),ret.end());
return ret;
}
ll fact[101010], totinv[101010];
void FNSinvsuminit(){
int i;
fact[0]=1;
totinv[0]=0;
for(i=1;i<=100000+5;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;
}
}
ll FNSinv(vector<int> v){ return accumulate(v.begin(),v.end(),0LL);}
ll FNSinvsum(vector<int> v) {
ll sum=0,num=0,f=1;
int i,j;
for(i=v.size()-1;i>=0;i--) {
sum += totinv[v.size()-1-i]*v[i]%mo;
FOR(j,v[i]) sum+=f*j%mo;
sum += v[i]*num%mo;
f=f*(v.size()-i)%mo;
num=(num+v[i]*f)%mo;
}
return sum%mo;
}
void solve() {
int i,j,k,l,r,x,y; string s;
FNSinvsuminit();
cin>>N>>K;
FOR(i,N) cin>>x, P.push_back(x);
P2=P=toFNS(P);
// add K
FOR(i,N) {
ll t=K+P2[N-1-i];
P2[N-1-i]=t%(i+1);
K=t/(i+1);
}
ll ret = K%mo*totinv[N]%mo;
ret += FNSinvsum(P2)-FNSinv(P2);
ret -= FNSinvsum(P)-FNSinv(P);
cout<<((ret%mo)+mo)%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;
}
kmjp