Problem Solving report SOJ2668 C (N,k)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.