跳到正文

算法·理论

联赛D7

acwatlemlecere约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月6日
篇幅
约 7 分钟

版本

  1. v1当前

T1:

C++
#include<bits/stdc++.h>
using namespace std;
#define fo freopen("edit.in","r",stdin);freopen("edit.out","w",stdout);
#define fc fclose(stdin);fclose(stdout);
struct bigint{//ؤ°æ¸ß¾«
    static const int BASE=100000000;
    static const int WIDTH=8;
    vector<long long>s;
    bigint(){
        *this=0;
    }
    bigint(const int& num) {
        *this=num;
    }
    bigint operator=(int num) {
        s.clear();
        do {
            s.push_back(num%BASE);
            num/=BASE;
        } while(num>0);
        return *this;
    }
    bigint operator = (const string& str) {
        s.clear();
        int x,len=(str.length()-1)/WIDTH+1;
        for(int i=0; i<len; i++) {
            int end=str.length()-i*WIDTH;
            int start=max(0,end-WIDTH);
            sscanf(str.substr(start,end-start).c_str(),"%lld",&x);
            s.push_back(x);
        }
        return *this;
    }
    bool operator < (const bigint& b) {
        if(s.size()<b.s.size())return true;
        if(s.size()>b.s.size())return false;
        for(int i=s.size()-1; i>=0; i--) {
            if(s[i]<b.s[i])return true;
            if(s[i]>b.s[i])return false;
        }
        return false;
    }
    bool operator >= (const bigint& b) {
        return !(*this<b);
    }
    bool operator == (const bigint& b) {
        if(s.size()!=b.s.size())return false;
        for(int i=0; i<s.size(); i++)
            if(s[i]!=b.s[i])return false;
        return true;
    }
    bigint operator + (const bigint& b) {
        bigint c;
        c.s.clear();
        for(int i=0,g=0; ; i++) {
            if(g==0&&i>=s.size()&&i>=b.s.size())break;
            int x=g;
            if(i<s.size())x+=s[i];
            if(i<b.s.size())x+=b.s[i];
            c.s.push_back(x%BASE);
            g=x/BASE;
        }
        return c;
    }
    friend istream& operator >> (istream& input,bigint& x) {
        string s;
        if(!(input>>s))return input;
        x=s;
        return input;
    }
    friend ostream& operator << (ostream& output,const bigint& x) {
        output<<x.s.back();
        for(int i=x.s.size()-2; i>=0; i--) {
            char buf[20];
            sprintf(buf,"%08d",x.s[i]);
            for(int j=0; j<strlen(buf); j++)output<<buf[j];
        }
        return output;
    }
};
int kk,n,bk;
bigint ans;
bool fst;
void dfs(int k,bigint tmp,string res){
	bigint sum;
	if(k==kk){
//		cout<<"1\n\n\n";
		sum=res;
//		cout<<sum<<','<<tmp<<',';
		sum=sum+tmp;
//		cout<<sum<<',';
		if((ans>=sum)||(fst)){
			ans=sum;
			fst=0;
		}
//		cout<<ans<<"\n\n\n\n\n"<<endl;
		return;
	}
	int l=res.size();
	for(int i=max(1,bk-1);i<min(l,bk+1);i++){
		sum=res.substr(0,i);
//		cout<<sum<<','<<res.substr(i,l-i+1)<<endl<<"\n\n\n";
		if((ans<tmp+sum)&&(!fst))continue;
		dfs(k+1,tmp+sum,res.substr(i,l-i+1));
	}
}
void solve(){
	fst=1;
	ans=0;
	string s;
	cin>>n>>kk>>s;
	bk=n/(kk+1);
	bigint a;
	dfs(0,a,s);
	cout<<ans<<endl;
}
int main(){fo
	int T;
	cin>>T;
	for(int i=1;i<=T;i++){
		solve();
	}fc
}

T2:

C++
#include<bits/stdc++.h>
using namespace std;
#define ll longlong
#define MOD 998244353
int n,m;
ll ksm(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;
}
int main(){
	cin>>n>>m;
	cout<<ksm(2,(((n-1)*n)/2)-m);
	return 0;
}

T3:

C++

T4:

C++

Test:

C++
原文