时间限制 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(T≤100)
, indicating the number of test cases.
The following T lines, each with an integer
x(1≤x≤2×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);
}
}
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(T≤100)
, indicating the number of test cases.
The following T lines, each with an integer
x(1≤x≤2×109)
, give the number that you are to calculate.
输出格式
Output Nass(x) for each given x.
输入样例
3
1
9
11
输出样例
0
8
0