HDU 4349 && POJ 3219 Lucas有趣应用c

来源:互联网 发布:matlab数据导入 编辑:程序博客网 时间:2024/06/09 19:06

才学了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


Problem Description
Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The day 2011.11.11 comes. Seeing classmates walking with their girl friends, he coundn't help running into his classroom, and then opened his maths book preparing to count odd numbers. He looked at his book, then he found a question "C(n,0)+C(n,1)+C(n,2)+...+C(n,n)=?". Of course, Xiao Ming knew the answer, but he didn't care about that , What he wanted to know was that how many odd numbers there were? Then he began to count odd numbers. When n is equal to 1, C(1,0)=C(1,1)=1, there are 2 odd numbers. When n is equal to 2, C(2,0)=C(2,2)=1, there are 2 odd numbers...... Suddenly, he found a girl was watching him counting odd numbers. In order to show his gifts on maths, he wrote several big numbers what n would be equal to, but he found it was impossible to finished his tasks, then he sent a piece of information to you, and wanted you a excellent programmer to help him, he really didn't want to let her down. Can you help him?
 

Input
Each line contains a integer n(1<=n<=108)
 

Output
A single line with the number of odd numbers of C(n,0),C(n,1),C(n,2)...C(n,n).
 

Sample Input
1211
 

Sample Output
228

这个题目求的是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;}

Binomial Coefficients
Time Limit: 1000MS Memory Limit: 131072KTotal Submissions: 6799 Accepted: 2836

Description

The binomial coefficient C(nk) has been extensively studied for its importance in combinatorics. Binomial coefficients can be recursively defined as follows:

C(n, 0) = C(nn) = 1 for all n > 0;
C(nk) = 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(nk).

Input

The input contains multiple test cases. Each test case consists of a pair of integers n and k (0 ≤ k ≤ n < 231n > 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(nk) divided by two.

Sample Input

1 11 02 1

Sample Output

110

这个题目就比较简单了,求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;}




0 0