結果
| 問題 | 
                            No.1727 [Cherry 3rd Tune] Stray
                             | 
                    
| コンテスト | |
| ユーザー | 
                            👑  tatyam
                         | 
                    
| 提出日時 | 2021-10-28 19:00:09 | 
| 言語 | C++23  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 6,000 ms | 
| コード長 | 1,231 bytes | 
| コンパイル時間 | 3,133 ms | 
| コンパイル使用メモリ | 258,616 KB | 
| 実行使用メモリ | 6,820 KB | 
| 最終ジャッジ日時 | 2024-10-07 09:18:21 | 
| 合計ジャッジ時間 | 3,803 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 8 | 
ソースコード
#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace std;
using Modint = atcoder::modint1000000007;
vector<pair<int, int>> factor(int x){
    vector<pair<int, int>> ans;
    if(x % 2 == 0){
        ans.emplace_back(2, 1);
        while((x /= 2) % 2 == 0) ans.back().second++;
    }
    for(int y = 3; y * y <= x; y += 2) if(x % y == 0){
        ans.emplace_back(y, 1);
        while((x /= y) % y == 0) ans.back().second++;
    }
    if(x != 1) ans.emplace_back(x, 1);
    return ans;
}
void solve(){
    int N, c;
    cin >> N >> c;
    const Modint C = c;
    vector<pair<int, int>> a = {{1, 2}}; // (いくつあるか, 何乗するか)
    for(auto [p, q] : factor(N)){
        vector<pair<int, int>> b;
        vector<int> pow(q + 1);
        pow[0] = 1;
        for(int i = 0; i < q; i++) pow[i + 1] = pow[i] * p;
        for(int i = 0; i < q; i++) for(auto [x, y] : a) b.emplace_back(x * (pow.rbegin()[i] - pow.rbegin()[i + 1]), y * pow[i]);
        for(auto [x, y] : a) b.emplace_back(x, y * pow[q]);
        swap(a, b);
    }
    Modint ans = C.pow(N) * N;
    for(auto [x, y] : a) ans += C.pow(y) * x;
    cout << (ans / N / 2).val() << endl;
}
int main(){
    int T;
    cin >> T;
    while(T--) solve();
}
            
            
            
        
            
tatyam