第四届“图灵杯”NEUQ-ACM程序设计竞赛部分题解

A

队友做的、

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL gcd(LL x,LL y) {
    return y==0?x:gcd(y,x%y);
}
double Jie(int n) {
    LL r1=(LL)(n+1),r2=(LL)(3*n+1);
    LL m1=(LL)n;
    LL m2=2*m1;
    LL gg;
    for(int i=0;i<n;i++) {
        r1*=m1;
        r2*=m2;
        gg=gcd(r1,r2);
        r1/=gg;
        r2/=gg;
        m1--;
        m2--;
    }
    return double(r1*1.0/r2);
}
 
int main() {
    int k;
    scanf("%d",&k);
    while(k--) {
        int n;
        scanf("%d",&n);
        printf("%.9lf\n",Jie(n));
    }
    return 0;
} 
/**************************************************************
    Problem: 1756
    User: T266
    Language: C++
    Result: 正确
    Time:0 ms
    Memory:944 kb
****************************************************************/


B

签到

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <string>
#include <cstring>
  
using namespace std;
#define LL long long
#define mst(a, b)   memset(a, b, sizeof a)
#define REP(i, x, n)    for(int i = x; i <= n; ++i)
const int qq = 2e5 + 10;
LL num[15];
 
int main(){
    int t;  scanf("%d", &t);
    while(t--){
        int Cas;    scanf("%d", &Cas);
        REP(i, 1, 10){
            scanf("%lld", num + i);
        }
        sort(num + 1, num + 1 + 10);
        printf("%d %lld\n", Cas, num[8]);
    }
    return 0;
}
/**************************************************************
    Problem: 1757
    User: T266
    Language: C++
    Result: 正确
    Time:8 ms
    Memory:1528 kb
****************************************************************/

C

队友做的、

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
 
char fib[510][120],a[120],b[120];
int p[510][120];
 
void init()
{
    int i,j,k;
    memset(p,0,sizeof(p));
    p[1][0]=1;
    p[2][0]=2;
    for(i=3;i<=500;i++)
    {
        for(j=0;j<=110;j++)
        {
            p[i][j]=p[i][j]+p[i-1][j]+p[i-2][j];
            if(p[i][j]>9)
            {
                p[i][j+1]=p[i][j+1]+p[i][j]/10;
                p[i][j]=p[i][j]%10;
            }
        }
    }
    for(i=1;i<=500;i++)
    {
        for(j=110;j>=0;j--)
            if(p[i][j]!=0)
            break;
        k=0;
        for(;j>=0;j--)
            fib[i][k++]=p[i][j]+'0';
            fib[i][k]='\0';
    }
}
 
int cmp(char *a,char *b)
{
    int la,lb;
    la=strlen(a);
    lb=strlen(b);
    if(la!=lb)
        return la>lb?1:-1;
    else
        return strcmp(a,b);
}
 
int find(char *num,int &flag)
{
    int l,r,mid,s;
    l=1;
    r=480;
    while(l<=r)
    {
        mid=(l+r)/2;
        s=cmp(num,fib[mid]);
        if(s==0)
        {
            flag=1;
            return mid;
        }
        else
        if(s<0)
        r=mid-1;
        else
        if(s>0)
        l=mid+1;
    }
    return l;
}
 
int main()
{
    int l,r,flaga,flagb;
    init();
    while(scanf("%s%s",a,b)!=EOF)
    {
        if(strcmp(a,"0")==0&&strcmp(b,"0")==0)
            break;
        flaga=0;
        flagb=0;
        l=find(a,flaga);
        r=find(b,flagb);
        if(flagb)
            printf("%d\n",r-l+1);
        else
            printf("%d\n",r-l);
    }
    return 0;
}
 
/**************************************************************
    Problem: 1758
    User: T266
    Language: C++
    Result: 正确
    Time:4 ms
    Memory:1240 kb
****************************************************************/


E

因为是非递减序列,你可以记录一下从1到i与num[i]相同数的个数,那么我们在查询区间l,r的时候我们只需要查找第一个数出现的区间,然后剩下的可以rmq

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <string>
#include <cstring>
  
using namespace std;
#define LL long long
#define mst(a, b)   memset(a, b, sizeof a)
#define REP(i, x, n)    for(int i = x; i <= n; ++i)
const int qq = 1e5 + 10;
int num[qq], e[qq], dp[qq][40];
void Solve(int n){
    for(int i = 1; i <= n; ++i){
        dp[i][0] = num[i];
    }
    for(int j = 1; (1 << j) <= n; ++j){
        for(int i = 1; i + (1 << j) - 1 <= n; ++i)
            dp[i][j] = max(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
    }
}
int Check(int l, int r, int n){
    int k = 0;
    while((1 << (k + 1)) <= r - l + 1) k++;
    return max(dp[l][k], dp[r - (1 << k) + 1][k]);
}
 
int main(){
    int n, q;
    while(scanf("%d", &n) != EOF){
        if(!n)  break;
        mst(num, 0);
        scanf("%d", &q);
        int pre = -1e9;
        REP(i, 1, n){
            int x;  scanf("%d", &x);
            if(x == pre)    num[i] = num[i - 1] + 1;
            else    pre = x, num[i] = 1;
            e[i] = x;
        }
        Solve(n);
        while(q--){
            int l, r;   scanf("%d%d", &l, &r);
            int k = upper_bound(e + 1 + l, e + 1 + r, e[l]) - e;
            int maxn = k - l;
            if(k < r){
                maxn = max(maxn, Check(k, r, n));
            }else if(k == r){
                maxn = max(maxn, 1);
            }
            printf("%d\n", maxn);
        }
    }
    return 0;
}
/**************************************************************
    Problem: 1760
    User: T266
    Language: C++
    Result: 正确
    Time:796 ms
    Memory:17932 kb
****************************************************************/

F

很明显的矩阵快速幂,不过得优化矩阵

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <string>
#include <cstring>
  
using namespace std;
#define LL long long
#define mst(a, b)   memset(a, b, sizeof a)
//#define REP(i, x, n) for(int i = x; i <= n; ++i)
const int qq = 100 + 10;
const int MOD = 1000000007;
LL n, k;
struct Rec{
    LL mar[qq][qq];
    Rec operator * (Rec &d){
        Rec tmp;
        mst(tmp.mar, 0);
        for(int i = 1; i <= n; ++i){
            for(int l = 1; l <= n; ++l){
                if(d.mar[i][l] == 0)    continue;
                for(int j = 1; j <= n; ++j){
                    tmp.mar[i][j] = (tmp.mar[i][j] + (d.mar[i][l] * mar[l][j]) % MOD + MOD) % MOD;
                    tmp.mar[i][j] %= MOD;
                }
            }
        }
        return tmp;
    }
}tmp, ans;
LL num[105];
void Solve(){
    LL p = k - n;
    mst(ans.mar, 0);
    for(int i = 1; i <= n; ++i){
        ans.mar[i][i] = 1;
    }
    while(p > 0){
        if(p & 1)   ans = ans * tmp;
        tmp = tmp * tmp;
        p >>= 1;
    }
}
LL Quick_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 t[105];
 
int main(){
        while(scanf("%lld%lld", &n, &k) != EOF){
            for(int i = 1; i <= n; ++i){
                scanf("%lld", num + i);
                num[i] %= MOD;
            }
            mst(tmp.mar, 0);
            for(int i = 1; i <= n; ++i){
                scanf("%lld", &tmp.mar[1][i]);
            }
            for(int i = 2; i <= n; ++i)
                tmp.mar[i][i - 1] = 1;
            Solve();
            LL d = 0;
            for(int i = 1; i <= n; ++i){
                d = (d + ((ans.mar[1][i] * num[n - i + 1]) % MOD + MOD) % MOD) % MOD;
                d %= MOD;
            }
            printf("%lld\n", d);
        }
    return 0;
}
/**************************************************************
    Problem: 1768
    User: T266
    Language: C++
    Result: 正确
    Time:672 ms
    Memory:1720 kb
****************************************************************/


I

签到

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <string>
#include <cstring>
  
using namespace std;
#define LL long long
#define mst(a, b)   memset(a, b, sizeof a)
#define REP(i, x, n)    for(int i = x; i <= n; ++i)
const int qq = 2e5 + 10;
string st;
 
int main(){
    getline(cin, st);
//  cout << st << endl;
    if(st == "I have some question!")   cout << "What can I do for you?" << endl;
    else    cout << "Yes, welcome to NEUQ." << endl;
    return 0;
}
/**************************************************************
    Problem: 1771
    User: T266
    Language: C++
    Result: 正确
    Time:0 ms
    Memory:1524 kb
****************************************************************/


J

xjb搞一下,开始WA了几发,可是过了,我不知道自己错哪里

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <utility>
#include <string>
#include <cstring>
  
using namespace std;
#define LL long long
#define mst(a, b)   memset(a, b, sizeof a)
#define pill pair<int, int>
#define REP(i, x, n)    for(int i = x; i <= n; ++i)
const int qq = 2e5 + 10;
map<string, int> id;
vector<string> vt[300005];
struct Node{
    string x;
    int mount;
    int f;
    bool operator < (const Node &d)const{
        if(mount == d.mount)    return vt[id[x]][0] < vt[id[d.x]][0];
        return mount > d.mount;
    }
}node[300005];
 
int main(){
    string x, y;
    int cnt = 1;
    while(cin >> x){
        y = x;
        sort(y.begin(), y.end());
//      cout << y << endl;
        if(!id[y]){
            id[y] = cnt++;
        }
        vt[id[y]].push_back(x);
    }
    map<string, int>::iterator it;
    int tot = 0;
    for(it = id.begin(); it != id.end(); ++it){
        node[tot].x = it->first;
        node[tot].f = it->second;
        node[tot].mount = (int)vt[node[tot].f].size();
        tot++;
    }
    for(int i = 1; i < cnt; ++i)
        sort(vt[i].begin(), vt[i].end());
    sort(node, node + tot);
    for(int i = 0; i < tot && i < 5; ++i){
        int f = node[i].f;
        printf("Group of size %d: ", node[i].mount);
        int k = unique(vt[f].begin(), vt[f].end()) - vt[f].begin();
        for(int j = 0; j < k; ++j)
            cout << vt[f][j] << " ";
        puts(".");
    }
    return 0;
}
/**************************************************************
    Problem: 1749
    User: T266
    Language: C++
    Result: 正确
    Time:808 ms
    Memory:9908 kb
****************************************************************/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值