CodeForces 215E Periodical Numbers 数位DP

本文介绍了一种算法,用于计算指定区间内满足特定二进制模式的整数数量。通过动态规划和去除重复计算的方法,该算法能够高效地解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给你一个区间[l,r],求这个区间内满足条件的数,条件是:这个数的二进制表示时,dig[i] == dig[i+k],(0<k<len,且len%k==0,len为这个数的二进制代码长度)

路:考虑[0,x]这个区间,若x的位数为len,当数的长度 i 为0~len-1时,则是无限制的,这时dp[i] = sum{2^(k-1)},k为满足条件的循环长度。而且还要去掉重复的,比如当长度为6时,循环长度为2,3的数均会重复计算,当数的长度为len时,则在限制下,计算满足条件的数,具体实现看代码注释:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdlib>
#include <algorithm>
using namespace std;
typedef __int64 int64;
typedef long long ll;
#define M 600005
#define N 1000005
#define max_inf 0x7f7f7f7f
#define min_inf 0x80808080
#define mod 1000000007
#define lc rt<<1
#define rc rt<<1|1

ll dp[70] , r , l , table[70];//table[i] = 2^i;
int dig[70];

ll Cal(int k)//计算长度为k的数,满足条件的个数
{
	int i , j;
	ll ret = 0;
	for (i = 1 ; i < k ; i++)
	{
		if (k%i)continue;
		dp[i] = table[i-1];
		for (j = 1 ; j < i ; j++)//减掉重复计算的数
		{
			if (i%j == 0)
				dp[i] -= dp[j];
		}
		ret += dp[i];
	}
	return ret;
}

ll Solve(ll k)
{
	int i , j , len = 0;
	ll ret = 0 , temp = k , num;
	while (temp)
	{
		dig[++len] = temp&1;
		temp >>= 1;
	}

	//计算无限制时满足条件的数
	for (i = 1 ; i < len ; i++)ret += Cal(i);

	for (i = 1 ; i < len ; i++)//长度为len时,枚举循环长度
	{
		if (len%i)continue;

		num = 1;
		temp = 0;
		dp[i] = 0;
		for (j = len-1 ; j > len-i ; j--)
		{
			//若dig[j]==1则可以令dig[j]=0,转变成无限制的情况
			if (dig[j])dp[i] += table[i-(len-j)-1];
			num = num*2+dig[j];
		}


		temp = num;
		int up = len/i;
		for (j = 1 ; j < up ; j++)num = (num<<i)+temp;
		//num保存的为循环长度为i,且循环内每一位都受限制的情况下的这个数
		dp[i] += (num <= k);//若num比k小,则加入答案中

		//去掉重复的计算的数
		for (j = 1 ; j < i ; j++)
		{
			if (i%j == 0)
				dp[i] -= dp[j];
		}

		ret += dp[i];
	}
	return ret;
}

int main()
{
	int i;
	for (table[0] = 1 , i = 1 ; i < 70 ; i++)table[i] = table[i-1]*2;
	while (~scanf("%I64d%I64d",&l,&r))
		printf("%I64d\n",Solve(r)-Solve(l-1));
	return 0;
}


### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值