LightOJ1032(数位DP)

这个还是非常好统计的。。统计好首位为1的数的个数就没啥了。。

不过这个oj的数据还真是良心。。基本过了样例就可以A了。。。




#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define eps 1e-8
#define inf 1000000007
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define ls T[i<<1]
#define rs T[i<<1|1]
#define op T[i]
#define mid (x+y>>1)
#define NM 35
#define nm 10005
#define pi 3.141592653
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}


int n,b[NM],c[NM][2];
ll d[NM];

ll dfs(int i,bool f){
	if(!i)return 0;
	if(!f&&d[i])return d[i];
	int m=f?b[i]:1;ll ans=0;
	ans=dfs(i-1,f&&m==0);
	c[i][0]=c[i-1][0]+c[i-1][1];
	if(m)ans+=dfs(i-1,f),ans+=c[i-1][1],c[i][1]=c[i-1][0]+c[i-1][1];else c[i][1]=0;
	return d[i]=ans;
}

ll solve(int x){
	n=0;mem(d);mem(b);
	for(int t=x;t;t>>=1)b[++n]=t%2;
	return dfs(n,1);
}

int main(){
	//freopen("data.in","r",stdin);
	c[0][0]=1;
	int _=read();
	inc(T,1,_)printf("Case %d: %lld\n",T,solve(read()));
	return 0;
}






1032 - Fast Bit Calculations
Time Limit: 2 second(s)Memory Limit: 32 MB

A bit is a binary digit, taking a logical value of either 1or 0 (also referred to as "true" or "false"respectively). And every decimal number has a binary representation which isactually a series of bits. If a bit of a number is 1 and its next bit isalso 1 then we can say that the number has a 1 adjacent bit. Andyou have to find out how many times this scenario occurs for all numbers up to N.

Examples:

      Number        Binary          Adjacent Bits

        12                    1100                        1

        15                    1111                        3

        27                    11011                      2

Input

Input starts with an integer T (≤ 10000),denoting the number of test cases.

Each case contains an integer N (0 ≤ N < 231).

Output

For each test case, print the case number and the summationof all adjacent bits from 0 to N.

Sample Input

Output for Sample Input

7

0

6

15

20

21

22

2147483647

Case 1: 0

Case 2: 2

Case 3: 12

Case 4: 13

Case 5: 13

Case 6: 14

Case 7: 16106127360

 


Problem Setter: Mohiul Alam Prince

Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值