HDU 2451 找规律 2008哈尔滨现场赛

题目链接
题目描述
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

题意:
给你一个整数n,问在i属于 [0,n)之间,有多少个i满足i+(i+1)+(i+2)是没有进位的(各个位置上都没进位)

分析:
仔细观察就可以发现,当只考虑一个位的时候,只有0、1、2才满足,其他的情况是不满足的,但是如果考虑多个位置的话,那就需要再找规律了;
定义dp[i]表示当有i+1个位置的时候的方案数;
可以发现当有三个位的时候,那么结果数就是在前一个的结果上乘4;
因为可以是0与[0,2]结合,1与[0,2]结合,2与[0,2]结合,3与[0,2]结合,这就是所以的情况了;然后其他的情况依次类推;

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e5+10;
const int INF=0x3f3f3f3f;
ll dp[20],a[20];
void init(){
    dp[1]=3;
    dp[0]=1;
    for(int i=2;i<=11;i++){
        ll t=3;
        for(int j=1;j<i;j++)
        t*=4;
        dp[i]=t;
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    ll n;
    init();
    while(scanf("%lld",&n)!=EOF){
        int cnt=0;
        ll m=n;
        while(m){
            a[cnt++]=m%10;
            m/=10;
        }
        ll ans=0;
        for(int i=cnt-1;i>=0;i--){//从高位开始模拟
            if(a[i]>=4) a[i-1]=9;//如果当前位置是大于等于4的,那么后面的位置是多少都没有关系了,干脆就设成9,只要是大于等于4就行
            if(a[i]<=3)
            ans+=a[i]*dp[i];
            else
            ans+=3*dp[i];//当前位置的话,最多就是到3,1什么什么,2什么什么,3什么什么,没了
        }
        printf("%lld\n",ans);
    }
    return 0;
}


其实这道题目还可以用递归来打表,然后二分答案;

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=1e6+10;
const int INF=0x3f3f3f3f;
ll a[N];
int cnt;
ll num;
void dfs(int step){
    if(step==10){
        a[cnt++]=num;
//        cout<<a[cnt-1]<<endl;
        return;
    }else if(step==9){//个位
        for(int i=0;i<=2;i++){
            num=num*10+i;
            dfs(step+1);
            num/=10;
        }
    }else{
        for(int i=0;i<=3;i++){
            num=num*10+i;
            dfs(step+1);
            num/=10;
        }
    }
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    ll n;
    dfs(0);
    while(scanf("%lld",&n)!=EOF){
        printf("%d\n",lower_bound(a,a+cnt,n)-a);
    }
    return 0;
}


发布了229 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/98654639