Codeforces Round 823 (Div. 2)C

Posted 有时候方向比努力更重要!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round 823 (Div. 2)C相关的知识,希望对你有一定的参考价值。

C. Minimum Notation

思路:我们可以进行的操作时将一个位置的数删除然后在任意位置处添加一个比当前数大1并且小于9的数,所以我们的操作只会让一个数变大,我们统计一个最大值的后缀,贪心的考虑如果当前数的后面有比他小的数的话,我们就需要让这个小的数往前走才能使字典序变小,如果当前值小于<= 后缀的最小值,说明这个数后面的所有数往前移动都不能使字典序变得更小,所以我们统计完后缀后,从前往后扫一遍,用multiset维护以下,即可。

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
char c[N], suf[N];
void run()

	scanf("%s", c + 1);
	int mn = 10;
	int n = strlen(c + 1);
	for(int i = n; i >= 1; -- i)
	
		int num = c[i] - \'0\';
		if(num < mn)	mn = num;
		suf[i] = mn;
	 
	multiset<int>s;
	for(int i = 1; i <= n; ++ i)
	
		int num = c[i] - \'0\';
		if(num <= suf[i])	s.insert(num);
		else
		
			if(num == 9)	s.insert(9);
			else	s.insert(min(num + 1, 9));
		
	
	for(auto x : s)	cout << x;
	printf("\\n");


int main()

//	freopen("1.in", "r", stdin);
	int t;cin >> t;
	while(t --)	run();	
	return 0;  

Codeforces Round #774 (Div. 2)

思维,暴力dfs

比赛链接

Codeforces Round #774 (Div. 2)

C. Factorials and Powers of Two

A number is called powerful if it is a power of two or a factorial. In other words, the number \\(m\\) is powerful if there exists a non-negative integer \\(d\\) such that \\(m=2^d\\) or \\(m=d !\\), where \\(d !=1 \\cdot 2 \\cdot \\ldots \\cdot d\\) (in particular, 0 ! \\(=1\\) ). For example 1,4 , and 6 are powerful numbers, because \\(1=1 !, 4=2^2\\), and \\(6=3 !\\) but 7,10 , or 18 are not.

You are given a positive integer \\(n\\). Find the minimum number \\(k\\) such that \\(n\\) can be represented as the sum of \\(k\\) distinct powerful numbers, or say that there is no such \\(k\\).

Input

Each test contains multiple test cases. The first line contains the number of test cases \\(t(1 \\leq t \\leq 100)\\). Description of the test cases follows.
A test case consists of only one line, containing one integer \\(n\\left(1 \\leq n \\leq 10^12\\right)\\)

Output

For each test case print the answer on a separate line.
If \\(n\\) can not be represented as the sum of distinct powerful numbers, print \\(-1\\).
Otherwise, print a single positive integer - the minimum possible value of \\(k\\).

Example

input

4
7
11
240
17179869184

output

2
3
4
1

Note

In the first test case, 7 can be represented as \\(7=1+6\\), where 1 and 6 are powerful numbers. Because 7 is not a powerful number, we know that the minimum possible value of \\(k\\) in this case is \\(k=2\\).
In the second test case, a possible way to represent 11 as the sum of three powerful numbers is \\(11=1+4+6\\). We can show that there is no way to represent 11 as the sum of two or less powerful numbers.

In the third test case, 240 can be represented as \\(240=24+32+64+120\\). Observe that \\(240=120+120\\) is not a valid representation, because the powerful numbers have to be distinct.

In the fourth test case, \\(17179869184=2^34\\), so 17179869184 is a powerful number and the minimum \\(k\\) in this case is \\(k=1\\).

解题思路

思维,dfs

可以发现不可能出现无解的情况(由二进制表示,一个数总是可以表示为若干个不相同的二次幂相加的形式),可以将所有小于等于 \\(1e12\\) 的乘方表示出来,共有 \\(16383\\) 个数,然后每次暴力每次选择某一个乘方数或不选,最后再用若干个二次幂来表示剩余的数,注意当乘方数中包含 \\(1\\)\\(2\\) 时,剩余的数的二次幂不能出现 \\(1\\)\\(2\\),否则两个数会重复

  • 时间复杂度:\\(O(16383\\times T)\\)

代码

// Problem: C. Factorials and Powers of Two
// Contest: Codeforces - Codeforces Round #774 (Div. 2)
// URL: https://codeforces.com/contest/1646/problem/C
// Memory Limit: 256 MB
// Time Limit: 3000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
// #define int long long
#define help cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y)  return (y > x) ? x = y, 1 : 0; 
template <typename T> bool chkMin(T &x, T y)  return (y < x) ? x = y, 1 : 0; 
 
template <typename T> void inline read(T &x) 
    int f = 1; x = 0; char s = getchar();
    while (s < \'0\' || s > \'9\')  if (s == \'-\') f = -1; s = getchar(); 
    while (s <= \'9\' && s >= \'0\') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;



int t,tot;
LL n,a[60];
vector<int> chosen;
unordered_map<LL,int> f;
unordered_set<LL> s1,s2;
void dfs(int x)

	if(x==tot+1)
	
		LL t=0;
		bool fl[2]=0;
		for(int i:chosen)
		
			if(a[i]==1)fl[0]=1;
			if(a[i]==2)fl[1]=1;
			t+=a[i];
		
		if(t>1e12||t==0)return ;
		if(fl[0])s1.insert(t);
		if(fl[1])s2.insert(t);
		f[t]=chosen.size();
		return ;
	
	chosen.pb(x);
	dfs(x+1);
	chosen.pop_back();
	dfs(x+1);

int main()

    LL b=1;
    for(int i=1;;i++)
    
    	b*=i;
    	if(b>1e12)break;
    	a[++tot]=b;
    
    dfs(1);
    for(cin>>t;t;t--)
    
    	cin>>n;
    	int res=__builtin_popcountll(n);
    	for(auto t:f)
    	
    		LL x=t.fi;
    		int y=t.se;
    		if(x>n)continue;
    		LL z=n-x;
    		if(s1.count(x)&&(z&1))continue;
    		if(s2.count(x)&&(z>>1&1))continue;
    		res=min(res,y+__builtin_popcountll(z));
    	
    	cout<<res<<\'\\n\';
    
    return 0;

以上是关于Codeforces Round 823 (Div. 2)C的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #705 (Div. 2)

Codeforces Round #774 (Div. 2)

Codeforces Round #808 (Div. 1)(A~C)

Codeforces Round #717 (Div. 2)

Codeforces Round #784 (Div. 4)

Codeforces Round #340 (Div. 2) B