题解:P11229 [CSP-J 2024] 小木棍(民间数据)

· · 题解

P11229 小木棍sticks 题解

题目分析

输入使用的木棍数量,输出符合木棍数的最小整数,否则输出 -1 。

思路推理

首先排除掉暴力的算法,时间复杂度承受不住; 其次考虑寻找规律(如果考场上一眼看不出规律的可以打一份暴力求证,或者根据题目中的特殊性质判断),不难发现本题答案与 n 对 7 的余数息息相关(为了表达方便,下将 n 对 7 的余数 设为 p )

当 n>=1 且 n<=7 时需要进行特判:

当 n=10 需要进行特判(考场上没有看到这个……),答案为 22 ;

接下来就是有序的规律了:

AC code:

#include<bits/stdc++.h>
using namespace std;
int price[11]={0,10,18,0,20,28,68};
int st[11]={0,-1,1,7,4,2,6,8};
int t,n;
int main()
{
    scanf("%d",&t);
    for(int j=1;j<=t;j++)
    {
        scanf("%d",&n);
        int cnt=n/7;
        if(n>=1&&n<=7) printf("%d\n",st[n]);
        else if(n==10) puts("22");
        else if(n%7==0)
        {
            for(int i=1;i<=cnt;i++) printf("8");
            puts("");
        }
        else if(n%7==3)
        {
            cnt-=2;
            printf("200");
            for(int i=1;i<=cnt;i++) printf("8");
            puts("");
        }
        else
        {
            cnt-=1;
            printf("%d",price[n%7]);
            for(int i=1;i<=cnt;i++) printf("8");
            puts("");
        }
    }
    return 0;
}

蒟蒻只希望不要被特判点给卡掉……