Hdu 5710 Digit-Sum【思维】

Digit-Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 486    Accepted Submission(s): 148


Problem Description
Let  S(N)  be digit-sum of  N , i.e  S(109)=10,S(6)=6 .

If two positive integers  a,b  are given, find the least positive integer  n  satisfying the condition  a×S(n)=b×S(2n) .

If there is no such number then output 0.
 

Input
The first line contains the number of test caces  T(T10) .
The next  T  lines contain two positive integers  a,b(0<a,b<101) .
 

Output
Output the answer in a new line for each test case.
 

Sample Input
  
  
3 2 1 4 1 3 4
 

Sample Output
  
  
1 0 55899


题目大意:

S(n)表示的是求数字n十进制下每个位子的数字的加和。


给你两个常数a,b,让你找到最小的值n,使得a*S(n)==b*S(2*n);


思路(思路参考自:http://blog.csdn.net/kim0403/article/details/52069813):


S(1)= 1,S(2*1)=2;

S(2)=2,S(2*2)=4;

S(3)=3,S(2*3)=6;

S(4)=4,S(2*4)=8;

S(5)=5,S(2*5)=1;

S(6)=6,S(2*6)=3;

S(7)=7,S(2*7)=5;

S(8)=8,S(2*8)=7;

S(9)=9,S(2*9)=9;


显然,S(2n)=S(n)*2-L*9;

这里L表示的是数字n中,包含5~9这几个数的个数。


那么我们根据等式:

a*S(n)=b*S(2*n);

化简有:

Sn/L=9*b/2b-a;


那么我们设定Sn就是9*b.那么L就是2b-a;

因为这里是成倍数增长的,那么我们将S/gcd(Sn,L),将L/gcd(Sn,L);

得到化简后的S和L.


那么此时的S就是数字n各个位子上的数字和,L就是数字n中,【5~9】的数字的个数。


接下来贪心的从后往前设定值即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int ans[150000];
int gcd(int x,int y)
{
    if(y==0)return x;
    else return gcd(y,x%y);
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        int s=9*b;
        int l=2*b-a;
        if(l==0){printf ("1\n"); continue;}
        if(l<0){printf ("0\n"); continue;}
        if(5*l>s){printf("0\n");continue;}
        int gc=gcd(s,l);
        l/=gc;
        s/=gc;
        s-=5*l;
        int cnt=0;
        for(int i=0;i<l;i++)
        {
            if(s>0)
            {
                int ad=min(4,s);
                s-=ad;
                ans[cnt++]=5+ad;
            }
            else ans[cnt++]=5;
        }
        while(s>0)
        {
            if(s>0)
            {
                ans[cnt++]=min(s,4);
                s-=min(s,4);
            }
            else break;
        }
        for(int i=cnt-1;i>=0;i--)
        {
            printf("%d",ans[i]);
        }
        printf("\n");
    }
}











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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值