Subtask 0
样例把答案告诉你了。
Subtask 1
直接爆搜即可,期望得分 分,复杂度: 。
Subtask 2
记 表示小柴可以选的魔法元素,记小柴选的魔法元素序列为 ,那么答案就是
我们考虑递推来求,即小柴放了前 个时的值怎么递推出来的。我们发现,前面 个选了哪些是不重要的,我们只关心他们的 值,我们可以记录下来放了前 个 值为 的方案有多少种,记为 ,那么对于 ,对于每个 和可以选的 ,即 ,都有
那么最后的答案就是:
复杂度:。
代码:
#include<bits/stdc++.h>
using namespace std;
using lll = __int128;
using ll = long long;
namespace Mashiro {
char buf[1<<18],*p1=buf,*p2=buf;
inline int getc() {
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline void read(T& x) {
x=0;int f=1;
char ch=getc();
while(!isdigit(ch)){if(ch=='-')f=~f+1;ch=getc();}
while (isdigit (ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getc();}
x*=f;
}
template <typename T,typename... Args> inline void read(T& x, Args&... args) {
read(x);
read(args...);
}
char buffer[1<<18];int p11=-1;const int p22=(1<<18)-1;
inline void flush() {fwrite(buffer,1,p11+1,stdout),p11=-1;}
inline void putc(const char &x) {if (p11==p22) flush();buffer[++p11]=x;}
template<typename T>inline void write(T x) {
static int buf[40],top=0;
if(x<0)putc('-'),x=~x+1;
while(x)buf[++top]=x%10,x/=10;
if(top==0)buf[++top]=0;
while (top) putc(buf[top--]^48);
putc(' ');
flush();
}
template <typename T,typename... Args> inline void write(T x, Args... args) {
write(x);
write(args...);
}
}
using namespace Mashiro;
const ll Mod=998244353;
int n,T,m,k;
ll cnt[1005][205];
int ggcd[1005][1005];
int Gcd(int x,int y){
if(ggcd[x][y])return ggcd[x][y];
else if(y==0)return ggcd[x][y]=x;
else return ggcd[y][x]=ggcd[x][y]=Gcd(y,x%y);
}
ll ans[maxn];
int main(){
read(n,T,k);
for(int i(1);i<=n;++i)cnt[i][1]=1;
for(int i(1),t;i<=k;++i){
read(t);
cnt[t][1]=0;
}
for(int l(2);l<=205;++l){
for(int i(1);i<=n;++i){
for(int j(1);j<=n;++j){
cnt[Gcd(i,j)][l]=(cnt[Gcd(i,j)][l]+cnt[i][l-1]*cnt[j][1]%Mod)%Mod;
}
}
}
while(T--){
read(m);
ll ans=0;
for(int i(1);i<=n;++i){
ans=(ans+cnt[i][m]*i%Mod)%Mod;
}
write(ans);
}
return 0;
}Subtask 3
我们改变一下 dp 顺序,我们固定 里面的 反过来枚举 。那么对于 ,对于每个 和可以选的 ,都有
我们改成 的倍数形式:
这个是一个比较显然的容斥。现在我们先去掉 的限制,改为 ,那么显然, 的方案数也会被算进去,如果我们先知道了这些值,那么减去他们即可,注意到 ,倒序枚举即可。
现在的问题变成了怎么求上面那个式子,我们把完整式子写出来:
我们枚举 的倍数:
交换求和顺序:
我们记 ,用调和级数算计算 的复杂度就是 。
那么:
然后再减去 即可。
复杂度:,期望得分:。
#include<bits/stdc++.h>
using namespace std;
using lll = __int128;
using ll = long long;
namespace Mashiro {
char buf[1<<18],*p1=buf,*p2=buf;
inline int getc() {
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline void read(T& x) {
x=0;int f=1;
char ch=getc();
while(!isdigit(ch)){if(ch=='-')f=~f+1;ch=getc();}
while (isdigit (ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getc();}
x*=f;
}
template <typename T,typename... Args> inline void read(T& x, Args&... args) {
read(x);
read(args...);
}
char buffer[1<<18];int p11=-1;const int p22=(1<<18)-1;
inline void flush() {fwrite(buffer,1,p11+1,stdout),p11=-1;}
inline void putc(const char &x) {if (p11==p22) flush();buffer[++p11]=x;}
template<typename T>inline void write(T x) {
static int buf[40],top=0;
if(x<0)putc('-'),x=~x+1;
while(x)buf[++top]=x%10,x/=10;
if(top==0)buf[++top]=0;
while (top) putc(buf[top--]^48);
putc(' ');
flush();
}
template <typename T,typename... Args> inline void write(T x, Args... args) {
write(x);
write(args...);
}
}
using namespace Mashiro;
const int maxn=1e5+10;
const int LogN=31;
const ll Mod=998244353;
int n,T,m,k;
ll cnt[3005][3005];
ll pre[3005][3005];
int ggcd[3005][3005];
int Gcd(int x,int y){
if(ggcd[x][y])return ggcd[x][y];
else if(y==0)return ggcd[x][y]=x;
else return ggcd[y][x]=ggcd[x][y]=Gcd(y,x%y);
}
int main(){
read(n,T,k);
for(int i(1);i<=n;++i)cnt[i][1]=1;
for(int i(1),t;i<=k;++i){
read(t);
cnt[t][1]=0;
}
for(int i(1);i<=n;++i){
for(int j(1);j<=n/i;++j){
pre[i][1]+=cnt[i*j][1];
}
}
for(int l(2);l<=3000;++l){
for(int i(n);i>=1;--i){
cnt[i][l]=pre[i][l-1]*pre[i][1]%Mod;
for(int j(2);j<=n/i;++j){
cnt[i][l]=(cnt[i][l]-cnt[i*j][l]+Mod)%Mod;
}
for(int j(1);j<=n/i;++j){
pre[i][l]=(cnt[i*j][l]+pre[i][l])%Mod;
}
}
}
while(T--){
read(m);
ll ans=0;
for(int i(1);i<=n;++i){
ans=(ans+cnt[i][m]*i%Mod)%Mod;
}
write(ans);
}
return 0;
}Subtask 4
有了上面的启示,我们发现其实可以不用一个一个去推,因为有:
然后再容斥。
对于 非常大,我们时间空间都经不住,我们考虑少存一些东西,一个比较好想的东西就是我们可以只存放了 个的,类似于倍增,那么记 表示选了前 个,最大公约数为 的方案数。转移和上面一样。
计算的时候类似的开一个 表示当前的值,然后做法参考倍增做法。
复杂度:,期望得分:。
#include<bits/stdc++.h>
using namespace std;
using lll = __int128;
using ll = long long;
namespace Mashiro {
char buf[1<<18],*p1=buf,*p2=buf;
inline int getc() {
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<18,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline void read(T& x) {
x=0;int f=1;
char ch=getc();
while(!isdigit(ch)){if(ch=='-')f=~f+1;ch=getc();}
while (isdigit (ch)) {x=(x<<3)+(x<<1)+(ch^48);ch=getc();}
x*=f;
}
template <typename T,typename... Args> inline void read(T& x, Args&... args) {
read(x);
read(args...);
}
char buffer[1<<18];int p11=-1;const int p22=(1<<18)-1;
inline void flush() {fwrite(buffer,1,p11+1,stdout),p11=-1;}
inline void putc(const char &x) {if (p11==p22) flush();buffer[++p11]=x;}
template<typename T>inline void write(T x) {
static int buf[40],top=0;
if(x<0)putc('-'),x=~x+1;
while(x)buf[++top]=x%10,x/=10;
if(top==0)buf[++top]=0;
while (top) putc(buf[top--]^48);
putc(' ');
flush();
}
template <typename T,typename... Args> inline void write(T x, Args... args) {
write(x);
write(args...);
}
}
using namespace Mashiro;
const int maxn=1e5+10;
const int LogN=31;
const ll Mod=998244353;
int n,T,m,k;
ll dp[maxn][LogN],f[maxn][LogN];
ll Dp[maxn],F[maxn];
using namespace Misaka;
int main(){
read(n,T,k);
for(int i(1);i<=n;++i){
dp[i][0]=1;
}
for(int i(1),t;i<=k;++i){
read(t);
dp[t][0]=0;
}
for(int i(1);i<=n;++i){
for(int j(1);j<=n/i;++j){
f[i][0]+=dp[j*i][0];
}
}
for(int l(1);l<31;++l){
for(int i(n);i>=1;--i){
dp[i][l]=f[i][l-1]*f[i][l-1]%Mod;
for(int j(2);j<=n/i;++j){
dp[i][l]=(dp[i][l]-dp[j*i][l]+Mod)%Mod;
}
for(int j(1);j<=n/i;++j){
f[i][l]=(f[i][l]+dp[i*j][l])%Mod;
}
}
}
while(T--){
read(m);
for(int i(30);~i;--i){
if(m&(1<<i)){
for(int j(1);j<=n;++j){
Dp[j]=dp[j][i];
F[j]=f[j][i];
}
m-=(1<<i);
break;
}
}
for(int j(30);~j;--j){
if(!m)break;
if(m&(1<<j)){
for(int i(n);i>=1;--i){
Dp[i]=F[i]*f[i][j]%Mod;
for(int l(2);l<=n/i;++l){
Dp[i]=(Dp[i]-Dp[l*i]+Mod)%Mod;
}
F[i]=0;
for(int l(1);l<=n/i;++l){
F[i]=(F[i]+Dp[l*i])%Mod;
}
}
}
}
ll ans=0;
for(int i(1);i<=n;++i){
ans=(ans+Dp[i]*i%Mod)%Mod;
}
write(ans);
}
return 0;
}