日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

hdu4349 Xiao Ming's Hope【C(n,m)的奇偶性】

發(fā)布時間:2025/4/16 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu4349 Xiao Ming's Hope【C(n,m)的奇偶性】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題意: Each line contains a integer n(1<=n<=10^8)?
? ? ? ? ?Output a single line with the number of odd numbers of C(n,0),C(n,1),C(n,2)...C(n,n).
? ? ??
? ? ? ?括弧[開始做的時候還是對統(tǒng)計二進制1的個數(shù)好像還是比較懵的.現(xiàn)在是會了
? ? ? 0. 看(0,0)=1 (1,0)=(1,1)=1 ?(0,1)=0
? ? ? 1. 那么把(n,i)的n,i都轉化為二進制的話也是看對應位置上的01搭配之后的奇偶性就可以了.
? ? ? 2. 知道了0.1.就可以找01搭配的規(guī)律了,n的二進制為全0的情況根本不存在, ?n的二進制對應為1的時候,無論i是多少?{0,1},?這一位參照0.算出來的結果都是 1 。也就是對n中的每一位二進制1對應的都有不同的i{0,1}值集合去匹配,就是2種情況.
? ? ? 3. 最終統(tǒng)計一下n中二進制1的個數(shù),計算一下這個個數(shù)對下對應的{0,1}集合總的情況就是了.

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <map> typedef long long ll; using namespace std; int main() {int n;while(scanf("%d",&n)!=EOF){ // ll ans=0; // while(n){if(n&1) ans++;n>>=1;}ll ans=__builtin_popcount(n);printf("%d\n",(1<<ans));}return 0; }

?總結:總的來說就是判斷組合數(shù)為奇偶的情況,上面這道題對應的n的值比較大,是比較優(yōu)的算法
當n比較小的時候還可以用下面的這條性質:C(n,k) ((n&k)==k)成立的話就是奇數(shù),反之為偶數(shù)(nefu 600)

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include <map> typedef long long ll; using namespace std; int main() {int n,k;while(scanf("%d%d",&n,&k)!=EOF){if((n&k)==k)cout<<"ODD"<<endl;else cout<<"EVEN"<<endl;}return 0; }

?

總結

以上是生活随笔為你收集整理的hdu4349 Xiao Ming's Hope【C(n,m)的奇偶性】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。