当前位置: 代码迷 >> 综合 >> ZZULIOJ 2809: OH哥的倍数问题(Hard Version)(容斥定理)
  详细解决方案

ZZULIOJ 2809: OH哥的倍数问题(Hard Version)(容斥定理)

热度:69   发布时间:2023-11-25 07:38:08.0

2809: OH哥的倍数问题(Hard Version)

在这里插入图片描述

#include<stdio.h>
typedef long long LL;
int n;
LL get(int x){
    return 1LL*(1+n/x)*(n/x)*x/2;}
int gcd(int a,int b){
    return b?gcd(b,a%b):a;}
int lcm(int a,int b,int c,int d,int e)
{
    int x=a*b/gcd(a,b);x=x*c/gcd(x,c);x=x*d/gcd(x,d);x=x*e/gcd(x,e);return x;
}
int main()
{
    int T;scanf("%d",&T);while(T--){
    int a,b,c,d,e;scanf("%d%d%d%d%d%d",&n,&a,&b,&c,&d,&e);LL res=get(a)+get(b)+get(c)+get(d)+get(e)-get(lcm(a,b,1,1,1))-get(lcm(a,c,1,1,1))-get(lcm(a,d,1,1,1))-get(lcm(a,e,1,1,1))-get(lcm(b,c,1,1,1))-get(lcm(b,d,1,1,1))-get(lcm(b,e,1,1,1))-get(lcm(c,d,1,1,1))-get(lcm(c,e,1,1,1))-get(lcm(d,e,1,1,1))+get(lcm(a,b,c,1,1))+get(lcm(a,b,d,1,1))+get(lcm(a,b,e,1,1))+get(lcm(a,c,d,1,1))+get(lcm(a,c,e,1,1))+get(lcm(a,d,e,1,1))+get(lcm(b,c,d,1,1))+get(lcm(b,c,e,1,1))+get(lcm(b,d,e,1,1))+get(lcm(c,d,e,1,1))-get(lcm(a,b,c,d,1))-get(lcm(a,b,c,e,1))-get(lcm(a,b,d,e,1))-get(lcm(a,c,d,e,1))-get(lcm(b,c,d,e,1))+get(lcm(a,b,c,d,e));printf("%lld\n",res);}return 0;
}
  相关解决方案