Codeforces Round #739 (Div. 3)

Codeforces Round #739 (Div. 3)

A. Dislike of Threes

题目大意:输出第 k k k个不能被 3 3 3整除且个位数不是 3 3 3的数。

分析:暴力枚举,从 1 1 1开始,判断每个数 x x x是否 x % 3 ≠ 0 且 x % 10 ≠ 3 x\%3\neq 0 且 x\%10\neq 3 x%3=0x%10=3

代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

void read(int &sum)
{
	sum=0;char last='w',ch=getchar();
	while (ch<'0' || ch>'9') last=ch,ch=getchar();
	while (ch>='0' && ch<='9') sum=sum*10+ch-'0',ch=getchar();
	if (last=='-') sum=-sum;
}
int main()
{
//	freopen("M.in","r",stdin);
//	freopen("M.out","w",stdout);
	int len=0,a[10000],i=1;
	while (len<1000)
	{
		if (i%3!=0 && i%10!=3) a[++len]=i;
		i++; 
	}
	int t;
	read(t);
	while (t--)
	{
		int x;read(x);
		printf("%d\n",a[x]);
	}
//	fclose(stdin);fclose(stdout);
	return 0;
}

B. Who’s Opposite?

题目大意:有 n ( 2 ∣ n ) n(2|n) n(2n)个人围成一个圈,每个人编号分别为 1 , 2 , 3... n 1,2,3...n 1,2,3...n,第 x x x个人正对面的人是第 y y y个人,给出 x , y x,y x,y,问第 z z z个人所对的人的编号是多少,无解输出 − 1 -1 1

分析:

  1. 观察两个人何时能相对,设 s 1 s_1 s1 x , y x,y x,y左边间隔的人,设 s 2 s_2 s2 x , y x,y x,y右边间隔的人,发现当且仅当 s 1 = s 2 s_1=s_2 s1=s2时, x , y x,y x,y是相当的
  2. 所以令 a = m i n ( x , y ) , b = m a x ( x , y ) a=min(x,y),b=max(x,y) a=min(x,y),b=max(x,y),那么 s 2 = y − x − 1 s_2=y-x-1 s2=yx1 s 1 = s 2 s_1=s_2 s1=s2,但明显 s 1 s_1 s1至少为 1 1 1~ x − 1 x-1 x1的人数,所以 s 1 ⩾ x − 1 s_1\geqslant x-1 s1x1,如果 s 2 < x − 1 s_2<x-1 s2<x1那么就是无解
  3. 通过 s 1 s_1 s1, s 2 s_2 s2,容易求出 n = s 1 + s 2 + 2 n=s_1+s_2+2 n=s1+s2+2,如果 z > n z>n z>n也是无解
  4. 最后根据 s 2 s_2 s2可以得出于 z z z相对的人的位置在 ( z + s 2 ) (z+s_2) (z+s2)的位置上, % \% %一下就好了

代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

void read(int &sum)
{
	sum=0;char last='w',ch=getchar();
	while (ch<'0' || ch>'9') last=ch,ch=getchar();
	while (ch>='0' && ch<='9') sum=sum*10+ch-'0',ch=getchar();
	if (last=='-') sum=-sum;
}
int t;
int a,b,c; 
int main()
{
//	freopen("M.in","r",stdin);
//	freopen("M.out","w",stdout);
	read(t);
	while (t--)
	{
		read(a),read(b),read(c);
		if (a>b) swap(a,b);
		if (a-1>b-a-1) { printf("-1\n"); continue; }
		int n=b+((b-a-1)-(a-1));
		if (c>n) { printf("-1\n"); continue; }
		if ((b+c-a+n*4)%n==0) printf("%d\n",n);
		else printf("%d\n",(b+c-a+n*4)%n);
		
	}
//	fclose(stdin);fclose(stdout);
	return 0;
}

C. Infinity Table

题目大意:按图的方式填格子,问 x x x在那个格子?

在这里插入图片描述

分析:

  1. 发现填充的顺序是在这里插入图片描述的一个顺序
  2. 所以把其分成 n n n段向下,和向左移动的子段,发现 ( n , 1 ) = k 2 (n,1)=k^2 (n,1)=k2,所以可以知道 ( x ) 2 = ( x 的 上 一 个 平 方 数 ) (\sqrt x)^2=(x的上一个平方数) (x )2=x,于是就得到了 x x x是从 ( x , 1 ) (\sqrt x,1) (x ,1)开始往下的 x − x 2 x-\sqrt x ^2 xx 2的数所填充的位置

代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

void read(int &sum)
{
	sum=0;char last='w',ch=getchar();
	while (ch<'0' || ch>'9') last=ch,ch=getchar();
	while (ch>='0' && ch<='9') sum=sum*10+ch-'0',ch=getchar();
	if (last=='-') sum=-sum;
}
int t,n;
int main()
{
//	freopen("M.in","r",stdin);
//	freopen("M.out","w",stdout);
	read(t);
	while (t--)
	{
		read(n);
		int k=sqrt(n)+1;
		if (n==(k-1)*(k-1)) k--;
		if (n-(k-1)*(k-1)<k) printf("%d %d\n",n-(k-1)*(k-1),k);
		else printf("%d %d\n",k,k-(n-(k-1)*(k-1)-k));
	}
//	fclose(stdin);fclose(stdout);
	return 0;
}

D. Make a Power of Two

题目大意:给出 x x x,每次可以删除 x x x的任意一位,或在 x x x末尾添一个数字,问使得 x = 2 k ( k ⩾ 0 ) x=2^k(k\geqslant 0) x=2k(k0)的最少次数

分析:

  1. 考虑极端情况, x x x 9 9 9位,删 9 9 9位在加上 1 1 1 2 2 2,那 x x x必然合法,所以最多操作 10 10 10次,所以 x ⩽ 1 0 19 x\leqslant 10^{19} x1019,所以枚举 k = 1 到 63 k=1到63 k=163,选取最小操作方案
  2. 这样,问题就变为了给两个数 a , b a,b a,b,问是的 a a a,变成 b b b的最小操作次数,举个例子在这里插入图片描述 1052 1052 1052变成 1024 1024 1024,因为我们是可以在末尾添加,而不能在首位添加,所以要使得高位相等的代价要高于使低位相等的代价,所以就要尽量使高位相等,从高位开始匹配即可,余下的位数要剪掉

代码:

在这里插入代码片#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define int long long
using namespace std;
typedef long long ll;

void read(int &sum)
{
	sum=0;char last='w',ch=getchar();
	while (ch<'0' || ch>'9') last=ch,ch=getchar();
	while (ch>='0' && ch<='9') sum=sum*10+ch-'0',ch=getchar();
	if (last=='-') sum=-sum;
}
int t;
string n;
int sortm(int a,int b)
{
	if (b==0) return 1;
	if (b==1) return a;
	int t=sortm(a,b/2);
	if (b%2==1) return t*t*b;
	else return t*t;
}
int solve(string a,int m)
{
	string b;char f[100];int len=0;
	while (m!=0)
	{
		char ch=m%10+'0';m/=10;
		f[len]=ch;len++;
	}
	for (int i=len-1;i>=0;i--)
		b+=f[i];
//	cout << b  << " " << a << endl;
	int l=0,r=0;
	int sum=0;
//	printf("l,r : %d %d\n",l,r);
	while (l<a.length() && r<b.length())
	{
//		printf("%c %c\n",a[l],b[r]);
		if (a[l]==b[r]) l++,r++;
		else l++,sum++;
	}
//	cout << a << " " << b << endl;
	if (l!=a.length()) sum+=a.length()-1-l+1;
	if (r!=b.length()) sum+=b.length()-1-r+1;
	return sum;
}
signed main()
{
//	freopen("M.in","r",stdin);
//	freopen("M.out","w",stdout);
	read(t);
	while (t--)
	{
		cin >> n;
		int k=1,ans=1<<30;
		for (int i=0;i<=60;i++)
		{
			ans=min(ans,solve(n,k));
//			printf("%lld %lld\n",solve(n,k),k);
			k*=2;
		}
		printf("%lld\n",ans);
	}
//	fclose(stdin);fclose(stdout);
	return 0;
}

E. Polycarp and String Transformation

题目大意:原来有一串字符 s s s,每次删除其中一种字母,直到为空串,设每次删除后的字符串为 s 1 , s 2 . . . s k s_1,s_2...s_k s1,s2...sk,则字符串 t = s + s 1 + s 2 + . . . + s k t=s+s_1+s_2+...+s_k t=s+s1+s2+...+sk,现在给出 t t t,问原字符串 s s s是啥,以及字母删除的顺序。

分析:

  1. 首先可以发现,如果在一个位置 m i d mid mid上, 1 1 1 ~ m i d mid mid中字符 G G G出现了,但在 m i d + 1 mid+1 mid+1 ~ t . l e n g t h ( ) t.length() t.length()中没有出现,则可以断定 G G G被删除了,从开头枚举到结尾,就可以得到删除的顺序了
  2. 那么如何求出 s s s呢?观察 t t t的构成, t = s + s 1 + s 2 + . . . + s k t=s+s_1+s_2+...+s_k t=s+s1+s2+...+sk,那么根据每个字母删除的顺序,我们可以得到,在 t t t中,每个字母出现了 S 在 s 中 出 现 的 次 数 ∗ T 第 几 个 删 除 的 S_{在s中出现的次数}*T_{第几个删除的} SsT,因为 s s s t t t的开头,枚举结尾在哪,利用前缀和判断是否合法即可

代码:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

int bz[26];
int* cnt=bz-'a';
pair<string,string> dep(string s)
{
	string ch;
	reverse(s.begin(), s.end());
	for (auto c:s)
	{
		if (!cnt[c])
			ch.push_back(c);
		cnt[c]++;
	}
	int m = ch.length();
	int yyds = 0;
	for (int i = 0; i < m; i++)
		yyds += cnt[ch[i]] / (m - i);

	reverse(ch.begin(), ch.end());
	return { string(s.rbegin(), s.rbegin() + yyds), ch };
}
string solve(pair<string, string> p)
{
	string result = p.first;

	for (auto c : p.second)
	{
		string temp;
		for (auto d : p.first)
			if (d != c)
			{
				temp.push_back(d);
				result.push_back(d);
			}
		p.first = temp;
	}

	return result;
}
int t;
int main()
{
	cin >> t;
	while (t--)
	{
		memset(bz,0,sizeof(bz));
		string s;
		cin >> s;
		auto ans=dep(s);
		auto check=solve(ans);
		if (check==s)
			cout << ans.first << ' ' << ans.second << '\n';
		else
			cout << "-1\n";
	}

	return 0;
}

F. Nearest Beautiful Number

题目大意:给出 n n n,求大于等于 n n n且最多有 k k k个不同数字的最小数

分析:

  1. 从高位开始考虑,设当为第 i i i位,值为 x x x,记录答案数字第 i i i位的数组为 a a a,分两种情况
  1. 如何 a a a不足 k k k位,直接 a [ i ] = x a[i]=x a[i]=x
  2. 如果 a a a已经有 k k k位了,两种情况
  1. 如果当位的值 x x x已经在 a a a中出现过了,直接填 a [ i ] = x a[i]=x a[i]=x即可
  2. 如果当前位的值 x x x没有在 a a a中出现,也分两种情况
  1. 如果前面的值是大于 n n n的前 i − 1 i-1 i1位的,那么就可以填一个前面最小的数
  2. 如果前面的值是等于 n n n的前 i − 1 i-1 i1位的,分类
  1. 尝试修改从前面已经出现过的数字中,选一个 ⩾ x \geqslant x x且最小的数
  2. 在第一种失败的情况下,就考虑把 a [ 1 ] a[1] a[1]$a[i-1]$中的某个数字变大,然后这一位在填$a[1]$ a [ i − 1 ] a[i-1] a[i1]中最小的数

代码

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

string solve()
{
	string n;
	int k;
	cin >> n >> k;
	while (true)
	{
		set<char> s;
		for (auto c : n) s.insert(c);
		if (s.size() <= k) return n;
		s.clear();
		int ptr = 0;
		while (ptr++)
		{
			s.insert(n[ptr]);
			if (s.size()>k)
			{
				while (n[ptr]=='9') ptr--;
				n[ptr]++;
				for (int i=ptr+1;i<n.size();i++)
					n[i]='0';
				break;
			}
		}
	}
}
int t;
int main()
{
	cin >> t;
	while (t--)
		cout << solve() << '\n';
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值