NBC#1官方题解

· · 个人记录

恭喜:

|problem|first blood| |:---:|:---:|:---:| |jcx's lucky number|Felix_F| |jcx's game|DavidBao| |jcx go to school|j2k183| |jcx's homework|j2k183| |Don't try to count|Henry2012| |jcx's Perfect Square|Henry2012|

恭喜Henry2012全部AK啦!!!

请上面同学加微信:ctwgdbfcblr

来领取奖励!!!

下面进入正文

jcx's lucky number

题目大意

如果有一个数她有且只有两个因数,那么她就是季宸希的幸运数。现在让你判断幸运数

题目分析

我们只要判断质数就行了

ACCode

//made by ymh
#include<bits/stdc++.h>
using namespace std;
bool isprime(int x){
    if (x<2 || (x>2 && x%2==0)) return false;
    for (int i=3;i*i<=x;++i)
        if (x%i==0) return false;
    return true;
}
signed main(){
    int n;
    cin>>n;
    if (isprime(n)) puts("Y");
    else puts("N");
    return 0;
}

jcx's game

这题数据错了,标程就在数据范围那里,不讲

jcx go to school

题目大意

季宸希每次可以上一个或者两个台阶,但是第二个台阶不可以从第一节上来,问你一个n节的楼梯有多少种上楼方案。

题目分析

很简单,定义如下函数:

\operatorname{fib}(n)=\begin{cases}1&n\le 2\\\operatorname{fib}(n-1)+\operatorname{fib}(n-2)&n>2\end{cases}

然后实现即可

ACCode

//made by ymh
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,num=1;
    cin>>n;
    for(int i=1;i<=30;i++){
        if(n==i) cout<<num;
        num=num*2+2;
    }
    return 0;
}

jcx's homework

题目大意

给定一个只包含加法和乘法的算术表达式,请你编程计算表达式的值。

题目分析

很简单,表达式求值,这个不用讲了吧。O(n)搞定

ACCode

//made by ymh
#include<bits/stdc++.h>
using namespace std;
const int M=1e4;
stack<int> stk;
string s;
signed main(){
    int x,ans=0;
    char op;
    cin>>x;
    stk.push(x);
    while (cin>>op>>x){
        if (op=='*') (stk.top()*=x)%=M;
        else stk.push(x);
    }
    while (stk.size())
        (ans+=stk.top())%=M,stk.pop();
    printf("%d",ans);
    return 0;
}

Don't try to count

题目大意

给定一个长度为

$s$,你可以进行下列操作: 将 字符串 $x$ 复制到字符串 $x$末尾 询问最少进行多少次操作字符串 $s$ 为字符串 $x$ 的子串。 ### 题目分析 十分简单,暴力模拟,直至当前的字符串长度大于等于 $n* m

ACCode

//made by ymh
#include<bits/stdc++.h>
using namespace std;
signed main(){
//  freopen("1.txt","r",stdin);
//  freopen("3.txt","w",stdout);
    int T,n,m;
    string s,t;
    cin>>T;
    while (T--){
        cin>>n>>m>>s>>t;
        int ct=0,last=0;
        while (s.find(t)==-1){
            if (n>2*m){
                if (last==2){
                    ct=-1;
                    break;
                }
                else ++last;
            }
            ++ct,s+=s,n+=n;
        }
        printf("%d\n",ct);
    }
    return 0;
}

jcx's Perfect Square

题目大意

季宸希有一个大小为 n×n的矩阵,里面填满了小写拉丁字母。 n的值是偶数。她想要改变一些字符,使得她的矩阵变成一个完美方阵。一个矩阵被称为完美方阵,如果它在顺时针旋转90度后仍然保持不变。在一次操作中,季宸希可以选择任意一个单元格,并将其值替换为字母表中的下一个字符。如果字符等于"z",则它的值不会改变。找出使得矩阵成为完美方阵所需的最小操作次数。

题目分析

首先拿到这题,我们第一个想到的解法就是爆搜!!!

但是吧,看到数据范围,我们会发现搜索会T在第一个点,T的连渣都不剩

查奕辰:连我都不剩?

所以,我们发现,大概率要用 O (n^2) 的算法了。

我们仔细分析就会发现中心对称的四个点都要一样才行!!!

这不就行了吗?

ACCode

//made by wjb
#include<iostream>
#include<string>
using namespace std;
int n;
string grid[1005];
int t;
int main(){
    cin>>t;
    while(t--){
        cin>>n;
        for(int i = 0;i < n;i++) cin>>grid[i];
        int ans=0;
        for(int i = 0;i < n/2;i++){
            for(int j = 0;j < n/2;j++){
                int temp=4*max(grid[i][j],max(grid[j][n-i-1],max(grid[n-j-1][i],grid[n-i-1][n-j-1]))) \
                -grid[i][j]-grid[n-i-1][n-j-1]-grid[j][n-i-1]-grid[n-j-1][i];
                ans+=temp;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

总结

到此为止