您的位置:首页 > 运维架构

HDU 4349 Xiao Ming's Hope (组合数的奇偶性&&Lucas定理)

2013-10-04 15:49 369 查看
今天比赛运气比较好,直接就做到了数学题。本来也想找规律来做,结果怕浪费时间,列到C(6,k)没发现什么就直接放弃开始暴力,开始又想打表打到1e8,结果又莫名其妙错误,到了50min才A,用的也不是什么好方法。。。竟然还交错题。。。交对了又TLE。。。我这个方法他过了真是奇葩。。。

题目链接


Xiao Ming's Hope

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1153 Accepted Submission(s): 792



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

1
2
11


Sample Output

2
2
8


这是我的马上就要超时的代码,思路如下:我们要判断组合数的寄偶,其计算方法为 n! / (n-m) ! * m ! 也就是说看看m,n的因数中偶数的位置,是不是一一对应,如果2进制位操作且&中,结果与m大小相同,则n-m的可以直接进行二进值减法且1位不改变,也就是说m 与 n-m的偶数的因数,与n的因数完全抵消,即为奇数!!!

#pragma comment(linker, "/STACK:102400000,102400000")
#include "iostream"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "sstream"
#include "queue"
#include "vector"
#include "string"
#include "stack"
#include "cstdlib"
#include "deque"
#include "fstream"
#include "map"
using namespace std;
typedef long long LL;
const int INF = 0x1fffffff;
const int MAXN = 1000000+100;
#define eps 1e-14
const int mod = 100000007;

int main()
{
int n,sum,i;
freopen("in","r",stdin);
while(scanf("%d",&n)!=EOF)
{
if(n%2!=0)
{
sum=0;
for(i=0;i<=n/2;i++)
{
if((n&i)==i)
sum++;
}
sum*=2;
}
else
{
sum=0;
for(i=0;i<n/2;i++)
{
if((n&i)==i)
sum++;
}
sum*=2;
if((n&(n/2))==(n/2))
sum++;
}
printf("%d\n",sum);
}
return 0;
}


于是学习了一下大神的算法,还有某个童鞋在赛场上琢磨出来的lucas定理。。。。

#include<stdio.h>
int main()
{
int s,n,i,g;
while(~scanf("%d",&n))
{
g=1,s=0;
while(n)
{
s+=(n&1);
n/=2;
}
for(i=1; i<=s; i++)
{
g*=2;
}
printf("%d\n",g);
}
}


其实原理是一样的。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: