結果

問題 No.890 移調の限られた旋法
ユーザー tko919tko919
提出日時 2019-09-22 01:27:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 4,768 bytes
コンパイル時間 2,211 ms
コンパイル使用メモリ 183,888 KB
実行使用メモリ 11,060 KB
最終ジャッジ日時 2023-10-19 06:53:05
合計ジャッジ時間 5,140 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
11,060 KB
testcase_01 AC 49 ms
11,060 KB
testcase_02 AC 49 ms
11,060 KB
testcase_03 AC 49 ms
11,060 KB
testcase_04 AC 49 ms
11,060 KB
testcase_05 AC 49 ms
11,060 KB
testcase_06 AC 49 ms
11,060 KB
testcase_07 AC 49 ms
11,060 KB
testcase_08 AC 49 ms
11,060 KB
testcase_09 AC 49 ms
11,060 KB
testcase_10 AC 50 ms
11,060 KB
testcase_11 AC 49 ms
11,060 KB
testcase_12 AC 50 ms
11,060 KB
testcase_13 AC 49 ms
11,060 KB
testcase_14 AC 49 ms
11,060 KB
testcase_15 AC 49 ms
11,060 KB
testcase_16 AC 49 ms
11,060 KB
testcase_17 AC 49 ms
11,060 KB
testcase_18 AC 49 ms
11,060 KB
testcase_19 AC 49 ms
11,060 KB
testcase_20 AC 49 ms
11,060 KB
testcase_21 AC 49 ms
11,060 KB
testcase_22 AC 49 ms
11,060 KB
testcase_23 AC 49 ms
11,060 KB
testcase_24 AC 49 ms
11,060 KB
testcase_25 AC 49 ms
11,060 KB
testcase_26 AC 49 ms
11,060 KB
testcase_27 AC 49 ms
11,060 KB
testcase_28 AC 49 ms
11,060 KB
testcase_29 AC 49 ms
11,060 KB
testcase_30 AC 49 ms
11,060 KB
testcase_31 AC 49 ms
11,060 KB
testcase_32 AC 49 ms
11,060 KB
testcase_33 AC 50 ms
11,060 KB
testcase_34 AC 49 ms
11,060 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 rrep(i,a,b) for(int i=(a);i>(b);i--)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll; typedef pair<ll, ll> P;
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; }
template<typename A,size_t N,typename T>void Fill(A(&array)[N],const T &val){fill((T*)array, (T*)(array+N), val);}
const int inf = INT_MAX / 2; const ll INF = LLONG_MAX / 2;
//template end

int mod = 1e9+7;
struct Mint {
    int val;
    Mint 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);
        return Mint(x);
    }
public:
    Mint() :val(0) {}
    Mint(ll x) :val(x >= 0 ? x % mod : x % mod + mod) {}
    int mtoi() { return this->val; }
    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) { return *this *= x.inv(); }
    bool operator==(const Mint& x) const { return val == x.val; }
    bool operator!=(const Mint& x) const { return val != x.val; }
    bool operator<(const Mint& x) const { return val < x.val; }
    bool operator<=(const Mint& x) const { return val <= x.val; }
    bool operator>(const Mint& x) const { return val > x.val; }
    bool operator>=(const Mint& x) const { return val >= x.val; }
    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; }
};
struct factorial {
    vector<Mint> Fact, Finv;
public:
    factorial(int maxx) {
        Fact.resize(maxx+1,Mint(1)),Finv.resize(maxx+1); rep(i,0,maxx)Fact[i+1]=Fact[i]*Mint(i+1);
        Finv[maxx]=Mint(1)/Fact[maxx]; rrep(i,maxx,0)Finv[i-1]=Finv[i]*Mint(i);
    }
    Mint fact(int n,bool inv) { if(inv) return Finv[n]; else return Fact[n]; }
    Mint nPr(int n,int r) { if(n<0||n<r||r<0) return Mint(0); else return Fact[n]*Finv[n-r]; }
    Mint nCr(int n,int r) { if(n<0||n<r||r<0) return Mint(0); else return Fact[n]*Finv[r]*Finv[n-r]; }
};
typedef vector<vector<Mint>> mat;
mat mul(mat a, mat b) {
    mat ans(a.size(), vector<Mint>(b[0].size(), 0));
    rep(i, 0, a.size())rep(j, 0, b[0].size())rep(k,0,b.size())ans[i][j] += a[i][k]*b[k][j];
    return ans;
}
mat matpow(mat a, ll t) {
    mat ans(a.size());
    rep(i, 0, a.size()){ans[i].resize(a.size(),0);ans[i][i]=1;}
    while(t){if(t&1)ans=mul(ans,a);a=mul(a,a);t>>=1;}
    return ans;
}
int gauss(mat &A, bool is_extended = false) {
    int m=A.size(), n=A[0].size(), rank=0;
    rep(col,0,n) {
        if (is_extended && col==n-1) break; int pivot = -1;
        rep(row,rank,m)if (A[row][col].mtoi()) {pivot = row; break;}
        if (pivot==-1) continue;
        swap(A[pivot],A[rank]); Mint inv=A[rank][col];
        rep(col2,0,n)A[rank][col2] /= inv;
        rep(row,0,m)if (row != rank && A[row][col].mtoi()) {
            Mint base=A[row][col];
            rep(col2,0,n) {A[row][col2]-=(A[rank][col2]*base);}
        } ++rank;
    } return rank;
}
int linear_equation(mat A, vector<ll> b, vector<ll> &res) {
    int m=A.size(),n=A[0].size(); mat M(m);
    rep(i,0,m){
        M[i].resize(n+1);
        rep(j,0,n) M[i][j] = A[i][j];
        M[i][n] = b[i];
    }
    int rank = gauss(M, true);
    rep(row,rank,m) if(M[row][n].mtoi()) return -1;
    res.resize(n); rep(i,0,rank)res[i] = M[i][n].mtoi();
    return rank;
}
bool IsPrime(int num){
    if(num < 2) return false;
    else if(num == 2) return true;
    else if(!num&1) return false;
    rep(i,3,sqrt(num)+1){
        if (num % i == 0)return false;
        i++;
    } return true;
}

int main(){
    int n,k; scanf("%d%d",&n,&k);
    Mint ans=0;
    int g=__gcd(n,k),div=2;
    vector<int> p;
    while(g!=1){
        if(IsPrime(div)&&g%div==0){
            p.push_back(div);
            while(g%div==0)g/=div;
        }
        div++;
    }
    factorial fact(1000010);
    int sz=p.size(),m=1<<sz;
    rep(mask,1,m){
        div=1;
        rep(i,0,sz)if(mask>>i&1)div*=p[i];
        Mint add=fact.nCr(n/div,k/div);
        if(__builtin_popcount(mask)&1)ans+=add;
        else ans-=add;
    }
    printf("%d",ans.mtoi());
    return 0;
}
0