才学了Lucas定理,觉得很好玩。这两道题也挺好玩的。
Xiao Ming's Hope
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1898 Accepted Submission(s): 1269
1 2 11
2 2 8
这个题目求的是C(n,0) C(n,1) 一直到 C(n,n)中有多少个奇数。
因为只分奇偶,所以由Lucas定理可知,C(n,x)%2=C(n[0],x[0])*C(n[1],x[2])*...*C(n[m],x[m])%2。里面只有四种情况,而这四种情况里面只有C(0,1)=0。其余的C(1,1)C(0,0)C(1,0)均为1。也就是说,最终结果想要是1,n在二进制下是固定的,当n在某一位为0时,想要最终结果为奇数,x别无选择,这一位只能是0。当n在某一位为1时,那么x在这一位有两种选择,1或者0。
所以只需要查n在二进制下有多少个1,结果再去做2的幂运算。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;
int n;
void solve()
{
int res = 0;
while (n)
{
if (n & 1)
res++;
n = n >> 1;
}
cout << (1 << res) << endl;
}
int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
while (scanf("%d", &n) != EOF)
{
solve();
}
//system("pause");
return 0;
}
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 6799 | Accepted: 2836 |
Description
The binomial coefficient C(n, k) has been extensively studied for its importance in combinatorics. Binomial coefficients can be recursively defined as follows:
C(n, 0) = C(n, n) = 1 for all n > 0;
C(n, k) = C(n − 1, k − 1) + C(n − 1, k) for all 0 < k < n.
Given n and k, you are to determine the parity of C(n, k).
Input
The input contains multiple test cases. Each test case consists of a pair of integers n and k (0 ≤ k ≤ n < 231, n > 0) on a separate line.
End of file (EOF) indicates the end of input.
Output
For each test case, output one line containing either a “0
” or a “1
”, which is the remainder of C(n, k) divided by two.
Sample Input
1 1 1 0 2 1
Sample Output
1 1 0
这个题目就比较简单了,求C(n,k)的奇偶性。
直接套Lucas公式。将n、k都化成二进制求每一位的结果,相乘即可。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
using namespace std;
typedef long long ll;
int n, k;
int cal(int x, int y)
{
if (x == 0 && y == 1)
{
return 0;
}
else
{
return 1;
}
}
void solve()
{
int res = 1;
while (n&&k)
{
res = res*cal(n % 2, k % 2);
n = n / 2;
k = k / 2;
if (res == 0)
break;
}
printf("%d\n", res);
}
int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout);
while (scanf("%d%d", &n, &k) != EOF)
{
solve();
}
//system("pause");
return 0;
}