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
1211
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;}
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 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;}
- HDU 4349 && POJ 3219 Lucas有趣应用c
- [Lucas定理推广] hdu 4349 and poj 3219
- HDU 4349——Lucas定理的巧妙应用
- HDU 4349 Xiao Ming's Hope (Lucas定理的应用)
- HDU 4349(Lucas 变形)
- hdu 4349(Lucas定理)
- HDU 4349 Xiao Ming's Hope(Lucas定理、C[n][m]为奇数的个数)
- hdu 4349 lucas定理的推导
- hdu 4349 lucas定理的推广
- HDU 3037 Saving Beans(Lucas定理的直接应用)
- 【HDU 4349】【组合数结论 LUCAS定理推广】 Xiao Ming's Hope【C[n][m]为奇数的个数】t
- hdu 1846 有趣,有趣
- hdu 3037 lucas定理
- hdu 3944 lucas
- HDU 5698 (数学 lucas)
- hdu 5894 组合Lucas
- hdu4349 lucas应用
- Lucas定理的应用
- 快速理解RequireJs中的config
- oks3c6410开发板 linux-3.0.1内核 ZC301P摄像头 构成视频监控系统时内核oops解决办法
- 第三十三课——循环语句
- 黑马程序员——super和this的区别和应用
- codeforces Police Recruits
- HDU 4349 && POJ 3219 Lucas有趣应用c
- 数据结构 总结
- POJ 1789 Kruskal
- Linux下Tomcat的启动、关闭
- KMP算法程序
- 51nod贪心例子独木舟问题
- BestCoder Round #65 1001 ZYB's Biology HDU5590
- 操作系统概念(第十章) 文件系统接口
- codeforces Prison Transfer