結果

問題 No.1039 Project Euler でやれ
ユーザー tko919tko919
提出日時 2020-04-24 23:50:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 3,267 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 181,696 KB
実行使用メモリ 15,012 KB
最終ジャッジ日時 2023-08-07 21:35:25
合計ジャッジ時間 3,130 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
14,900 KB
testcase_01 AC 21 ms
14,820 KB
testcase_02 AC 21 ms
15,012 KB
testcase_03 AC 22 ms
14,892 KB
testcase_04 AC 22 ms
14,732 KB
testcase_05 AC 21 ms
14,788 KB
testcase_06 AC 21 ms
14,948 KB
testcase_07 AC 22 ms
14,820 KB
testcase_08 AC 22 ms
14,852 KB
testcase_09 AC 22 ms
14,832 KB
testcase_10 AC 21 ms
15,004 KB
testcase_11 AC 22 ms
14,896 KB
testcase_12 AC 21 ms
14,712 KB
testcase_13 AC 21 ms
14,948 KB
testcase_14 AC 21 ms
14,944 KB
testcase_15 AC 21 ms
14,736 KB
testcase_16 AC 21 ms
14,892 KB
testcase_17 AC 22 ms
14,960 KB
testcase_18 AC 21 ms
14,956 KB
testcase_19 AC 21 ms
14,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

template<unsigned mod=1000000007>struct mint {
   unsigned val;
   static unsigned get_mod(){return mod;}
   unsigned inv() const{
      int tmp,a=val,b=mod,x=1,y=0;
      while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
      if(x<0)x+=mod; return x;
   }
   mint():val(0){}
   mint(ll x):val(x>=0?x%mod:mod+(x%mod)){}
   mint pow(ll t){mint res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;}return res;}
   mint& operator+=(const mint& x){if((val+=x.val)>=mod)val-=mod;return *this;}
   mint& operator-=(const mint& x){if((val+=mod-x.val)>=mod)val-=mod; return *this;}
   mint& operator*=(const mint& x){val=ll(val)*x.val%mod; return *this;}
   mint& operator/=(const mint& x){val=ll(val)*x.inv()%mod; return *this;}
   mint operator+(const mint& x)const{return mint(*this)+=x;}
   mint operator-(const mint& x)const{return mint(*this)-=x;}
   mint operator*(const mint& x)const{return mint(*this)*=x;}
   mint operator/(const mint& x)const{return mint(*this)/=x;}
   bool operator==(const mint& x)const{return val==x.val;}
   bool operator!=(const mint& x)const{return val!=x.val;}
}; using Mint=mint<>;
template<typename T>struct factorial {
   vector<T> Fact,Finv,Inv;
   factorial(int maxx){
      Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
      Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1; unsigned mod=Mint::get_mod();
      rep(i,2,maxx){
         Fact[i]=Fact[i-1]*i;
         Inv[i]=Inv[mod%i]*(mod-mod/i);
         Finv[i]=Finv[i-1]*Inv[i];
      }
   }
   T fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];}
   T inv(int n){return Inv[n];}
   T nPr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[n-r];}
   T nCr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[r]*Finv[n-r];}
};

factorial<Mint> fact(1010000);
vector<vector<int>> part[21];

int main(){
   part[0].push_back({0});
   rep(i,0,20)for(auto v:part[i]){
      int s=max(1,v.back());
      rep(add,s,20-i){
         auto w=v; w.push_back(add);
         part[i+add].push_back(w);
      }
   }

   int n; cin>>n;
   Mint res=fact.fact(n);
   vector<pair<int,int>> ps;
   for(int p=2;p*p<=n;p++)if(n%p==0){
      int cnt=0;
      while(n%p==0)cnt++,n/=p;
      ps.push_back({p,cnt});
   }
   if(n!=1)ps.push_back({n,1});
   for(auto P:ps){
      int p=P.first,cnt=P.second;
      Mint mul;
      for(auto& v:part[cnt]){
         Mint add=1; int m=v.size();
         vector<int> c(m),d(m);
         rep(i,1,m)c[i]=d[i]=i;
         rep(i,1,m)rep(j,1,m)if(v[i]==v[j]){
            chmin(c[i],j); chmax(d[i],j);
         }
         rep(i,1,m)add*=(Mint(p).pow(d[i])-Mint(p).pow(i-1));
         rep(i,1,m)add*=Mint(p).pow(v[i]*(m-d[i]-1));
         rep(i,1,m)add*=Mint(p).pow((v[i]-1)*(m-c[i]));
         mul+=add.inv();
      } res*=mul;
   } printf("%d\n",res.val);
   return 0;
}
0