硬币求和
#include <bits/stdc++.h>
using namespace std;
int a[10] = {1, 2, 5, 10, 20, 50, 100, 200}, n = 8;
int f[209], m = 200;
int main() {
f[0] = 1;
for (int i = 0; i < n; i ++) {
for (int j = a[i]; j <= m; j ++) {
f[j] += f[j - a[i]];
}
}
cout << f[m];
return 0;
}
全数字的乘积
#include <bits/stdc++.h>
using namespace std;
int f[10000];
bool fun(int a, int b) {
int c = a * b;
int h[10] = {1};
while (a) {
int t = a % 10;
if (h[t]) return false;
h[a % 10] = 1;
a /= 10;
}
while (b) {
int t = b % 10;
if (h[t]) return false;
h[b % 10] = 1;
b /= 10;
}
while (c) {
int t = c % 10;
if (h[t]) return false;
h[c % 10] = 1;
c /= 10;
}
return true;
}
int main() {
int ans = 0;
for (int i = 1; i <= 9; i ++) {
for (int j = 1000; j <= 9999; j ++) {
if (fun(i, j)) {
if (f[i * j] == 0) {
ans += i * j;
f[i * j] = 1;
}
}
}
}
for (int i = 10; i <= 99; i ++) {
for (int j = 100; j <= 999; j ++) {
if (fun(i, j)) {
if (f[i * j] == 0) {
ans += i * j;
f[i * j] = 1;
}
}
}
}
cout << ans;
return 0;
}
消去数字的分数
#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) {
return y == 0 ? x : gcd(y, x % y);
}
int main() {
int a = 1, b = 1;
for (int i = 1; i < 10; i ++) {
for (int j = 1; j < 9; j ++) {
for (int k = j + 1; k < 10; k ++) {
int u, d;
u = 10 * j + i;
d = 10 * i + k;
if (u * k == d * j) {
a *= u;
b *= d;
}
u = 10 * i + j;
d = 10 * k + i;
if (u * k == d * j) {
a *= u;
b *= d;
}
}
}
}
int t = gcd(a, b);
cout << b / t;
return 0;
}
各位数字的阶乘
#include <bits/stdc++.h>
using namespace std;
const int N = 3e6;
int f[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};
int a[N + 9];
int main() {
int sum = 0;
for (int i = 1; i <= N; i ++) {
a[i] = a[i / 10] + f[i % 10];
if (a[i] == i) {
sum += i;
}
}
cout << sum - 1 - 2;
return 0;
}
圆周素数
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
bool a[N + 9];
int p[N], n;
int b[4] = {1, 3, 7, 9}, cnt;
int base[7] = {-1, 1, 10, 100, 1000, 10000, 100000};
void dfs(int x, int pos) {
if (pos > 6) return;
for (int i = 0; i < 4; i ++) {
int num = x * 10 + b[i], t = num;
bool flag = true;
for (int j = 1; j <= pos; j ++) {
if (a[t]) {
flag = false;
break;
}
t = t % 10 * base[pos] + t / 10;
}
if (flag) cnt ++;
dfs(num, pos + 1);
}
}
int main() {
a[1] = true;
for (int i = 2; i * i <= N; i ++) {
if (!a[i]) {
for (int j = i * i; j <= N; j += i) {
a[j] = true;
}
}
}
dfs(0, 1);
cout << cnt + 2;
return 0;
}
双进制回文数
#include <bits/stdc++.h>
using namespace std;
int a[100000], n, ans;
int main() {
for (int i = 1; i <= 9; i ++) {
a[++ n] = i;
a[++ n] = i * 11;
}
for (int i = 10; i < 100; i ++) {
a[++ n] = i * 10 + i / 10;
a[++ n] = i * 100 + i % 10 * 10 + i / 10;
}
for (int i = 100; i < 1000; i ++) {
a[++ n] = i * 100 + i / 10 % 10 * 10 + i / 100;
a[++ n] = i * 1000 + i % 10 * 100 + i / 10 % 10 * 10 + i / 100;
}
for (int i = 1; i <= n; i ++) {
int t = a[i];
int len = log2(t) + 1;
bool flag = true;
for (int j = 0, k = len - 1; j < k; j ++, k --) {
if ((t >> j & 1) != (t >> k & 1)) {
flag = false;
break;
}
}
if (flag) ans += t;
}
cout << ans;
return 0;
}
|