题解 P5153 【简单的函数】
思路与Great_Influence相似,更加详细且有代码。
首先观察题目条件。若
- 对于任意
i(1\le i<t) 都有i\mid x ,即lcm(1,2,\dots,t-1)\mid x -
lcm(1,2,\dots,t)\nmid x
根据这个原理我们进行打表不难发现使得
将
注意到这些
时间复杂度显然是正确的。
代码:
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define mod 1000000007
using namespace std;
typedef long long ll;
const int MAXN = 50;
int f[MAXN];
ll n,lcm[MAXN],cnt[10];
inline ll gcd(ll a, ll b)
{
return b?gcd(b,a%b):a;
}
inline ll calc(ll l, ll r, ll x)
{
return r/x-(l-1)/x;
}
inline ll qpow(ll a, ll b)
{
ll res = 1;
for(; b; a = a*a%mod, b >>= 1ll)
if(b&1ll)
res = res*a%mod;
return res;
}
int main()
{
cin >> n;
f[2] = 1;
for(int i = 3; i<=50; i++)
for(int j = 2; j<i; j++)
if(i%j)
{
f[i] = f[j]+1;
break;
}
lcm[1] = 1;
int pos = 2;
for(pos = 2; pos<=50; pos++)
{
lcm[pos] = lcm[pos-1]*(pos/gcd(pos,lcm[pos-1]));
if(lcm[pos]>n)
break;
}
for(int i = 2; i<=pos; i++)
cnt[f[i]+1] += calc(pos+1,n,lcm[i-1])-calc(pos+1,n,lcm[i]);
for(int i = 2; i<=pos; i++)
cnt[f[i]]++;
ll ans = 1;
for(int i = 2; i<=5; i++)
ans = ans*qpow(i,cnt[i])%mod;
cout << ans << endl;
return 0;
}