(bc #45) A - Dylans loves numbers (hdu 5272)

Dylans loves numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 569    Accepted Submission(s): 320

Problem Description

Who is Dylans?You can find his ID in UOJ and Codeforces.
His another ID is s1451900 in BestCoder.

And now today‘s problems are all about him.

Dylans is given a number N.
He wants to find out how many groups of "1" in its Binary representation.

If there are some "0"(at least one)that are between two "1",
then we call these two "1" are not in a group,otherwise they are in a group.

Input

In the first line there is a number T.

T is the test number.

In the next T lines there is a number N.

0≤N≤1018,T≤1000

Output

For each test case,output an answer.

Sample Input

1
5

Sample Output

2

Source

BestCoder Round #45

快要炸了..

tle成狗

因为是tle,看了下自己没有写cin cout,估计就是算法的问题...

我是先存了二进制的每一位到数组,然后扫一遍...

嗯,这都tle...

那我不存不扫,直接记录当前二进制位和之前二进制位..

logn的复杂度总可以了吧啊?

还TLE..........

嗯,其实已经发现 n是小于等于1e18的,没开long long

但是一位没开long long 会是wa...就没理...

之后实在黔驴技穷,改了下,竟然过了...

然后想明白了.

因为存二进制的时候有一个while

没开long long 的话就炸了,不知道读进去的是什么,while就出不来,于是就tle了.T T

果然太年轻.

/*************************************************************************
    > File Name: code/bc/#45/1001.cpp
    > Author: 111qqz
    > Email: [email protected]
    > Created Time: 2015年07月29日 星期三 13时25分01秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=1E3+5;
int a[N];
LL n,nn;
int main()
{
    int T;
    cin>>T;
    while (T--)
    {
    scanf("%lld",&n);
    nn = n ;
    int k = 0;
    while (nn)
    {
        k++;
        a[k]=nn&1;
        nn = nn >>1;
    }
    int ans = 0;
//    for ( int i = 1 ; i <= k ; i++ )
//        cout<<a[i]<<endl;
    for ( int i = 1 ; i <= k ; i++ )
    {
        if (a[i]==1&&a[i-1]==0)
        ans++;
    }
    printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-15 15:00:50

(bc #45) A - Dylans loves numbers (hdu 5272)的相关文章

hdu 5272 Dylans loves numbers 水题

Dylans loves numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5272 Description Dylans是谁?你可以在 UOJ 和 Codeforces上看到他.在BestCoder里,他有另外一个ID:s1451900.今天的题目都和他有关哦.Dylans得到了一个数N.他想知道N的二进制中有几组1.如果两个1之间有若干个(至少一个)0

HDU 5272 Dylans loves numbers

Dylans loves numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 58    Accepted Submission(s): 49 Problem Description Who is Dylans?You can find his ID in UOJ and Codeforces. His another

AC日记——Dylans loves tree hdu 5274

Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1611    Accepted Submission(s): 388 Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i] .Node

HDU 5273 Dylans loves numbers(水题)

题意:给出一个0≤N≤1018,求其二进制中有几处是具有1的,假设相连的1只算1处,比如1101011就是3处. 思路:一个个数,当遇到第一个1时就将flag置为1:当遇到0就将flag置为0.当遇到1时,flag=0就统计,当flag=1时就不统计. 1 #include <bits/stdc++.h> 2 #define LL long long 3 using namespace std; 4 5 int main() 6 { 7 int t; 8 LL n; 9 cin>>

Dylans loves numbers

Problem Description Who is Dylans?You can find his ID in UOJ and Codeforces. His another ID is s1451900 in BestCoder.And now today's problems are all about him.Dylans is given a number N. He wants to find out how many groups of "1" in its Binary

BestCoder #45 1003 Dylans loves tree

problem 题意 给定一棵树,并给定在这棵树上的两种操作.一种操作是改变一个节点的权值,另外一个操作是对两个节点之间的路径上的权值进行统计,如果每个权值出现的次数都是偶数,输出-1,否则输出出现次数为奇数的权值(保证只有一个) 思路 这题是一个DFS序的模板题.首先想到,我们获得这棵树的DFS序,对于这个序列,我们可以去维护区间的异或和.由于是单点修改区间查询,可以用树状数组也可以直接写线段树.然后对于每个询问,我们查询出每个点到根的异或和(这里直接用DFS序中到根节点的异或和即可,因为如果

hdu 5273 Dylans loves sequence 逆序数简单递推

Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5273 Description Dylans得到了N个数a[1]...a[N].有Q个问题,每个问题形如(L,R)他需要求出L−R这些数中的逆序对个数.更加正式地,他需要求出二元组(x,y)的个数,使得L≤x,y≤R且x<y且a[x]>a[y] Input 第一行有两个数N和Q

HDU 5273 Dylans loves sequence(区间DP)

Dylans loves sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 114    Accepted Submission(s): 59 Problem Description Dylans is given N numbers a[1]....a[N] And there are Q questions. E

【BC#24 1002 HDOJ 5273】Dylans loves sequence

[BC#24 1002 HDOJ 5273]Dylans loves sequence 逆序对动归 比赛时候各种奇葩姿势都上了个遍 归并也憋出来了 谁知道就给我看这个.... 赛后有的思路 收到赛后题解的启发 dp[l][r]是l~r之间逆序对 先暴力把dp[1][1~n]枚举出来 然后i从2~n枚举左边界 右边界从i+1~n 这样dp[i][j] 即求出了区间左端点从2往后的所有逆序对数 而dp[i][j]即为dp[i-1][j]中吧i-1的逆序对数减去的余数 这样顺序暴力即可......还是