洛谷试炼场被虐记录——新手村

https://www.luogu.org/training/mainpage

P1217 [USACO1.5] 回文质数

https://www.luogu.org/problemnew/show/P1217

高性能,可以先跑一遍5~10^8,除了11没有偶数位的回文质数。

#include <iostream>
#include <stdio.h>
using namespace std;

#define N 10000001
bool M[N];
int prime[800000];
int cnt = 0;
void EulerSieve() {
    for (int i = 2; i < N; i++) {
        if (!M[i]) {
            prime[cnt++] = i;
        }
        for (int j = 0; j < cnt&&prime[j] * i < N; j++) {
            M[prime[j] * i] = true;
            if (i%prime[j] == 0)break;
        }
    }
}

bool check(int x) {
    int t = x, y = 0;
    while (t) {
        y = y * 10 + t % 10;
        t /= 10;
    }
    if (x == y) return true;
    return false;
}

int main() {
    EulerSieve();
    int a, b;
    cin >> a >> b;
    //小于10^8,最后一个回文质数
    if (b > 9989899)b = 9989899;
    int i = 0;
    while (prime[i] < a)i++;

    for (; prime[i] <= b; i++) {
        if (check(prime[i])) {
            printf("%d\n", prime[i]);
        }
    }

    return 0;
}
View Code

P1149 火柴棒等式

https://www.luogu.org/problemnew/show/P1149

最多24根火柴,去掉 + 和 = 还有20个,搞不太清等式中,需要表示的最小数字下界,写个暴力的,反正由于火柴棒数目的限制,等式是不可能超过 11111+11111=xxx;测试出来的结果,最大只需要表示的数是:711;

测试代码:

#include <iostream>
using namespace std;

int num[10] = { 6,2,5,5,4,5,6,3,7,6 };
int cnt[25000];
int res = 0;

int main() {
    int n;
    cin >> n;
    cnt[0] = 6;
    for (int i = 0; i < 25000; i++) {
        int t = i;
        while (t) {
            cnt[i] += num[t % 10];
            t /= 10;
        }
    }

    int res = 0;
    for (int i = 0; i <= 11111; i++) {
        for (int j = 0; j <= 11111; j++) {
            int k = i + j;
            if (cnt[i] + cnt[j] + cnt[k] == n - 4) {
                cout << i << "+" << j << "=" << k << endl;
                //res ++;
            }
        }
    }
    //cout << res << endl;
}
View Code

测试结果:

/*
24
0+8=8
0+12=12
0+13=13
0+15=15
0+21=21
0+31=31
0+47=47
0+51=51
0+74=74
0+117=117
0+171=171
0+711=711
1+20=21
1+30=31
1+42=43
1+47=48
1+50=51
1+112=113
1+117=118
1+170=171
1+710=711
2+8=10
2+10=12
2+19=21
2+41=43
2+72=74
2+77=79
2+111=113
3+10=13
3+13=16
3+44=47
3+114=117
4+43=47
4+57=61
4+70=74
4+113=117
4+117=121
5+10=15
5+16=21
5+17=22
6+15=21
7+15=22
7+27=34
7+40=47
7+41=48
7+54=61
7+72=79
7+77=84
7+110=117
7+111=118
7+114=121
8+0=8
8+2=10
9+12=21
10+2=12
10+3=13
10+5=15
11+13=24
11+14=25
11+16=27
11+31=42
11+41=52
11+61=72
12+0=12
12+9=21
13+0=13
13+3=16
13+11=24
14+11=25
14+27=41
14+77=91
15+0=15
15+6=21
15+7=22
16+5=21
16+11=27
17+5=22
17+24=41
17+57=74
17+74=91
19+2=21
20+1=21
21+0=21
24+17=41
27+7=34
27+14=41
30+1=31
31+0=31
31+11=42
40+7=47
41+2=43
41+7=48
41+11=52
41+71=112
42+1=43
43+4=47
44+3=47
47+0=47
47+1=48
50+1=51
51+0=51
54+7=61
57+4=61
57+17=74
61+11=72
70+4=74
71+41=112
72+2=74
72+7=79
74+0=74
74+17=91
77+2=79
77+7=84
77+14=91
110+7=117
111+2=113
111+7=118
112+1=113
113+4=117
114+3=117
114+7=121
117+0=117
117+1=118
117+4=121
170+1=171
171+0=171
710+1=711
711+0=711
*/
View Code

代码:

#include <iostream>
using namespace std;

int num[10] = { 6,2,5,5,4,5,6,3,7,6 };
int cnt[712];
int res = 0;

int main() {
    int n;
    cin >> n;
    cnt[0] = 6;
    for (int i = 0; i < 712; i++) {
        int t = i;
        while (t) {
            cnt[i] += num[t % 10];
            t /= 10;
        }
    }
    int res = 0;
    for (int i = 0; i < 712; i++) {
        if (cnt[i] > n - 4)continue;
        for (int j = 0; j < 712; j++) {
            int k = i + j;
            //含有711以上的等式表示不出来
            if (k > 711 || cnt[i] + cnt[j] > n - 4)continue;
            if (cnt[i] + cnt[j] + cnt[k] == n - 4) {
                //cout << i << "+" << j << "=" << k << endl;
                res++;
            }
        }
    }
    cout << res << endl;
}
View Code

 

P2089 烤鸡

https://www.luogu.org/problemnew/show/P2089

#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n;
vector<string>v;
void DFS(string help,int sum,int k) {
    if (k == 11)return;
    if (sum == n) {
        if (k != 10)return;
        //cout << help << endl;
        //因为必须要先输出个数,所以只能先存起来
        v.push_back(help);
    }
    for (int i = 1; i <= n - sum && i <= 3; i++) {        
        DFS(help + string(1, i + '0')+" ", sum + i, k + 1);
    }
}
int main() {
    cin >> n;
    DFS("",0, 0);
    if (v.size() == 0)cout << 0 << endl;
    else {
        cout << v.size() << endl;
        for(int i=0;i<v.size();i++){
            cout << v[i] << endl;
        }
    }
    return 0;
}
View Code

 

P1014 Cantor表

https://www.luogu.org/problemnew/solution/P1014

#include <iostream>
using namespace std;

int getSum(int i) {
    return (i*i + i) / 2;
}

int main() {
    int n;
    cin >> n;
    int i = 1;
    while (getSum(i) < n) {
        i++;
    }

    int sum = getSum(i - 1);
    int top = i + 1, down = 0;
    while (sum < n) {
        top--; down++;
        sum++;
    }
    if (i & 1) {
        cout << top << "/" << down << endl;
    }else{
        cout << down << "/" << top << endl;
    }
    return 0;
}
View Code

 

P1464 Function

https://www.luogu.org/problemnew/show/P1464

#include <iostream>
#include <stdio.h>
using namespace std;
int m[21][21][21];

int w(long long a, long long b, long long c) {
    if (a <= 0 || b <= 0 || c <= 0) return m[0][0][0];
    if (a > 20 || b > 20 || c > 20) return m[20][20][20];
    if (m[a][b][c] != 0)return m[a][b][c];
    if (a < b&&b < c)return m[a][b][c] = w(a, b, c - 1) + w(a, b - 1, c - 1) - w(a, b - 1, c);
    return m[a][b][c] = w(a - 1, b, c) + w(a - 1, b - 1, c) + w(a - 1, b, c - 1) - w(a - 1, b - 1, c - 1);
}
int main() {
    long long a, b, c;
    m[0][0][0] = 1;
    m[20][20][20] = 1048576;
    while (cin >> a >> b >> c) {
        if (a == -1 && b == -1 && c == -1)break;
        printf("w(%lld, %lld, %lld) = %d\n", a, b, c, w(a, b, c));
    }
    return 0;
}
View Code

 

P1022 计算器的改良

https://www.luogu.org/problemnew/show/P1022

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
    char c,cc;
    bool flag = false, end = false;
    int l = 0, r = 0, t = 0, coe = 1;
    while (!end && scanf("%c", &c)) {
        if (c == '\n')end = true;
        if (c >= '0'&&c <= '9') {
            t *= 10;
            if (flag) coe = -coe;
            
            t += coe * (c - '0');
        }
        else {
            if (c >= 'a'&&c <= 'z') {
                cc = c;
                //t==0,是a系数为1的情况
                l += t == 0 ? 1 : t;
            }
            else r += t;
            t = 0;

            if (c == '+')coe = 1;
            else if (c == '-')coe = -1;
            else if (c == '=') {
                flag = true;
                coe = 1;
            }
        }
       }
    r = -r;
    printf("%c=%.3f", cc, r / (float)l);
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/czc1999/p/10464881.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值