【中石油】【HDU】Simple Addition expression

杭电上也有这道题:传送门(HDU 2451)

题目描述

A luxury yacht with 100 passengers on board is sailing on the sea in the twilight. The yacht  is ablaze with lights and there comes out laughers and singing from the hall where an evening party is in full swing. People are singing, dancing and enjoying themselves.

The yacht is equipped with the most advanced navigation and driving system which can all be manipulated by a computer. When the captain notices that there is only gentle breeze and the sea waves are not high, he starts the autopilot. The yacht sails forward smoothly, ploughs the waves. When it’s completely dark, the passengers start to feel a little funny for sudden forward rushes or sudden decelerations or slight swings. The captain immediately walks to the driving platform and switches the autopilot to human manipulation. The yacht returns back to normal and the party restarts. Laughers come back, too.

The captain summons the engineer on board to do a thorough check of the navigation system. It turns out that only the computer is out of order, but the exact failure is still unclear. There is a computer scientist among the passengers who is also invited to the cab to give a hand. He first inputs several groups of data to test the computer. When he inputs 1+2+3, the computer outputs 6, which is exactly right. But when he inputs 4+5+6, the computer outputs 5, which is wrong. Then he inputs 12+13+14, and gets 39, another right answer, while he inputs 14+15+16, and gets 35, another wrong answer. After the test, the computer scientist says smilingly: “the failure is clear now. The computer's adder can not carry." After excluding the failure, the captain restarts the autopilot and the yacht returns back to normal, sailing smoothly on the sea.

The captain and the engineer invite the computer scientist to sit down and have a talk. The computer scientist tells a story as following:

A former mathematician defined a kind of simple addition expression. 
If there is an expression (i) + (i+1) + (i+2), i>=0, when carried out additive operations, no position has a carry, it is called simple addition expression.

For instance, when i equals 0, 0+1+2 is a simple addition expression, meanwhile when i equals 11, 11+12+13 is a simple addition expression, too. Because of that no position has a carry.

However, when i equals 3, 3+4+5 is not a simple addition expression, that is because 3+4+5 equals 12, there is a carried number from unit digit to tens digit. In the same way, when i equals 13, 13+14+15 is not a simple addition expression, either. However, when i equals 112, 112+113+114 is a simple addition expression. Because 112+113+114 equals 339, there is no carry in the process of adding.

when the students have got the definition of simple addition expression, the mathematician puts forward a new question: for a positive integer n, how many simple addition expressions exist when i<n. In addition, i is the first number of a simple addition expression.

when the value of n is large enough, the problem needs to be solved by means of computer.

输入

There are several test cases, each case takes up a line, there is an integer n (n<10^10).

输出

Output the number of all simple addition expressions when i<n.

 样例输入

1
2
3
4
10
11

样例输出

1
2
3
3
3
4

题目大意:给你一个数,让你找出比这个数小的符合(i-1)+ i +(i+1)没有进位的个数,不管是个位十位还是百位等等都不能进位,符合这个要求的数有多少个。


思路分析:依照惯例,我们会列出n=10时,符合条件个数,n=100时,符合条件个数,这样我们不难发现,对于确定的位数得数每一位都小于3的这些数都是符合的,所以规律就有了。小于10的一共有3个,10到100有9个,100到1000有36个,我们可以用前缀和的思想把小于10的倍数的总共的都存起来,前缀和也是有规律的。前10个一共有3个,前100有12个,前1000有48个,所以每一次都是前一次的3倍。然后就是计算了,我们先计算出比这个数少一位的一共有多少个,在找出和这个数位数一样的比它小的数有多少个加起来就是答案了,这题难就难在找出本位数的有多少个。

给你们一个例子看看就懂了

例如124124124

我们先定义一个num=0,p[i].num是存前缀的(提前打好表),然后从这个数的最低位开始找。

cnt=1,cnt是这个数的当前第几位。

1.cnt==1,4>3,num=p[cnt].num,

2.cnt==2,2<=3,num+=3*2,3是第一位有三种情况,2是本位的数是2,比2小的有0、1两种情况。

3.cnt==3,1<=3,num+=3*4*1,3是第一位有三种情况,4是第二位有四种情况,1是本位有0一种情况。

4.cnt==4,4>3,num=p[cnt].num,因为到目前比这个数小的数都可以,所以num=p[cnt].num。

5. 以此类推......

AC代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll lldcin()
{
    ll tmp = 0, si = 1;
    char c;
    c = getchar();
    while (c > '9' || c < '0')
    {
        if (c == '-')
            si = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        tmp = tmp * 10 + c - '0';
        c = getchar();
    }
    return si * tmp;
}
struct node{
    ll num;
    ll index;
    ll bound;
    ll d;
}p[20];
void make_pre()
{
    p[0].num=0;
    p[1].num=3;
    p[1].index=10;
    p[1].d=1;
    p[1].bound=3;
    for(int i=2;i<=19;i++)
    {
        p[i].num=p[i-1].num*4;
        p[i].index=p[i-1].index*10;
        p[i].d=i;
        p[i].bound=p[i-1].bound*10+3;
    }
}
int main()
{
    make_pre();
    ll n;
    ll sum=0;
    while(~scanf("%lld",&n))
    {
        sum=0;
        ll tmp=n,num1;
        for(int i=0;i<=19;i++){
        	if(n<p[i].index){
        		ll cnt=1,num=1;
        		num1=0;
        		if(n<p[i].bound)
        		while(tmp){
        			int a=tmp%10;
        			if(cnt==1){
        				if(a>=3){
        					num1=p[cnt].num;
						}
						else
							num1+=a;
						num=3;
					}
					else if(cnt==p[i].d){
						if(a<=3&&a>=1)
							num1+=num*(a-1);
						else
							num1=p[cnt].num;
					}
					else{
						if(a<=3)
							num1+=a*num;
						else
							num1=p[cnt].num;
						num*=4;
					}
					cnt++;
					tmp/=10;
				}
				else
					sum=p[i].num;
        		break;
			}
			sum=p[i].num;
		}
		sum+=num1;
        printf("%lld\n",sum);
    }
    return 0;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值