D. Nass Number 第八届北京邮电大学程序设计竞赛 - 热身赛 (2)

题目描述

We define two functions under base 10. f(x) indicates the number which is formed by operating bitwise exclusive or(xor) with each digit of x, and g(x) indicates the sum of all these digits. For instance, f(47019)=4 xor 7 xor 0 xor 1 xor 9=11, g(47019)=4+7+0+1+9=21. The Nass Number of x is defined to be an integer y, which satisfies the conditions below:
1. 0≤y<x;
2. f(y)≤f(x);
3. Define Set S={y0,y1,…,yk}, where yi satisfies the two conditions above. The Nass Number y∈S, meanwhile g(y)≥g(yi) for i from 0 to k. If there exists another y′ so that g(y′)=g(y), it should be satisfied that y>y′.
Given x, you're required to answer Nass(x).

输入格式

The first line contains an integer  T(T100) , indicating the number of test cases.
The following T lines, each with an integer  x(1x2×109) , give the number that you are to calculate.

输出格式

Output Nass(x) for each given x. 

输入样例

3
1
9
11

输出样例

0
8
0
用例:

47019

399


数位dp

要求满足条件的最大的数,从9到0枚举,保证数最大。

然后还加了一个剪枝。。



#include<stdio.h>  
#include<string.h>  
#include<math.h>  
#include<stdlib.h>  
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 1010

int ffx,ggx;
int n,ans;
int bit[20];

void dfs(int pos,int fx,int gx,int x,int f)
{
	if(ggx!=-1 && gx+(pos+1)*9<=ggx) return ;//一个剪枝,不然就T死了。。
	if(pos==-1)
	{
		if(x<n && fx<=ffx)
		{
			if(ggx==-1)
			{
				ans=x,ggx=gx;
			}
			else if(gx>ggx){
				ans=x;
				ggx=gx;
			}
		}
		return ;
	}
	int end=f?bit[pos]:9;
	int i;
	for(i=end;i>=0;i--)
		dfs(pos-1,fx^i,gx+i,x*10+i,f&&(i==end));
}
void calc(int x)
{
	int i,j,cnt=0;
	while(x){
		bit[cnt++]=x%10;
		x/=10;
	}
	ffx=bit[0];
	for(i=1;i<cnt;i++)
		ffx^=bit[i];
	for(i=bit[cnt-1];i>=0;i--){
		dfs(cnt-2,i,i,i,i==bit[cnt-1]);
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int i,j,k;
		scanf("%d",&n);
		ans=0;
		ggx=-1;
		calc(n);
		printf("%d\n",ans);
	}
}


时间限制 1000 ms  内存限制 65536 KB

题目描述

We define two functions under base 10. f(x) indicates the number which is formed by operating bitwise exclusive or(xor) with each digit of x, and g(x) indicates the sum of all these digits. For instance, f(47019)=4 xor 7 xor 0 xor 1 xor 9=11, g(47019)=4+7+0+1+9=21. The Nass Number of x is defined to be an integer y, which satisfies the conditions below:
1. 0≤y<x;
2. f(y)≤f(x);
3. Define Set S={y0,y1,…,yk}, where yi satisfies the two conditions above. The Nass Number y∈S, meanwhile g(y)≥g(yi) for i from 0 to k. If there exists another y′ so that g(y′)=g(y), it should be satisfied that y>y′.
Given x, you're required to answer Nass(x).

输入格式

The first line contains an integer  T(T100) , indicating the number of test cases.
The following T lines, each with an integer  x(1x2×109) , give the number that you are to calculate.

输出格式

Output Nass(x) for each given x. 

输入样例

3
1
9
11

输出样例

0
8
0
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值