“科大讯飞杯”第十七届同济大学程序设计预选赛暨高校网络友谊赛题解

题目链接

按难度顺序

F. 排列计算

题意:



题解:



AC代码

/*
    Author:zzugzx
    Lang:C++
    Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

ll a[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        int l,r;
        cin>>l>>r;
        a[l]++,a[r+1]--;
    }
    for(int i=1;i<=n;i++)a[i]+=a[i-1];
    sort(a+1,a+1+n);
    ll ans=0;
    for(ll i=1;i<=n;i++){
        ans+=i*a[i];
    }
    cout<<ans;
    return 0;
}

B. 伤害计算

题意:




题解:












AC代码

/*
    Author:zzugzx
    Lang:C++
    Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};


int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
   string s;
    cin>>s;
    double ans=0;
    int i=0;
    while(i<s.length()){
        double a=0,b=0;
        while(isdigit(s[i])&&i<s.length())a=a*10+s[i]-'0',i++;
        if(i==s.length()){ans+=a;break;}
        if(s[i]!='d'){ans+=a;i++;continue;}
        i++;
        while(isdigit(s[i])&&i<s.length())b=b*10+s[i]-'0',i++;
        i++;
        ans+=a*(b+1)/2;
    }
    ll res=ans;
    if(res==ans)cout<<res;
    else printf("%.1lf",ans);
    return 0;
}

A.张老师和菜哭武的游戏

题意:




题解:






AC代码

/*
    Author:zzugzx
    Lang:C++
    Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

ll a[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;
    cin>>t;
    while(t--){
        int n,a,b;
        cin>>n>>a>>b;
        if((n/__gcd(a,b))&1)cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

D.车辆调度

题意:






题解:











AC代码

/*
    Author:zzugzx
    Lang:C++
    Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int n,m,k;
char g[20][20];
set<pii> e;
vector<pii> s;
bool ok(int x,int y){
    if(x>=1&&x<=n&&y>=1&&y<=m&&g[x][y]=='.')return 1;
    return 0;
}
void dfs(int cur){
    if(cur==k)return;
    for(auto &i:s)
        for(int p=0;p<4;p++){
            int cx=i.fi,cy=i.se;
            int x=i.fi,y=i.se;
            int dx=x+dir[p][0];
            int dy=y+dir[p][1];
            while(ok(dx,dy))x=dx,y=dy,dx+=dir[p][0],dy+=dir[p][1];
            if(e.count(mp(x,y))){cout<<"YES";exit(0);}
            swap(g[cx][cy],g[x][y]);
            i.fi=x,i.se=y;
            dfs(cur+1);
            i.fi=cx,i.se=cy;
            swap(g[cx][cy],g[x][y]);
        }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    cin>>m>>n>>k;
    for(int i=1;i<=n;i++)cin>>g[i]+1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            if(g[i][j]=='R')s.pb(mp(i,j));
            if(g[i][j]=='D')e.insert(mp(i,j)),g[i][j]='.';
        }
    dfs(0);
    cout<<"NO"<<endl;
    return 0;
}

E.弦

题意:

题解:










AC代码

在这里插入代码片`/*
    Author:zzugzx
    Lang:C++
    Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

ll Pow(ll a, ll b){
    ll ans = 1;
    while(b > 0){
        if(b & 1){
            ans = ans * a % mod;
        }
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
//逆元
ll inv(ll b){
    return Pow(b,mod-2);
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ll n;
    cin>>n;
    ll a=Pow(2,n),b=1;
    for(ll i=1;i<=n+1;i++)b=b*i%mod;
    ll ans=a*inv(b)%mod;
    cout<<ans;
    return 0;
}
`

C. 张老师的旅行

题意:






题解:


















AC代码

/*
    Author:zzugzx
    Lang:C++
    Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod=1e9+7;
//const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

int dp[1010][1010][2],a[1010],t[1010];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    int pos;
    for(int i=1;i<=n;i++){
        cin>>t[i];
        if(t[i]==0)pos=i;
    }
    memset(dp,inf,sizeof dp);
    dp[pos][pos][0]=dp[pos][pos][1]=0;
    for(int len=1;len<=n;len++)
        for(int l=max(1,pos-len+1),r;l<=pos&&l+len-1<=n;l++){
            r=l+len-1;
            if(dp[l+1][r][0]+a[l+1]-a[l]<=t[l])
                dp[l][r][0]=min(dp[l][r][0],dp[l+1][r][0]+a[l+1]-a[l]);
            if(dp[l+1][r][1]+a[r]-a[l]<=t[l])
                dp[l][r][0]=min(dp[l][r][0],dp[l+1][r][1]+a[r]-a[l]);
            if(dp[l][r-1][1]+a[r]-a[r-1]<=t[r])
                dp[l][r][1]=min(dp[l][r][1],dp[l][r-1][1]+a[r]-a[r-1]);
            if(dp[l][r-1][0]+a[r]-a[l]<=t[r])
                dp[l][r][1]=min(dp[l][r][1],dp[l][r-1][0]+a[r]-a[l]);
        }
    int ans=min(dp[1][n][0],dp[1][n][1]);
    if(ans==inf)cout<<-1;
    else cout<<ans;
    return 0;
}

J.斐波那契和

题意:


题解:






模版来自:https://www.cnblogs.com/zzqsblog/p/6877339.html

/*
    Author:zzugzx
    Lang:C++
    Blog:blog.csdn.net/qq_43756519
*/
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
//const int mod=1e9+7;
const int mod=998244353;
const double eps = 1e-10;
const double pi=acos(-1.0);
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f;
const int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};


const int MOD=998244353;
const int SZ=maxn;
ll qp(ll a,ll b)
{
    ll x=1; a%=MOD;
    while(b)
    {
        if(b&1) x=x*a%MOD;
        a=a*a%MOD; b>>=1;
    }
    return x;
}
namespace linear_seq {
inline vector<ll> BM(vector<ll> x)
{
    vector<ll> ls,cur;
    int pn=0,lf,ld;
    for(int i=0;i<int(x.size());++i)
    {
        ll t=-x[i]%MOD;
        for(int j=0;j<int(cur.size());++j)
            t=(t+x[i-j-1]*(ll)cur[j])%MOD;
        if(!t) continue;
        if(!cur.size())
        {cur.resize(i+1); lf=i; ld=t; continue;}
        ll k=-t*qp(ld,MOD-2)%MOD;
        vector<ll> c(i-lf-1); c.pb(-k);
        for(int j=0;j<int(ls.size());++j) c.pb(ls[j]*k%MOD);
        if(c.size()<cur.size()) c.resize(cur.size());
        for(int j=0;j<int(cur.size());++j)
            c[j]=(c[j]+cur[j])%MOD;
        if(i-lf+(int)ls.size()>=(int)cur.size())
            ls=cur,lf=i,ld=t;
        cur=c;
    }
    vector<ll>&o=cur;
    for(int i=0;i<int(o.size());++i)
        o[i]=(o[i]%MOD+MOD)%MOD;
    return o;
}
int N; ll a[SZ],h[SZ],t_[SZ],s[SZ],t[SZ];
inline void mull(ll*p,ll*q)
{
    for(int i=0;i<N+N;++i) t_[i]=0;
    for(int i=0;i<N;++i) if(p[i])
        for(int j=0;j<N;++j)
            t_[i+j]=(t_[i+j]+p[i]*q[j])%MOD;
    for(int i=N+N-1;i>=N;--i) if(t_[i])
        for(int j=N-1;~j;--j)
            t_[i-j-1]=(t_[i-j-1]+t_[i]*h[j])%MOD;
    for(int i=0;i<N;++i) p[i]=t_[i];
}
inline ll calc(ll K)
{
    for(int i=N;~i;--i) s[i]=t[i]=0;
    s[0]=1; if(N!=1) t[1]=1; else t[0]=h[0];
    for(;K;mull(t,t),K>>=1) if(K&1) mull(s,t); ll su=0;
    for(int i=0;i<N;++i) su=(su+s[i]*a[i])%MOD;
    return (su%MOD+MOD)%MOD;
}
inline int gao(vector<ll> x,ll n)
{
    if(n<int(x.size())) return x[n];
    vector<ll> v=BM(x); N=v.size(); if(!N) return 0;
    for(int i=0;i<N;++i) h[i]=v[i],a[i]=x[i];
    return calc(n);
}
}
ll fff[3000]={1,1,1};
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ll n,k;
    cin>>n>>k;
    vector<ll> qqq;
    qqq.pb(0);
    for(int i=3;i<2000;i++)
        fff[i]=(fff[i-1]+fff[i-2])%MOD;
    for(int i=1;i<=1000;i++){
        ll sss=0;
        for(int j=1;j<=i;j++)
            sss=(sss+qp(j,k)*fff[j])%MOD;
        qqq.pb(sss);
    }
    cout<<linear_seq::gao(qqq,n);
    return 0;
}
全部评论

相关推荐

勇敢的联想人前程似锦:如果我是你,身体素质好我会去参军,然后走士兵计划考研211只需要200多分。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
1 1 评论
分享
牛客网
牛客企业服务