atcoder ABC51 B题

题目链接 http://abc051.contest.atcoder.jp/tasks/abc051_b

O(n)做法 

虽然是水题,但是思想在进步啊 !!

#include
#include
#define ll long long
using namespace std;
ll f(ll s,ll k){
    if(s < 0) return 0;
    if(k >= s) return s+1;
    if(s%2 == 0 && k < s/2) return 0;
    if(s%2 != 0 && k <= s/2) return 0;
    return s+1-(s-k)*2;
}
int main(){
    ll k,s;
    while(scanf("%lld%lld",&k,&s)!=EOF){
        ll ans = 0;
        for(ll i = 0;i <= k;++i){
            ans += f(s-i,k);
        }
        cout << ans << endl;
    }
    return 0;
}


你可能感兴趣的:(ACM程序设计)