题目要求
Description
Tadashi喜欢2种数,7的倍数,17的倍数,他想知道<=n中他"不"喜欢的数有多少个?
Input
一个整数T(1<=T<=200),T组测试
一个整数N,N(1<=N<=1e18)
Output
N(包括N)以内不是 7,17 的倍数的个数
Sample Input
Raw
2
17
119
Sample Output
Raw
14
96
刚开始做题目时没注意到数字的范围,采用对n进行遍历求解,显而易见的超时了。
1e18=1*10^18
正确的解法是利用n中有多少个7(a)和17(b),采用整除的方法来寻找个数。但是7和17中间是有公倍数的导致了中间交叉的数字发生重复,所以需要寻找7和17的最小公倍数(ab)的个数,总数sum=a+b-ab
完整代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include <stdlib.h>
using namespace std;
int main(void)
{
int m;
long long sum, a, b, ab,n;
cin >> m;
while (m--)
{
while (cin >> n)
{
a = n / 7;
b = n / 17;
ab = n / 119;//7*17
sum = a + b - ab;
cout << n - sum << endl;
}
}
return 0;
}