当前位置: 代码迷 >> 综合 >> HDU6069 2017 Multi-University Training Contest - Team 4 1003 Counting Divisors(唯一分解定理+思维优化)
  详细解决方案

HDU6069 2017 Multi-University Training Contest - Team 4 1003 Counting Divisors(唯一分解定理+思维优化)

热度:37   发布时间:2023-12-06 19:34:27.0

题目大意:

求L到R范围内,每个数K次方的因数个数之和。

思路:

唯一分解定理


不要对L-R之间内的数一次次地进行分解质因数,要把质因数的循环放外面,去试除L-R的每个数,这样才能做到一大波常数优化。

#include<bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1000105;
const ll mod = 998244353;
int prim[maxn], p[maxn], cnt = 0;
void init()
{for(ll i = 2; i <= 1000000; i++)if(prim[i] == 0){p[++cnt] = i;for(ll j = i*i; j <= 1000000; j += i)prim[j] = 1;}
}
ll number[maxn], val[maxn], tol[maxn];
int main()
{init();int t;ll l, r, k;scanf("%d", &t);while(t--){cin >> l >> r >> k;for(ll i = l; i <= r; i++)number[i-l+1] = i, val[i-l+1] = 1;for(ll i = 1; i <= cnt; i++){if(r < p[i]) break;ll t1 = (l / p[i]) * p[i];for(; t1 <= r; t1 += p[i])if(t1 >= l){ll cou = 0;while(number[t1-l+1] % p[i] == 0){cou++;number[t1-l+1] /= p[i];}val[t1-l+1] = val[t1-l+1] * (cou * k + 1) % mod;}}for(ll i = l; i <= r; i++)if(number[i-l+1] != 1)val[i-l+1] = val[i-l+1] * (k + 1) % mod;ll ans = 0;for(ll i = l; i <= r; i++)ans = (ans + val[i-l+1]) % mod;cout << ans << endl;}return 0;
}







  相关解决方案