CodeForces - 121A - Lucky Sum

本文介绍了一个算法问题:使用仅由4和7构成的数字替换给定区间内的每个整数,并求替换后的总和。通过预先计算所有可能的4和7构成数并进行排序,可以有效地解决该问题。

题意:

对于每个数a,用一个由4或者7组成,且大于等于a的数替代它,要最接近的,比如8,那就用44替代,不能用47。给定一个区间,求这些区间的数被替代完之后的总和。

思路:

看到题目就想到打表,枚举一下所有与4和7有关的数字。其实我们可以把所求区间切成一段一段,每一段都会是有同一个替代数,这个替代数乘以片段长度就是这段数字的总和。

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
long long bi[5000],sum=0;
int z=0;
void da(long long co){
		if(co>1000000000) return;
		long long te=co*10+4;
		bi[z++]=te;
		da(te);
		te=co*10+7;
		bi[z++]=te;
		da(te);		
} 

int main(){
	da(0);
	sort(bi,bi+z);
	int st,en,z1=0;
	int s1,s2;
	scanf("%d %d",&st,&en);
	while(bi[z1]<st) z1++;
	s1=z1;
	while(bi[z1]<en) z1++;
	s2=z1;
	if(s1==s2) {sum=bi[s1]*(en-st+1);} 
	else {
			int i;
			sum+=bi[s1]*(bi[s1]-st+1);
			for(i=s1+1;i<=s2;i++){
					sum+=(bi[i]-bi[i-1])*bi[i];
			}
			sum-=bi[s2]*(bi[s2]-en);
		}
		
	printf("%I64d\n",sum);
	
	return 0;
}


### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Blaze Jack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值