牛客练习赛107

题目链接:https://ac.nowcoder.com/acm/contest/49035

A

当 $n!<=m$ 时,$(n!)!$ 直接代入求解

当 $n!>m$ 时,$(n!)!\%m=0$

#include <bits/stdc++.h>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define nep(i, r, l) for (int i = r; i >= l; i--)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,int> PLI;
const int INF=0x3f3f3f3f,MOD=1000000007,P=131,N=1000010,M=2*N;

int n,m;
LL fact[N];

int main()
{
    int T; cin>>T>>m;
    fact[0]=1;
    int k=0;
    rep(i,1,1000000)
    {
        fact[i]=fact[i-1]*i%m;
        if(k==0 && fact[i-1]*i>m) k=i;
    }

    while(T--)
    {
        int x; cin>>x;
        if(x>=k) cout<<0<<endl; //fact[x]>m
        else cout<<fact[fact[x]]<<endl;
    }
    return 0;
}

B

$Q$ 数列第一个元素只能是1或2,剩下的直接构造

#include <bits/stdc++.h>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define nep(i, r, l) for (int i = r; i >= l; i--)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,int> PLI;
const int INF=0x3f3f3f3f,MOD=1000000007,P=131,N=1500010,M=2*N;

int n, p[N];

int main()
{
    int T; cin>>T;
    while(T--)
    {
        scanf("%d", &n);
        vector<int> res(n+10, 0);
        rep(i,1,n) {
            scanf("%d",&p[i]);
        }
        res[p[1]]=1;
        if(p[1]!=1) res[1]=2;
        for(int i=1, k=res[1]+1;i<=n;i++) {
            if(res[i]) continue;
            res[i]=k++;
        }
        
        rep(i,1,n) printf("%d ", res[i]);
        puts("");
    }
    return 0;
}

D

可以发现无论怎么操作两元素和不变。统计所有元素每个二进制位个数,把大的数分配在后面就行

#include <bits/stdc++.h>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define nep(i, r, l) for (int i = r; i >= l; i--)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,int> PLI;
const int INF=0x3f3f3f3f,MOD=1000000007,P=131,N=1500010,M=2*N;

int n;
int a[N], cnt[35];

int main()
{
    int T; cin>>T;
    while(T--)
    {
        memset(cnt, 0, sizeof cnt);
        scanf("%d", &n);
        rep(i,1,n){
            scanf("%d", &a[i]);
            rep(j,0,30)
            {
                if(a[i]>>j&1) cnt[j]++;
            }
        }

        vector<int> res;
        nep(i,n,1){
            int sum=0;
            rep(j,0,30){
                if(cnt[j]) {
                    sum+=1<<j;
                    cnt[j]--;
                }
            }
            res.push_back(sum);
        }
        
        for(int i=res.size()-1;i>=0;i--) printf("%d ", res[i]);
        puts("");
    }
    return 0;
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇