Foj 1559 Count Zeros

本文探讨了如何计算从1到2^n范围内所有正整数的二进制表示中0的数量,并提供了一种高效的计算方法。通过递推公式简化计算过程,避免了直接转换每个数字为二进制形式的复杂度。
部署运行你感兴趣的模型镜像

Problem 1559 Count Zeros

Accept: 143Submit: 416
Time Limit: 1000 mSecMemory Limit : 32768 KB

Problem Description

As is known to us all, any positive decimal integer can be expressed into a unique binary digit. For example the decimal number 13 can be expressed in the form of binary digit as 1101, your task is to count the total number of 0s in the binary digit of 1..2n. For example, if n = 2, then 2n = 4. Binary digits of 1,2,3,4 are 1,10,11,100. So the number of 0s is 3.

Input

There are multiple test cases. Each test case contains a non-negative integer n (n<=5000)

Output

For each test case, print the number of 0s which the binary digits of 1..2n contains. The result may be vary large, so output it mod 2007.

Sample Input

2
3

Sample Output

3
8
推公式,
t[n]=3*t[n-1]-2*t[n-2]+2^(n-2)-1
汗死,
把t[1]写成0了, wa了无数次.......
orz...
orz...
orz...
#include<stdio.h> int t[5001]; void init() { int k=4,tmp; t[1]=1;t[2]=3;t[3]=8; for(int i=4;i<=5000;i++) { tmp=3*t[i-1]-2*t[i-2]+k-1; if(tmp<=0) tmp=3*(t[i-1]+2007)-2*t[i-2]+k-1; t[i]=tmp%2007; k*=2; k%=2007; } } int main() { int n; init(); while(scanf("%d",&n)!=EOF) printf("%d/n",t[n]); return 0; }
这是暴力的Java代码
import java.math.BigInteger; import java.util.*; public class Main { static BigInteger[] big = new BigInteger[5001]; public static void main(String[] args) { init(); Scanner in = new Scanner(System.in); int n; while(in.hasNext()) { n=in.nextInt(); System.out.println(big[n].mod(new BigInteger("2007"))); } } public static void init() { BigInteger tmp1,tmp2; big[0]=new BigInteger("0"); big[1]=new BigInteger("1"); big[2]=new BigInteger("3"); big[3]=new BigInteger("8"); int k=4; for(int i=4;i<=5000;i++) { tmp1=big[i-1].multiply(new BigInteger("3")); tmp2=big[i-2].multiply(new BigInteger("2")); tmp1=tmp1.subtract(tmp2); tmp1=tmp1.add(new BigInteger(k+"")); big[i]=tmp1.subtract(new BigInteger("1")); k*=2; k%=2007; } } }

您可能感兴趣的与本文相关的镜像

PyTorch 2.6

PyTorch 2.6

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值