个人题解 |

三角形

https://ac.nowcoder.com/acm/contest/85347/A

略抽象的一场

A.

签到

#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define li __int128
#define pll pair<ll,ll>
ll a[200005];

int main(){
    map<int,int>mp;
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
        mp[a[i]]++;
        if(mp[a[i]]>=3){
            cout<<"YES";
            return 0;
        }
	}
    cout<<"NO";
	return 0;
}

B

签到,容易知道只要没有0就符合

#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define li __int128
#define pll pair<ll,ll>
ll a[200005];

int main(){
    int n;cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]==0){
            cout<<"NO";
            return 0;
        }
    }
    cout<<"YES";
	return 0;
}

C

首先原数列是正整数树列,意味着其前缀和序列是严格单调上升的,0<si说明si序列不含0;

假设大于0的平方数有x个,显然我们要做的就是从这里面挑n个数字组成一个严格单增的序列,组合数C(x,n)即可

#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define li __int128
#define pll pair<ll,ll>

const ll mod=1e9+7;
const int N=1e5+10;
ll f[N],invf[N];

ll pows(ll a,ll n){
    ll ans=1;
    while(n){
        if(n&1) ans=ans*a%mod;
        a=a*a%mod;n>>=1;
    }
    return ans;
}

void init(){
    f[1]=1;
    invf[1]=pows(f[1],mod-2);
    for(int i=2; i<N; i++){
        f[i]=f[i-1]*i%mod;invf[i]=pows(f[i],mod-2);
    }
}

ll c(ll a,ll b){
    if(a==b) return 1;
    return f[a]*invf[a-b]%mod*invf[b]%mod;
}

int main(){
    init();
    ll n,x;cin>>n>>x;
    ll rig=0,sum=0;
    for(ll i=1;i<=x;i++){
       if(i*i<=x){
           rig++;
       }
    }
    if(rig<n){
        cout<<0<<'\n';
        return 0;
    }
    cout<<c(rig,n);
	return 0;
}

D

一个非常重要的思维:b[i]数组是没有用的,对于,无论i怎么取结果都是1

这意味着我们只看a就可以

然后看一眼数据范围,bfs就完了

#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cmath>
#include<algorithm>
#include <cstring>
using namespace std;
#define ll long long
#define li __int128
#define pll pair<ll,ll>

int dirc[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int bfs(const vector<vector<int>>& maze, int p) {
    int n = maze.size();
    int m = maze[0].size();
    queue<tuple<int, int, int, int>> q;
    int steps[n][m][p-1];
    
    memset(steps, -1, sizeof(steps));
    
    q.emplace(0, 0, maze[0][0] % (p - 1), 0);
    steps[0][0][maze[0][0] % (p - 1)] = 0;

    while (!q.empty()) {
        auto [x, y, c, step] = q.front();
        q.pop();
        if (x == n - 1 && y == m - 1 && c == 0) {
            return step;
        }
        for (auto& dir : dirc) {
            int nx = x + dir[0];
            int ny = y + dir[1];
            if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
                int new_c = (c + maze[nx][ny]) % (p - 1);
                if (steps[nx][ny][new_c] == -1 || step + 1 < steps[nx][ny][new_c]) {
                    q.emplace(nx, ny, new_c, step + 1);
                    steps[nx][ny][new_c] = step + 1;
                }
            }
        }
    }
    return -1;
}

int main() {
    int n, m, p;
    cin >> n >> m >> p;
    vector<vector<int>> maze(n, vector<int>(m));

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            cin >> maze[i][j];
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            int xxx;
            cin >> xxx;
        }
    }
    int res = bfs(maze, p);
    if (res == -1) {
        cout << "-1";
    } 
    else {
        cout << res;
    }

    return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
找个工作&nbsp;学历是要卡的&nbsp;要求是高的&nbsp;技能不足是真的&nbsp;实习经验是0的&nbsp;简历无处可写是事实的&nbsp;钱不好赚是真的&nbsp;想躺平又不敢躺&nbsp;也不甘心躺&nbsp;怕自己的灵感和才华被掩埋甚至从未被自己发现&nbsp;又质疑自己是否真正有才华
码农索隆:你现在啊,你心里都明白咋回事,但是你没办法改变现状,一想到未来,你又没有信心狠下心来在当下努力。 得走出这种状态,不能一直困在那里面,哪不行就去提升哪,你一动不动那指定改变不了未来,动起来,积少成多才能越来越好
点赞 评论 收藏
分享
05-26 09:07
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务