这题DP或者母函数都能做,不过要注意的是计算总和要用int64 开始用int的时候一直WA真是捉急,还有一点是 注意一组测试数据 90 80 0 输出应该是1
AC代码如下:
母函数:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
int main(){
int A, B, t;
__int64 f1[61], f2[61];
while( scanf( "%d%d%d", &A, &B, &t ) != EOF ){
memset( f1, 0, sizeof( f1 ) );
memset( f2, 0, sizeof( f2 ) );
t = t / 15;
if( t == 0 ){
if( A > B ){
cout << 1 << endl;
}else{
cout << 0 << endl;
}
continue;
}
B += t / 2;
t = ( t + 1 ) / 2;
f1[1] = f1[2] = f1[3] = 1;
for( int i = 2; i <= t; i++ ){
for( int j = 1; j <= 3; j++ ){
for( int k = 1; k <= 60; k++ ){
f2[j+k] += f1[k];
}
}
for( int j = 1; j <= 60; j++ ){
f1[j] = f2[j];
}
memset( f2, 0, sizeof( f2 ) );
}
__int64 ans = 0;
for( int i = 1; i <= 60; i++ ){
if( A + i > B ){
ans += f1[i];
}
}
cout << ans << endl;
}
return 0;
}
DP做法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
__int64 dp[21][61];// dp[i][j] 第i次进攻后增加得分的为j的方案数
void init(){
memset( dp, 0, sizeof( dp ) );
dp[1][1] = dp[1][2] = dp[1][3] = 1;
for( int i = 2; i <= 20; i++ ){
for( int j = 1; j <= 60; j++ ){
if( j >= 1 ) dp[i][j] += dp[i-1][j-1];
if( j >= 2 ) dp[i][j] += dp[i-1][j-2];
if( j >= 3 ) dp[i][j] += dp[i-1][j-3];
}
}
}
int main(){
int A, B, t;
init();
while( scanf( "%d%d%d", &A, &B, &t ) != EOF ){
t /= 15;
if( t == 0 ){
if( A > B ){
cout << 1 << endl;
}else{
cout << 0 << endl;
}
continue;
}
B += t / 2;
t = ( t + 1 ) / 2;
__int64 ans = 0;
for( int i = 1; i <= 60; i++ ){
if( i + A > B ){
ans += dp[t][i];
}
}
cout << ans << endl;
}
return 0;
}