对于 100% 的数据,1≤N≤5000。
#include
using namespace std;
const int N = 5003;
int len = 1;
int f[N][N];
int main()
{
int n; scanf("%d", &n);
f[1][1] = 1; f[2][1] = 2;
for (int i = 3; i <= n; i ++ )
{
for (int j = 1; j <= len; j ++ )
f[i][j] = f[i - 1][j] + f[i - 2][j];
for (int j = 1; j <= len; j ++ )
{
f[i][j + 1] += f[i][j] / 10;
f[i][j] %= 10;
if (f[i][len + 1]) len ++ ;
}
}
for (int i = len; i; i -- ) printf("%d", f[n][i]);
}
对于100% 的数据,1≤n,m≤20,0≤ 马的坐标≤20。
#include
using namespace std;
const int N = 30;
typedef long long ll;
int dx[8] = {-2, -2, -1, 1, 2, 2, 1, -1};
int dy[8] = {-1, 1, 2, 2, 1, -1, -2, -2};
ll f[N][N];
bool st[N][N];
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int bx, by, mx, my; cin >> bx >> by >> mx >> my;
bx += 2, by += 2, mx += 2, my += 2;
st[mx][my] = true;
for (int i = 0; i < 8; i ++ )
{
int x = mx + dx[i], y = my + dy[i];
st[x][y] = true;
}
f[2][2] = 1;
for (int i = 2; i <= bx; i ++ )
for (int j = 2; j <= by; j ++ )
{
if (i == 2 && j == 2) continue;
if (st[i][j]) continue;
f[i][j] = f[i - 1][j] + f[i][j - 1];
}
cout << f[bx][by];
}
输入文件只含一个整数 n(1≤n≤18)。
#include
using namespace std;
typedef long long ll;
const int N = 20;
ll f[N][N];
ll dfs(int que, int stk)
{
if (f[que][stk]) return f[que][stk];
if (!que) return 1;
if (stk) f[que][stk] += dfs(que, stk - 1);
f[que][stk] += dfs(que - 1, stk + 1);
return f[que][stk];
}
int main()
{
int n; scanf("%d", &n);
printf("%lld", dfs(n, 0));
}
一个正整数 n(n≤1000)。
#include
using namespace std;
typedef long long ll;
ll dfs(int u) {
if (u == 1)
ll tot = 1;
for (int i = 1; i <= u / 2; ++ i) {
tot += dfs(i);
}
return tot;
}
int main() {
cout << "a[1001]={";
for (int i = 1; i <= 1001; ++ i) {
cout << dfs(i) << ',';
}
cout << "}";
}
f[1] = 1
f[2] = f[1] + 1
f[3] = f[1] + 1
f[4] = f[1] + f[2] + 1
f[5] = f[1] + f[2] + 1
#include
using namespace std;
typedef long long ll;
const int N = 1010;
ll f[N];
int main() {
int n;
cin >> n;
f[1] = 1;
for (int i = 1; i <= n; ++ i) {
f[i] = 1;
for (int j = 1; j <= i / 2; ++ j) {
f[i] += f[j];
}
}
cout << f[n];
}
保证输入的数在[−9223372036854775808,9223372036854775807]之间,并且是整数。
return f[a][b][c] = dfs(a - 1, b, c) + ...
的形式,return最后再写#include
using namespace std;
typedef long long ll;
ll f[25][25][25];
ll dfs(ll a, ll b, ll c) {
if (a <= 0 || b <= 0 || c <= 0) return 1;
if (f[a][b][c]) return f[a][b][c];
ll t;
if (a > 20 || b > 20 || c > 20) t = dfs(20, 20, 20);
else if (a < b && b < c) t = dfs(a, b, c - 1) + dfs(a, b - 1, c - 1) - dfs(a, b - 1, c);
else t = dfs(a - 1, b, c) + dfs(a - 1, b - 1, c) + dfs(a - 1, b, c - 1) - dfs(a - 1, b - 1, c - 1);
f[a][b][c] = t;
return f[a][b][c];
}
int main() {
ll a, b, c;
while (scanf("%lld%lld%lld", &a, &b, &c) == 3) {
if (a == -1 && b == -1 && c == -1) break;
printf("w(%lld, %lld, %lld) = ", a, b, c);
if (a > 21) a = 21;
if (b > 21) b = 21;
if (c > 21) c = 21;
printf("%lld\n", dfs(a, b, c));
}
}
对于 100%的数据:解压后的字符串长度在 20000 以内,最多只有十重压缩。
保证 有且仅有 数字字母和 [ 和 ]
#include
using namespace std;
string dfs() {
char ch;
string res = "", s = "";
while (cin >> ch) {
if (ch == '[') {
int k;
cin >> k;
s = dfs();
while (k -- ) {
res += s;
}
} else if (ch == ']') {
return res;
} else {
res += ch;
}
}
}
int main() {
cout << dfs();
}
对于100%的数据,M,N≤1000
#include
using namespace std;
const int N = 1010;
int f[N][N];
int len = 1;
int main() {
int m, n;
cin >> m >> n;
f[1][1] = 1;
f[2][1] = 1;
for (int i = 3; i <= n - m + 1; ++ i) {
for (int j = 1; j <= len; ++ j) {
f[i][j] = f[i - 1][j] + f[i - 2][j];
}
for (int j = 1; j <= len; ++ j) {
f[i][j + 1] += f[i][j] / 10;
f[i][j] %= 10;
}
if (f[i][len + 1]) len ++ ;
}
for (int i = len; i; i -- )
cout << f[n - m + 1][i];
}
正数 a i a_i ai可以有相同的数字,每个数字均在1000以内;保证答案的范围在int之内。
#include
using namespace std;
int f[110][10010];
int a[110];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
for (int i = 0; i <= n; ++ i) {
f[i][0] = 1;
}
for (int i = 1; i <= n; ++ i) {
for (int j = 1; j <= m; ++ j) {
if (j >= a[i]) f[i][j] += f[i - 1][j - a[i]];
f[i][j] += f[i - 1][j];
}
}
cout << f[n][m];
}
#include
using namespace std;
int f[10010];
int a[110];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
f[0] = 1;
for (int i = 1; i <= n; ++ i) {
for (int j = m; j >= a[i]; -- j)
f[j] += f[j - a[i]];
}
cout << f[m];
}
第一行两个空格隔开的整数 n,k(1≤n≤20,k 一个整数N(1<=N<=1000000),表示墙壁的长。 该字符串包含最多30个大写字母,并 N ≤ 1 0 18 \leq10^{18} ≤1018 4≤n≤100 1 ≤ n ≤ 2 ∗ 1 0 4 1 \leq n \leq 2*10^{4} 1≤n≤2∗104
#include
P1990 覆盖墙壁
#include
P3612 [USACO17JAN]Secret Cow Code S
#include
P1259 黑白棋子的移动
#include
P1010 [NOIP1998 普及组] 幂次方
#include
P1228 地毯填补问题
#include
P1498 南蛮图腾
#include