洛谷试炼场 4

P2257 YY的GCD

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

using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e7+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
bool vis[maxn];
ll sum[maxn];
int prim[maxn];
int mu[maxn],g[maxn];
int cnt;
void (int n){
mu[1]=1;
for(int i=2;i<=n;i++)
{
if(!vis[i]){mu[i]=-1;prim[++cnt]=i;}
for(int j=1;j<=cnt&&prim[j]*i<=n;j++)
{
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[prim[j]*i]=-mu[i];
}
}
for(int j=1;j<=cnt;j++)
for(int i=1;i*prim[j]<=n;i++)g[i*prim[j]]+=mu[i];
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+g[i];
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
get_mu(10000000);
int t;
cin>>t;
while(t--){
int n,m;
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)*(sum[r]-sum[l-1]);
}
cout<<ans<<endl;
}
return 0;
}

[POI2007]ZAP-Queries

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

using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e5+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
bool vis[maxn];
ll sum[maxn];
int prim[maxn];
int mu[maxn],g[maxn];
int cnt;
void (int n){
mu[1]=1;
for(int i=2;i<=n;i++)
{
if(!vis[i]){mu[i]=-1;prim[++cnt]=i;}
for(int j=1;j<=cnt&&prim[j]*i<=n;j++)
{
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[prim[j]*i]=-mu[i];
}
}
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+mu[i];
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
get_mu(50000);
int t;
cin>>t;
while(t--){
int a,b,d;
cin>>a>>b>>d;
int mx=min(a/d,b/d);
ll ans=0;
for(int l=1,r;l<=mx;l=r+1){
r=min((a/d)/((a/d)/l),(b/d)/((b/d)/l));
ans+=(ll)((a/d)/l)*((b/d)/l)*(sum[r]-sum[l-1]);
}
cout<<ans<<endl;
}
return 0;
}

P3327 [SDOI2015]约数个数和

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

using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e5+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
bool vis[maxn];
ll sum[maxn];
int prim[maxn];
int mu[maxn];
ll g[maxn];
int cnt;
void (int n){
mu[1]=1;
for(int i=2;i<=n;i++)
{
if(!vis[i]){mu[i]=-1;prim[++cnt]=i;}
for(int j=1;j<=cnt&&prim[j]*i<=n;j++)
{
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[prim[j]*i]=-mu[i];
}
}
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+mu[i];
for(int i=1;i<=n;i++){
ll ans=0;
for(int l=1,r;l<=i;l=r+1){
r=(i/(i/l));
ans+=1LL*(r-l+1)*1LL*(i/l);
}
g[i]=ans;
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
get_mu(50000);
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
int mx=min(n,m);
ll ans=0;
for(int l=1,r;l<=mx;l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=(1LL*g[n/l]*1LL*g[m/l])*1LL*(sum[r]-sum[l-1]);
}
cout<<ans<<endl;
}
return 0;
}

P2522 [HAOI2011]Problem b

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
71
72
73

using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e5+50;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
bool vis[maxn];
ll sum[maxn];
int prim[maxn];
int mu[maxn];
ll g[maxn];
int cnt;
int k;
void (int n){
mu[1]=1;
for(int i=2;i<=n;i++)
{
if(!vis[i]){mu[i]=-1;prim[++cnt]=i;}
for(int j=1;j<=cnt&&prim[j]*i<=n;j++)
{
vis[i*prim[j]]=1;
if(i%prim[j]==0)break;
else mu[prim[j]*i]=-mu[i];
}
}
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+mu[i];
}
ll get(int a,int b){
int mx=min(a,b);
ll ans=0;
for(int l=1,r;l<=mx;l=r+1){
r=min(a/(a/l),b/(b/l));
ans+=(1ll*a/(1ll*l*k))*(1ll*b/(1ll*l*k))*(sum[r]-sum[l-1]);
}
return ans;
}

template <class T>
inline bool read(T &ret)
{
char c;
int sgn;
if (c = getchar(), c == EOF) return 0; //EOF
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
ret = (c == '-') ? 0 : (c - '0');
while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
ret *= sgn;
return 1;
}
inline void out(ll x)
{
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
int main()
{
/* std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);*/
get_mu(50000);
int t;
//cin>>t;
read(t);
while(t--){
int a,b,c,d;
// cin>>a>>b>>c>>d>>k;
read(a);read(b);read(c);read(d);read(k);
out(get(b,d)-get(b,c-1)-get(a-1,d)+get(a-1,c-1));
puts("");
}
return 0;
}