結果
問題 | No.696 square1001 and Permutation 5 |
ユーザー | どらら |
提出日時 | 2018-06-09 00:07:43 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
MLE
|
実行時間 | - |
コード長 | 5,962 bytes |
コンパイル時間 | 2,747 ms |
コンパイル使用メモリ | 200,588 KB |
実行使用メモリ | 814,780 KB |
最終ジャッジ日時 | 2024-06-30 11:29:05 |
合計ジャッジ時間 | 6,948 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | MLE | - |
testcase_01 | -- | - |
testcase_02 | -- | - |
testcase_03 | -- | - |
testcase_04 | -- | - |
testcase_05 | -- | - |
testcase_06 | -- | - |
testcase_07 | -- | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
ソースコード
#include <bits/stdc++.h> using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it) #define ALLOF(c) (c).begin(), (c).end() typedef long long ll; typedef unsigned long long ull; class BIT{ static const int MAX_N = 1 << 21; int n; vector<int> bit; public: BIT(int n_):bit(MAX_N+1){ n = n_; for(int i=0; i<n+1; i++) bit[i] = 0; } int sum(int i){ int s = 0; while(i>0){ s+=bit[i]; i-=i&(-i); } return s; } void add(int i, int x){ while(i<=n){ bit[i]+=x; i+=i&(-i); } } }; const long double PI = 4.0 * atan(1); typedef std::vector< std::complex<long double> > VComp; void fft(VComp& a, long double theta){ int n = a.size(); for(int m=n; m>=2; m>>=1){ for(int i=0; i<(m>>1); i++){ std::complex<long double> w = exp(i*theta*std::complex<long double>(0,1)); for(int j=i; j<n; j+=m){ int k=j+(m>>1); std::complex<long double> x = a[j]-a[k]; a[j] += a[k]; a[k] = w*x; } } theta *= 2; } int i=0; for(int j=1; j<n-1; j++){ for(int k=n>>1; k>(i^=k); k>>=1); if(j<i) swap(a[i], a[j]); } if(theta<0){ for(int i=0; i<a.size(); i++){ a[i] /= n; } } } VComp str2vec(const std::string& str, int n){ VComp ret(n, std::complex<long double>(0,0)); for(int i=str.length()-1,j=0; i>=0; i--,j++){ ret[j] = str[i] - '0'; } return ret; } VComp VComp_mult(const VComp& a, const VComp& b){ VComp ret(a.size(), std::complex<long double>(0,0)); for(int i=0; i<a.size(); i++){ ret[i] = a[i] * b[i]; } return ret; } void print(const VComp& v){ for(int i=0; i<v.size(); i++){ std::cout << i << " : " << real(v[i]) << " " << imag(v[i]) << std::endl; } } std::string fft_multi(const std::string& str1, const std::string& str2){ int n = 1; while(str1.size()+str2.size() >= n) n <<= 1; VComp ret1 = str2vec(str1, n); VComp ret2 = str2vec(str2, n); fft(ret1, 2 * PI / n); fft(ret2, 2 * PI / n); VComp ret = VComp_mult(ret1, ret2); fft(ret, -2 * PI / n); //print(ret); std::string ans = ""; int carry = 0; int nonzero_idx = 0; for(int i=0; i<ret.size(); i++){ if(real(ret[i])<0) ret[i] *= 0; if(i == 0){ ans += (char)('0' + (int)(real(ret[i])+0.5)%10); }else{ int tmp = carry + (int)(real(ret[i-1])+0.5)/10 + (int)(real(ret[i])+0.5)%10; carry = carry/10 + tmp/10; tmp %= 10; ans += (char)('0' + tmp); if(tmp != 0) nonzero_idx = i; } } if(carry > 0){ ans += (char)('0' + carry); nonzero_idx = ret.size(); } std::string noleadingzero = ans.substr(0,nonzero_idx+1); std::reverse(noleadingzero.begin(), noleadingzero.end()); return noleadingzero; } class Bignum{ public: Bignum(){} Bignum(string s){ for(int i=0;i<s.length();i++) d.push_front(s[i]-'0'); normalize(); } Bignum &normalize(){ for(int i=d.size()-1;i>=0;i--){ if(d[i]!=0) break; d.pop_back(); } return *this; } int operator[](int i){ return i>=d.size()?0:d[i]; } int size(){ return normalize(),d.size(); } void toZero(){ d.clear(); d.push_front(0); } void toOne(){ d.clear(); d.push_front(1); } Bignum &inc(){ Bignum one("1"); return operator+=(one); } Bignum &operator+=(Bignum n){ int l=max(size(),n.size()); d.resize(l+1,0); for(int c=0,i=0;i<=l;i++) d[i]=(c=d[i]+n[i]+c/10)%10; return normalize(); } Bignum &operator-=(Bignum n){ //assert(size()>=n.size()); for(int c=0,i=0;i<d.size();i++) d[i]=((c=d[i]-n[i]+(c-9)/10)+10)%10; return normalize(); } Bignum &operator*=(int n){ for(int i=0;i<15;i++) d.push_back(0); for(int c=0,i=0;i<d.size();i++) d[i]=(c=d[i]*n+c/10)%10; return normalize(); } Bignum &operator*=(ull n){ for(int i=0;i<25;i++) d.push_back(0); for(ull c=0,i=0;i<d.size();i++) d[i]=(c=d[i]*n+c/10ULL)%10ULL; return normalize(); } Bignum operator+(Bignum n){ return Bignum(*this)+=n; } Bignum operator-(Bignum n){ return Bignum(*this)-=n; } Bignum operator*(int n){ return Bignum(*this)*=n; } Bignum operator*(ull n){ return Bignum(*this)*=n; } Bignum operator*(Bignum n){ Bignum ret; int ka=size(),kb=n.size(); ret.d.resize(ka+kb,0); for(int i=0;i<ka;i++) for(int c=0,j=0;j<=kb;j++) ret.d[i+j]=(c=ret.d[i+j]+d[i]*n[j]+c/10)%10; return ret.normalize(); } Bignum &operator*=(Bignum n){ return (*this)=(*this)*n; } int div(int n){ int c=0; for(int i=size()-1;i>=0;i--) d[i]=(c=c%n*10+d[i])/n; return c%n; } Bignum &operator<<=(int n){ for(int i=0;i<n;i++) d.push_front(0); return normalize(); } Bignum operator<<(int n){ return Bignum(*this)<<=n; } bool operator>(Bignum n){ if(size()!=n.size()) return size()>n.size(); for(int i=size()-1;i>=0;i--) if(d[i]!=n[i]) return d[i]>n[i]; return false; } deque<int> d; }; ostream &operator<<(ostream &os, const Bignum &l) { Bignum n=l; if (n.size()==0) os<<0; else for (int i=n.d.size()-1;i>=0;i--) os<<n.d[i]; return os; } int main(){ int N; cin >> N; vector<int> v; rep(i,N){ int p; cin >> p; v.push_back(p); } BIT bit(1<<20); vector<Bignum> v_num(N); vector<Bignum> v_p(N); Bignum p("1"); for(int i=N-1; i>=0; i--){ ull num = bit.sum(v[i]); v_num[i].toOne(); v_num[i] *= num; v_p[i] = p; p *= N-i; bit.add(v[i], 1); } Bignum ret("1"); rep(j,N){ string tmp1="", tmp2=""; { if(v_num[j].d.size() == 0) tmp1 = "0"; else for (int i=v_num[j].d.size()-1;i>=0;i--) tmp1 += '0' + v_num[j].d[i]; } { if(v_p[j].d.size() == 0) tmp2 = "0"; else for (int i=v_p[j].d.size()-1;i>=0;i--) tmp2 += '0' + v_p[j].d[i]; } string tmp_nump = fft_multi(tmp1, tmp2); Bignum b(tmp_nump); ret += b; } cout << ret << endl; return 0; }