题解 P17269 [ICPC 2017 Urumqi R] Sum of the Line
没人发现答案其实很优美吗
题意
一个神秘三角形 中只有 的位置值为 ,否则为 。
而 ,要求第 行所有 的和。
交换求和号:
所以问题简化为:
求 到 中所有与 互质的数的平方和,对 取模。
莫比乌斯函数大法好
由
得:
其中有:
因此:
这里其实已经能做了,但是不难发现可以化简。
令:
展开后:
众所周知:
- ,本题 ,所以该项为 )。
- (因为 是积性函数,且在质数幂上的值为 )。
- 。
代入上式:
最终得到封闭形式:
代码
Code
#include<bits/stdc++.h>
using namespace std;
namespace rnd {
std::mt19937& rng() {
static std::mt19937 e(
static_cast<unsigned>(
std::chrono::steady_clock::now().time_since_epoch().count()
)
);
return e;
}
int nextInt(int l, int r) {
return std::uniform_int_distribution<int>(l, r)(rng());
}
double nextDouble(double l = 0.0, double r = 1.0) {
return std::uniform_real_distribution<double>(l, r)(rng());
}
template <typename Container>
void shuffle(Container& c) {
std::shuffle(c.begin(), c.end(),rng());
}
void seed(unsigned s) {
rng().seed(s);
}
}
constexpr int mod=998244353;
using ll=long long;
namespace STD{
ll n;
ll mp(ll x,ll y,ll p){
x %= mod;
ll ans = 1;
while(y) {
if(y & 1) ans = (x * ans) % p;
x=(x * x) % p;
y >>= 1;
}
return ans;
}
void work(){
cin >> n;
if(n == 1){
cout << 1 << "\n";
return;
}
ll temp = n;
vector <ll> ftr;
for(ll i = 2; i * i <= temp; i++){
if(temp % i == 0) ftr.push_back(i);
while(temp % i == 0){
temp /= i;
}
}
if(temp > 1) ftr.push_back(temp);
n %= mod;
ll phi = n;
for(auto v : ftr){
phi = phi * ((v - 1) % mod) % mod * mp(v % mod, mod - 2, mod) % mod;
}
ll ans = 2 * n % mod * n % mod * phi % mod;
ll res = n;
for(auto v : ftr){
ll factor = ((1 - v) % mod + mod) % mod;
res = res * factor % mod;
}
ans = (ans + res) % mod * mp(6, mod - 2, mod) % mod;
cout << ans << "\n";
}
}
struct TiMe{
using Clock = std::chrono::steady_clock;
using TP = Clock::time_point;
TP start_, end_;
void start(){
start_ = Clock::now();
}
void stop() {
end_ = Clock::now();
}
ll get_us(){
return chrono::duration_cast<std::chrono::microseconds>(end_ - start_).count();
}
}TIME;
void rw(){
ios::sync_with_stdio(0);
cin.tie(0);
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// TIME.start();
rw();
int T = 1; cin >> T;
while(T--) STD::work();
// TIME.stop();
// freopen("p.txt","w",stdout);
// cout<<TIME.get_us();
// system("fc .out .out");
return 0;
}复杂度分析
- 质因数分解:试除到 ,单次最坏 。
- 逆元计算:一次快速幂 。
- 总复杂度:。