洛谷P2522的弱化版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

const int maxn=1e6+9;

ll tt=1e6,a,b,c,d,k,mu[maxn],s[maxn],pr[maxn],cnt,ans;
bitset<(int)1e7+9>vis;

void init(int x){
mu[1]=1;
for(ll i=2;i<=x;++i){
if(!vis[i])pr[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*pr[j]<=x;++j){
vis[pr[j]*i]=1;
if(i%pr[j]==0)break;
mu[i*pr[j]]=-mu[i];
}
}
for(int i=1;i<=x;++i)s[i]=s[i-1]+mu[i];
}

ll cal(ll x,ll y){
x/=k,y/=k;
if(x>y)swap(x,y);
ll res=0;
for(ll l=1,r;l<=x;l=r+1){
r=min(x/(x/l),y/(y/l));
res+=(x/l)*(y/l)*(s[r]-s[l-1]);
}
return res;
}


int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
init(tt);
cin>>a>>b>>k;
cout<<cal(a,b);
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll tt=5e4,a,b,c,d,k,mu[(int)2e6+9],s[(int)2e6+9],pr[(int)2e6+9],cnt,ans;
bitset<(int)1e7+9>vis;

void init(int x){
mu[1]=1;
for(ll i=2;i<=x;++i){
if(!vis[i])pr[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*pr[j]<=x;++j){
vis[pr[j]*i]=1;
if(i%pr[j]==0)break;
mu[i*pr[j]]=-mu[i];
}
}
for(int i=1;i<=x;++i)s[i]=s[i-1]+mu[i];
}

ll cal(ll x,ll y){
x/=k,y/=k;
if(x>y)swap(x,y);
ll res=0;
for(ll l=1,r;l<=x;l=r+1){
r=min(x/(x/l),y/(y/l));
res+=(x/l)*(y/l)*(s[r]-s[l-1]);
}
return res;
}


int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
init(tt);
cin>>tt;
f(S,1,tt){
cin>>a>>b>>c>>d>>k;
cout<<cal(b,d)-cal(b,c-1)-cal(a-1,d)+cal(a-1,c-1)<<"\n";
}
return 0;
}

有感觉了

两种不同公式的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n=2e6,mu[(int)2e6+9],pr[(int)2e6+9],cnt,ans, s[(int)2e6+9];
bitset<(int)1e7+9>vis;

void init(int x){
mu[1]=1;
for(ll i=2;i<=x;++i){
if(!vis[i])pr[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*pr[j]<=x;++j){
vis[pr[j]*i]=1;
if(i%pr[j]==0)break;
mu[i*pr[j]]=-mu[i];
}
}
for(int i=1;i<=x;++i)for(ll j=1;j*i<=x;++j)s[j*i]+=1ll*mu[j]*i;
for(int i=1;i<=x;++i)s[i]+=s[i-1];
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
init(n);
cin>>n;
for(ll l=1,r;l<=n;l=r+1){
r=n/(n/l);
ans+=1ll*(n/l)*(n/l)*(s[r]-s[l-1]);
}
ans=(ans-1ll*(n+1ll)*n/2)/2;
cout<<(ll)ans;
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n=2e6,mu[(int)2e6+9],s[(int)2e6+9],pr[(int)2e6+9],cnt,ans;
bitset<(int)1e7+9>vis;

void init(int x){
mu[1]=1;
for(ll i=2;i<=x;++i){
if(!vis[i])pr[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*pr[j]<=x;++j){
vis[pr[j]*i]=1;
if(i%pr[j]==0)break;
mu[i*pr[j]]=-mu[i];
}
}
for(int i=1;i<=x;++i)s[i]=s[i-1]+mu[i];
}

ll cal(int x){
ll res=0;
for(ll l=1,k=n/x,r;l<=k;l=r+1){
r=k/(k/l);
res+=(k/l)*(k/l)*(s[r]-s[l-1]);
}
return res;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
init(n);
cin>>n;
for(int i=1;i<=n;++i)ans+=1ll*i*cal(i);
ans=(ans-1ll*n*(n+1ll)/2)/2;
cout<<ans;
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

int tt=1e7,n,m,mu[(int)1e7+9],s[(int)1e7+9],pr[(int)1e7+9],cnt;
bitset<(int)1e7+9>vis;

void init(int x){
mu[1]=1;
for(ll i=2;i<=x;++i){
if(!vis[i])pr[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*pr[j]<=x;++j){
vis[pr[j]*i]=1;
if(i%pr[j]==0)break;
mu[i*pr[j]]=-mu[i];
}
}
for(int i=1;i<=cnt;++i)for(ll j=1;j*pr[i]<=x;++j)s[j*pr[i]]+=mu[j];
for(int i=1;i<=x;++i)s[i]+=s[i-1];
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
init(tt);
cin>>tt;
f(S,1,tt){
cin>>n>>m;
if(n>m)swap(n,m);
ll ans=0;
for(int l=1,r;l<=n;l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=1ll*(n/l)*(m/l)*(s[r]-s[l-1]);
}
cout<<ans<<"\n";
}
return 0;
}

题意是要找出一个最小集合,使得从其中选取任意次数的元素之和等于或者大于

容易发现一个可能解就是题目给定的集合,如果这不是一个解,那么答案就是无解。

接下来考虑的是如何缩减这个集合,显然如果有,那么就可以从解集中删除,而且容易发现不可能有某个元素仅由三个及以上的元素相加才可得,因为,那么必有

所以我们只要一次NTT,直接卷自己即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <bits/stdc++.h>
using namespace std;

using ll=long long;

const int mod=998244353;
const int g=3,gi=332748118,NR=1<<21;

int n,m,a[(int)1e6+9],A[NR],B[NR],rev[NR],lim=1,bit,ans;
int powmod(int a,int b){
int res=1;
for(;b>0;b>>=1,a=1ll*a*a%mod)if(b&1)res=1ll*res*a%mod;
return res;
}

void NTT(int *a,int n,int type){
for(int i=0;i<n;++i)if(i<rev[i])swap(a[i],a[rev[i]]);
for(int i=1;i<n;i<<=1){
int gn=powmod(type?g:gi,(mod-1)/(i<<1));
for(int j=0;j<n;j+=(i<<1)){
ll g0=1;
for(int k=0;k<i;++k,g0=1ll*g0*gn%mod){
int x=a[j+k],y=g0*a[i+j+k]%mod;
a[j+k]=(x+y)%mod,a[i+j+k]=(x-y+mod)%mod;
}
}
}
if(type==1)return ;
int invn=powmod(n,mod-2);
for(int i=0;i<n;++i)a[i]=1ll*a[i]*invn%mod;
}

bitset<(int)1e6+9> vis;


int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n;++i)cin>>a[i];
ans=n;
while(lim<=2*m)lim<<=1,bit+=1;
for(int i=0;i<lim;++i)rev[i]=(rev[i>>1]>>1)|((i&1)<<(bit-1));
for(int i=1;i<=n;++i)A[a[i]]=B[a[i]]=1;
NTT(A,lim,1),NTT(B,lim,1);
for(int i=0;i<lim;++i)A[i]=1ll*A[i]*B[i]%mod;
NTT(A,lim,0);
for(int i=1;i<=n;++i)if(A[a[i]])ans-=1,A[a[i]]=0,vis[a[i]]=1;
for(int i=0;i<=m;++i)if(A[i]){
cout<<"NO\n";
return 0;
}
cout<<"YES\n"<<ans<<"\n";
for(int i=1;i<=n;++i)if(!vis[a[i]])cout<<a[i]<<" ";
}
0%