codeforces#701(思维)

Add and Divide

题意:
输入两个正整数 a,b。
两个操作:a=a/b,b=b+1。
求使a=0的最小操作次数

主要思路:
如果a==b 输出2
如果a<b 输出1
否则 暴力枚举(b=[2->b+1000]的所有操作数找最小值

#include<iostream>
using namespace std;
long long slove(long long a,long long b)
{
	long long sum=0;
	while(a>0)
	{
		a/=b;
		sum++;
	}
	return sum;
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		long long a,b;
		cin>>a>>b;
		long long ans=9999999999999;
		if(a==b)
			cout<<"2"<<endl;
		else if(a<b)
			cout<<"1"<<endl;
		else
		{
			for(int i=b;i<=b+10000;i++)
			{
				if(i==1) continue;
				long long sum=slove(a,i);
				ans=min(ans,sum+i-b);
			}	
			cout<<ans<<endl;
		}
	}
	return 0;
} 

Replace and Keep Sorted

题意:
输入n(数组长度),q(询问次数),k(最大数)
输入q行l,r
计算有多少个与a[l]~a[r]相似的数组

主要思路:
①左边界
②右边界
③开区间内(利用前缀和)
第i个数受a[i-1],a[i+1]限制

#include<iostream>
using namespace std;
int main()
{
	long long n,q,k;
	cin>>n>>q>>k;
	int a[100001],sum[100001];
	for(int i=1;i<=n;i++)
		cin>>a[i];
	for(int i=1;i<=n;i++)
		sum[i]=sum[i-1]+(a[i+1]-a[i-1]-2);//处理区间内的情况,2即减去a[i],a[i+1] 
	while(q--)
	{
		long long l,r;
		cin>>l>>r;
		if(l==r)
		{
			cout<<k-1<<endl;
			continue;
		}
		long long ans=0;
		if(r-l>1)
			ans+=sum[r-1]-sum[l];//(l,r)
		ans+=(k-a[r-1]-1);//右边界的变化 
		ans+=(a[l+1]-2);//左边界 
		cout<<ans<<endl;
	}
	return 0;
}

Floor and Mod

题意:
输入x,y。有1<=a<=x,1<=b<=y.
a/b==a%b;
求满足的a,b情况

主要思路:
利用公式:x=bk+k推导
min(y,x/i-1)-i
b=x/i-1,k在[1,x/k]中
减i是因为当k==i时,前b在[1,i]中不满足条件

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int x,y;
		cin>>x>>y;
		long long ans=0;
		for(int i=1;i<=x/i;i++)//k的循环
			ans+=max(min(y,x/i-1)-i,0);//b的范围限制(考虑y,x)
		cout<<ans<<endl;
	}
	return 0;
}

Multiples and Power Differences

题意:
输入n,m矩阵a(1<=元素<=16),输出一个矩阵b保证bij是aij的倍数,且相邻的数的差是某一元素的四次方

主要思路:
找到1~16的最小公倍数720720

#include<iostream>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	int x;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>x;
			if((i+j)%2==1)
				cout<<720720<<" ";
			else
				cout<<720720+x*x*x*x<<' ';
		} 
		cout<<endl;
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值