The 2019 ICPC Asia Yinchuan Regional Programming Contest/2019银川区域赛 D Easy Problem(莫比乌斯容斥+欧拉降幂)
The 2019 ICPC Asia Yinchuan Regional Programming Contest/2019银川区域赛 D Easy Problem(莫比乌斯容斥+欧拉降幂)
题面:
题意:
给定4个整数\(n,m,d,k\),让你求\(\sum_{a_1=1}^{m}\sum_{a_2=1}^{m}\dots\sum_{a_n=1}^{m}[gcd(a_1,a_2,\dots,a_n)=d](a_1*a_2*\dots*a_n)^k\)。
思路:
利用莫比乌斯函数性质和乘法分配律得:
\[\sum_{a_1=1}^{m}\sum_{a_2=1}^{m}\dots\sum_{a_n=1}^{m}[gcd(a_1,a_2,\dots,a_n)=d](a_1*a_2*\dots*a_n)^k
\\
=d^{kn}\sum_{a_1=1}^{m/d}\sum_{a_2=1}^{m/d}\dots\sum_{a_n=1}^{m/d}[gcd(a_1,a_2,\dots,a_n)=1](a_1*a_2*\dots*a_n)^k
\\
=d^{kn}\sum_{a_1=1}^{m/d}\sum_{a_2=1}^{m/d}\dots\sum_{a_n=1}^{m/d}\sum_{j|gcd(a_1,a_2,\dots,a_n)}\mu(j)(a_1*a_2*\dots*a_n)^k
\\
=d^{kn}\sum_{j=1}^{m/d}\mu(j)\ j^k\sum_{a_1=1}^{m/d/j}\sum_{a_2=1}^{m/d/j}\dots\sum_{a_n=1}^{m/d/j}(a_1*a_2*\dots*a_n)^k
\\
=d^{kn}\sum_{j=1}^{m/d}\mu(j)\ j^k(\sum_{i=1}^{m/d/j}i^k)^n
\\
\]
这样我们就可以预处理出\(1^k,2^k,3^k,\dots,m^k\),然后枚举\(\mathit j\)进行快速求解,时间复杂度:\(O(T*m*logk)\)。
观察答案的计算式可以发现,\(\mathit n\)只存在于指数部分,于是我们可以利用欧拉降幂来将其降数量级。
欧拉降幂的公式为:
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
bool vis[maxn];
long long prim[maxn], mu[maxn], sum[maxn], cnt;
void get_mu(long long n)
{
mu[1] = 1;
for (long long i = 2; i <= n; i++) {
if (!vis[i]) {mu[i] = -1; prim[++cnt] = i;}
for (long long j = 1; j <= cnt && i * prim[j] <= n; j++) {
vis[i * prim[j]] = 1;
if (i % prim[j] == 0) { break; }
else { mu[i * prim[j]] = -mu[i]; }
}
}
// for (long long i = 1; i <= n; i++) { sum[i] = sum[i - 1] + mu[i]; }
}
ll euler(ll n) //log(n)时间内求一个数的欧拉值
{
ll ans = n;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
ans -= ans / i;
while (n % i == 0) { n /= i; }
}
}
if (n > 1) { ans -= ans / n; }
return ans;
}
char s[maxn];
ll m, d, k;
ll a[maxn];
int main()
{
#if DEBUG_Switch
freopen("D:\\code\\input.txt", "r", stdin);
#endif
//freopen("D:\\code\\output.txt","w",stdout);
get_mu(maxn - 1);
ll mod = euler(59964251);
ll base = 59964251ll;
int t;
t = readint();
while (t--) {
scanf("%s %lld %lld %lld", s + 1, &m, &d, &k);
int len = strlen(s + 1);
ll n = 0;
repd(i, 1, len) {
n = n * 10 + (s[i] - '0');
if (n > mod) {
n = n % mod + mod;
}
}
repd(i, 1, m / d) {
a[i] = powmod(i, k, base);
sum[i] = (a[i] + sum[i - 1]) % base;
}
repd(i, 1, m / d) {
sum[i] = powmod(sum[i], n, base);
}
ll ans = 0ll;
repd(j, 1, m / d) {
ans += mu[j] * sum[m / d / j] % base * powmod(a[j], n, base) % base;
ans = (ans + base) % base;
}
ans = ans * powmod(d, k * n , base) % base;
printf("%lld\n", ans );
}
return 0;
}
本博客为本人原创,如需转载,请必须声明博客的源地址。
本人博客地址为:www.cnblogs.com/qieqiemin/
希望所写的文章对您有帮助。