P12226 「WyOJ Round 1」启 · 破茧初阳
题目
求从公元后 年至公元后 年有多少年为快乐年?
快乐年的定义为:能被 整除且不能被 整除,或者是能被 整除的年份,即将闰年定义中的 依次替换为 。
思路
这里有两种情况:
- 能被 整除且不能被 整除。
- 能被 整除的年份。
根据容斥原理,我们就可以写成 。
第一种情况就是 。
第二种情况就是 。
而两种情况的并集就是 。
Code
#include<bits/stdc++.h>
#define int __int128//注意由于数据过大,需要用到 __int128 来代替 long long
using namespace std;
int T;
int n,a,b,c;
void read(int &x) {
x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch - '0');
ch = getchar();
}
}
void print(int x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
int gcd(int a,int b)
{
while(b!=0)
{
a%=b;
swap(a,b);
}
return a;
}
int lcm(int a,int b)
{
return (a/gcd(a,b))*b;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
read(T);
while(T--)
{
read(n),read(a),read(b),read(c);
int a_b=lcm(a,b);
int a_c=lcm(a,c);
int a_b_c=lcm(a_b,c);
int x=n/a-n/a_b;
int y=n/c;
int x_y=n/a_c-n/a_b_c;
print(x+y-x_y);
printf("\n");
}
return 0;
}