Problem Solving report SOJ2668 C (n,k)
Description The parity input file of the combined number C (n, k) is multi-case, one n (1<=n<=10^9) and K (0<=k<=n) per line, and when n equals 0 and K equals 0 o'clock input end output For each case, the output line is the parity of the combined number C (n, k), odd output 1, and even output 0 sample Input2 0 Sample Output10 Author windy7926778
Main topic: Give N and K, calculate the parity of C (n,k), odd words output 1, even output 0.
Analysis: First of all, it must be clear that it cannot be hard to calculate directly. So we need to find a simple way. We want to see whether a number is odd or even, in addition to the last one, you can see if he can be divisible by 2, or he has no quality factor 2. We know that C (n,k) can be represented as n!/k!/(N-K)!. In this way, we use the arithmetic basic theorem of prime numbers, that is, the mass factor decomposition of n!, the number of mass factor p is pn (n,p) =[n/p]+[n/p^2]+[n/p^3]+ ... Until [N/p^q]==0, where [] means rounding down. Then we calculate PN (n,2)-pn (k,2)-PN (n-k,2) According to this formula, if the result >=1 description is even, ==0 is odd.
on the code:
#include <iostream> #include <cmath>using namespace Std;long long get (long long N) {Long long = 2;long long num = 0;while (n >=) {num + = N/two;two *=;} return num;} int main () {Long long n, k;while (cin >> n >> k&&n) {if (k = = 0) {cout << 1 << Endl;} Else{long Long num = Get (n)-Get (N-k)-get (k); cout << (num = = 0? 1:0) << Endl;}} return 0;}
Number theory brushes up and away.
Problem Solving report SOJ2668 C (N,k)