hdu-5920 Ugly Problem(贪心)

题目链接:

Ugly Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 363    Accepted Submission(s): 134
Special Judge

Problem Description

Everyone hates ugly problems.

You are given a positive integer. You must represent that number by sum of palindromic numbers.

A palindromic number is a positive integer such that if you write out that integer as a string in decimal without leading zeros, the string is an palindrome. For example, 1 is a palindromic number and 10 is not.

Input

In the first line of input, there is an integer T denoting the number of test cases.

For each test case, there is only one line describing the given integer s (1≤s≤101000).

Output

For each test case, output “Case #x:” on the first line where x is the number of that test case starting from 1. Then output the number of palindromic numbers you used, n, on one line. n must be no more than 50. en output n lines, each containing one of your palindromic numbers. Their sum must be exactly s.

Sample Input

2

18

1000000000000

Sample Output

Case #1:

2

9

9

Case #2:

2

999999999999

1

题意:

给一个大数,然后让你找到不超过50个回文数的和为这个数;

思路:

每次找前边取一半减一,然后再复制另一半,这样很快就好了,手写了了一个大数减法,写成了智障了都;

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map>

using namespace std;

#define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));

typedef  long long LL;
typedef unsigned long long ULL;
template<class T> void read(T&num) {
    char CH; bool F=false;
    for(CH=getchar();CH<‘0‘||CH>‘9‘;F= CH==‘-‘,CH=getchar());
    for(num=0;CH>=‘0‘&&CH<=‘9‘;num=num*10+CH-‘0‘,CH=getchar());
    F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
    if(!p) { puts("0"); return; }
    while(p) stk[++ tp] = p%10, p/=10;
    while(tp) putchar(stk[tp--] + ‘0‘);
    putchar(‘\n‘);
}

const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=1e4+120;
const int maxn=1e3+220;
const double eps=1e-12;

int n,k,cnt;
char s[maxn];
int a[maxn];

struct Big
{
    int a[maxn],leng;
}ans[maxn],temp;
Big fun(Big A,Big B)
{
    Big C;
    mst(C.a,0);
    for(int i=A.leng;i<maxn;i++)A.a[i]=0;
    for(int i=B.leng;i<maxn;i++)B.a[i]=0;
    int hi=max(A.leng,B.leng),lc=0;
    for(int i=0;i<hi;i++)
    {
        if(A.a[i]<B.a[i])
        {
            A.a[i+1]--;
            A.a[i]+=10;
            C.a[i]=A.a[i]-B.a[i];
        }
        else C.a[i]=A.a[i]-B.a[i];
    }
    for(int i=0;i<=hi;i++)
    {
        if(C.a[i]<0)
        {
            C.a[i+1]--;
            C.a[i]+=10;
        }
    }
    int flag=1;
    for(int i=hi+1;i>=0;i--)
    {
        if(C.a[i]>0){lc=i;flag=1;break;}
    }
    if(flag)C.leng=lc+1;
    else C.leng=0;
    return C;
}

Big newBig(Big A)
{
    int lenth=A.leng;
    Big B,C,D,E;
    mst(B.a,0);B.leng=0;
    mst(C.a,0);C.leng=0;
    mst(D.a,0);D.leng=0;
    mst(E.a,0);E.leng=0;
    B.leng=lenth;
    if(lenth%2==1)
    {
        int mid=lenth/2;
        for(int i=lenth-1;i>=mid;i--)B.a[i]=A.a[i];
        for(int i=mid-1;i>=0;i--)B.a[i]=D.a[i]=0;
        D.a[mid]=1;
        D.leng=mid+1;
        E=fun(B,D);
        if(E.leng==A.leng-1)
        {
            C.leng=E.leng;
            for(int i=0;i<C.leng;i++)C.a[i]=9;
        }
        else
        {
            C.leng=A.leng;
            for(int i=C.leng-1;i>=mid;i--)C.a[i]=E.a[i];
            for(int i=mid-1;i>=0;i--)C.a[i]=E.a[2*mid-i];
        }
    }
    else
    {
        int mid=lenth/2;
        for(int i=lenth-1;i>=mid;i--)B.a[i]=A.a[i];
        for(int i=mid-1;i>=0;i--)B.a[i]=D.a[i]=0;
        D.a[mid]=1;D.leng=mid+1;
        E=fun(B,D);
        if(E.leng!=lenth)
        {
            C.leng=E.leng;
            for(int i=C.leng-1;i>=0;i--)C.a[i]=9;
        }
        else
        {
            C.leng=lenth;
            for(int i=C.leng-1;i>=mid;i--)C.a[i]=E.a[i];
            for(int i=mid-1;i>=0;i--)C.a[i]=E.a[2*mid-i-1];
        }
    }
    return C;
}
void solve()
{
    int lenth=temp.leng;
    Big A;
    while(lenth)
    {
        if(lenth==2&&temp.a[1]==1)
        {
            A.leng=1;
            A.a[0]=9;
        }
        else if(lenth==1)
        {
            if(temp.a[0]==0)break;
            A.leng=1;
            A.a[0]=temp.a[0];
            ans[++cnt]=A;
            break;
        }
        else A=newBig(temp);
        ans[++cnt]=A;
        temp=fun(temp,A);
        lenth=temp.leng;
    }
}
int main()
{
    int t,Case=0;
    read(t);
    while(t--)
    {
        printf("Case #%d:\n",++Case);
        scanf("%s",s);
        int len=strlen(s),num=0;
        cnt=0;
        for(int i=len-1;i>=0;i--)temp.a[num++]=s[i]-‘0‘;
        temp.leng=len;
        solve();
        printf("%d\n",cnt);
        for(int i=1;i<=cnt;i++)
        {
            for(int j=ans[i].leng-1;j>=0;j--)printf("%d",ans[i].a[j]);
            printf("\n");
        }
    }
    return 0;
}

  

时间: 2024-12-19 07:38:53

hdu-5920 Ugly Problem(贪心)的相关文章

HDU 5920 Ugly Problem 【模拟】 (2016中国大学生程序设计竞赛(长春))

Ugly Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0    Accepted Submission(s): 0Special Judge Problem Description Everyone hates ugly problems. You are given a positive integer. You m

HDU - 5920 Ugly Problem 求解第一个小于n的回文数

http://acm.hdu.edu.cn/showproblem.php?pid=5920 http://www.cnblogs.com/xudong-bupt/p/4015226.html 把前半部分复制过去,如果太大,那么早到第一个会使得其太大的点,减1,然后对应的中间的变成9 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <a

【高精度 JAVA】HDU 5920 Ugly Problem

题意 给你一个数字n(n < 10^1000),将其拆成若干个回文串(不超过50个)输出拆分方案 分析 不难想到,我们可以每次给n减一个小于他的最大的回文串,这样能够尽量构造出最少数量的回文串,方法可以使直接将前一半反转贴到后一半,如果比原来的数字大,那么前一半减少1,再反转贴为后一半 比较坑的地方就是 如果构造出来的是11比n大, 那么前一半-1变成了00 ,特判一下,应该为9:如果构造出来是101比n大,应该特判为99 这个题没太多好讲的,我想在这里记录一下java编程心得,以后也会更新 主

D - Ugly Problem HDU - 5920

D - Ugly Problem HDU - 5920 Everyone hates ugly problems. You are given a positive integer. You must represent that number by sum of palindromic numbers. A palindromic number is a positive integer such that if you write out that integer as a string i

HDU 4974 A simple water problem(贪心)

HDU 4974 A simple water problem 题目链接 签到题,很容易贪心得到答案是(sum + 1) / 2和ai最大值的最大值 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 100005; typedef long long ll; int t, n; ll a, Max, sum; int main(

HDU Hotaru&#39;s problem(Manacher算法+贪心)

manacher算法详见 http://blog.csdn.net/u014664226/article/details/47428293 题意:给一个序列,让求其最大子序列,这个子序列由三段组成,第一段和第二段对称,第一段和第三段一样. 思路:首先利用Manacher算法求出以任意两个相邻元素为中心的回文串长度,用a[i]表示i-1,i为中心的回文串长度的一半, 那么问题就转化成了求最大的x,使得a[i]>=x,a[i+x]>=x,这一步可以贪心来做. 将a[i]从大到小排序(间接排序保留

hdu 4296 Buildings(贪心)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4296 Buildings Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1822    Accepted Submission(s): 722 Problem Description Have you ever heard the sto

HDU 4903 (模拟+贪心)

Fighting the Landlords Problem Description Fighting the Landlords is a card game which has been a heat for years in China. The game goes with the 54 poker cards for 3 players, where the “Landlord” has 20 cards and the other two (the “Farmers”) have 1

HDU 1045 Fire Net 贪心

Problem Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through wh