1. 程式人生 > >[51nod]1284 2 3 5 7的倍數

[51nod]1284 2 3 5 7的倍數

std fine bsp () 一個 style inpu 容斥原理 black

給出一個數N,求1至N中,有多少個數不是2 3 5 7的倍數。 例如N = 10,只有1不是2 3 5 7的倍數。 Input
輸入1個數N(1 <= N <= 10^18)。
Output
輸出不是2 3 5 7的倍數的數共有多少。
Input示例
10
Output示例
1

容斥原理,先求反面,即1-N有多少是2 3 5 7的倍數。
cnt = A2+A3+A5+A7-(A23+A25+A27+A35+A37+A57)+(A235+A237+A257+A357)-A2357
A2表示被2整除的個數 即N/2 A23為即被2又被3整除的個數即N/6
最後結果為 N-cnt

代碼:
#include <stdio.h>
#include 
<iostream> using namespace std; #define LL long long int main() { //freopen("1.txt", "r", stdin); LL N; cin >> N; LL cnt = 0; cnt += (N/2 + N/3 + N/5 + N/7); cnt -= (N/6 + N/10 + N/14 + N/15 + N/21 + N/35); cnt += (N/30 + N/42 + N/70 + N/105); cnt -= (N/210); cout
<< N - cnt; return 0; }



[51nod]1284 2 3 5 7的倍數