跳到正文

个人记录

比赛题解

Warm_zephyr约 57 分钟
原文
文章信息与版本

文章信息

收录
2026年1月2日
更新
2026年2月14日
篇幅
约 57 分钟

版本

  1. v2当前
  2. v1

U541755 抽象函数

C++
#include<bits/stdc++.h>
using namespace std;
long long k;
short f[6] = {-1, -1, 0, 1, 1, 0};
int main()
{
    cin >> k;
    cout << f[k % 6];
    return 0;
}
/*
f(1) = f(0) + f(2), 即 f(1) = f(0) + 1; 又 f(x + 1) 为奇函数, 所以 f(0) = -f(2) = -1, 那么 f(1) = 0;
由 f(2) = f(3) + f(1) 得 f(3) = 1; 同理 f(4) = 0, f(5) = -1, f(6) = -1, f(7) = 0, f(8) = 1......
因此, 我们知道 f(x) 的周期为 6
f(x) = f(x % 6);
*/

U546490 自幂数的判断

C++
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll num[210];
short wei[210];
bool compare(ll n1,ll n2)
{
    return n1>n2;
}
ll power(ll n1,short n2)
{
    ll result = 1;
    for (short i=0;i<n2;++i)
        result*=n1;
    return result;
}
char isSelf_Power(ll n,short w)
{
    ll m=n,sum=0;
    while(m)
    {
        sum+=power(m%10,w);
        m/=10;
    }
    if(n==sum) return 'v';
    return 'x';
}
int main()
{
    char str[1350];
    short i=1,cnt=0;
    while((str[i]=getchar())!=EOF)
    {
        if(isdigit(str[i]))
        {
            num[cnt]=num[cnt]*10+str[i]-'0';
            wei[cnt]++;
        }
        else
            if(isdigit(str[i-1]))
                cnt++;
        i++;
    }
    if(isdigit(str[i-1])) cnt++;
    if(cnt==0)
    {
        cout<<"NO";
        return 0;
    }
    sort(num,num+cnt,compare);
    sort(wei,wei+cnt,compare);
    for(short j=0;j<cnt-1;j++)
        cout<<isSelf_Power(num[j],wei[j])<<","<<'\n';
    cout<<isSelf_Power(num[cnt-1],wei[cnt-1])<<"。";
    return 0;
}

网友优秀做法 11:@Penn_Li

C++
#include <iostream>
#include <cctype>
#include <vector>
#include <algorithm>
#include <string>
#include <cstdio>
using namespace std;

string add(string a, string b) {
    if (a == "0") return b;
    if (b == "0") return a;
    int i = a.size() - 1, j = b.size() - 1;
    int carry = 0;
    string res = "";
    while (i >= 0 || j >= 0 || carry) {
        int x = (i >= 0) ? (a[i] - '0') : 0;
        int y = (j >= 0) ? (b[j] - '0') : 0;
        i--; j--;
        int s = x + y + carry;
        carry = s / 10;
        res = char(s % 10 + '0') + res;
    }
    return res;
}

string mul(string a, int d) {
    if (d == 0) return "0";
    if (d == 1) return a;
    string res = "";
    int carry = 0;
    for (int i = a.size() - 1; i >= 0; i--) {
        int digit = a[i] - '0';
        int product = digit * d + carry;
        carry = product / 10;
        res = char(product % 10 + '0') + res;
    }
    if (carry) {
        res = char(carry + '0') + res;
    }
    return res;
}

string removeLeadingZeros(string s) {
    int i = 0;
    while (i < s.size() && s[i] == '0') i++;
    if (i == s.size()) return "0";
    return s.substr(i);
}

string pow_table[10][61];

void precompute() {
    for (int i = 0; i < 10; i++) {
        pow_table[i][0] = "1";
        for (int j = 1; j <= 60; j++) {
            pow_table[i][j] = mul(pow_table[i][j - 1], i);
        }
    }
}

bool cmp(const pair<string, bool> &a, const pair<string, bool> &b) {
    string s1 = a.first;
    string s2 = b.first;
    if (s1.length() != s2.length()) {
        return s1.length() > s2.length();
    }
    return s1 > s2;
}

int main() {
    precompute();
    string total;
    char c;
    while ((c = getchar()) != EOF) {
        total += c;
    }
    vector<string> num_strs;
    string current = "";
    for (char c : total) {
        if (isdigit(c)) {
            current += c;
        } else {
            if (!current.empty()) {
                num_strs.push_back(current);
                current = "";
            }
        }
    }
    if (!current.empty()) {
        num_strs.push_back(current);
    }
    if (num_strs.empty()) {
        cout << "NO" << endl;
        return 0;
    }
    vector<pair<string, bool>> numbers;
    for (string s : num_strs) {
        string clean = removeLeadingZeros(s);
        int n = clean.length();
        if (n > 60) {
            numbers.push_back({clean, false});
            continue;
        }
        string sum = "0";
        for (char c : clean) {
            int d = c - '0';
            string p = pow_table[d][n];
            sum = add(sum, p);
        }
        numbers.push_back({clean, (sum == clean)});
    }
    sort(numbers.begin(), numbers.end(), cmp);
    int n = numbers.size();
    for (int i = 0; i < n; i++) {
        if (numbers[i].second) {
            if (i == n - 1) {
                cout << "v。\n";
            } else {
                cout << "v,\n";
            }
        } else {
            if (i == n - 1) {
                cout << "x。\n";
            } else {
                cout << "x,\n";
            }
        }
    }
    return 0;
}

网友优秀做法 22,@convie:

C++
#include<iostream>
#include<cstring>
#include<algorithm>
#include <chrono>
#include <random>
#include<unordered_map>
#include<vector>
using namespace std;
#define int long long
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
// #define MAXN 1000010
const double eps=1e-6;
//随机数生成
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
    std::uniform_int_distribution<int> distribution(l, r);
    return distribution(rng);
}

int qmi(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=ans*a;
        a=a*a;
        b>>=1;
    }
    return ans;
}
// string q[MAXN];
struct node{
    int x;
    bool flag;
};
vector<node> v;
bool check(int x){
    int y=x;
    // cout<<x<<"%"<<"\n";
    int ans=0;
    int cn=0;
    while(y){
        cn++;
        y/=10;
    }
    // cout<<cn<<"\n";
    y=x;
    while(y){
        if(y%10)ans+=qmi(y%10,cn);
        y/=10;
    }
    // cout<<ans<<"\n";
    if(ans==x)return true;
    else return false;
}
bool cmp(node a,node b){
    return a.x>b.x;
}
void solve(){
	int a;
	int cnt=0;
    string s;
    bool flag=0;
	while(cin>>s){
        int a=0;
        int id=0;
        s=s+"&";
        bool flag1=0;
        for(int i=0;i<s.size();i++){
            if(s[i]>='0'&&s[i]<='9'){
                // bool cn=1;
                flag1=1;
                if(flag){
                    flag=0;
                    // cout<<","<<"\n";
                }
                a*=10;
                a+=s[i]-'0';
            }else {
                if(!flag1)continue;
                flag=1;
                flag1=0;
                cnt++;
                // cout<<a<<"\n";
                if(check(a)){
                    v.push_back({a,1});
                }
                else v.push_back({a,0});
                a=0;
            }
        }
	}
    if(cnt==0)cout<<"NO";
    else {
        sort(v.begin(),v.end(),cmp);
        // for(int i=0;i<v.size();i++)
        //     cout<<v[i].x<<" ";
        // cout<<endl;
        bool flag=0;
        for(int i=0;i<v.size();i++){
            if(flag)cout<<","<<"\n";
            flag=1;
            if(v[i].flag)cout<<"v";
            else cout<<"x";
        }
        cout<<"。";
    }
}

signed main(){
	ios;
	int t=1;
//	cin>>t;
	while(t--)
	solve();
	return 0;
}

U535472 完全数(完美数)【升级版】

C++
#include <bits/stdc++.h>
using namespace std;
long long perfect[9];
bool isPrime(long long n)
{
    if(n==2) return true;
	for(long long i=3;i*i<=n;i+=2)
		if(n%i==0) return false;
	return true;
}
int main()
{
    long long x,y;
    cin>>x>>y;
    long long a=min(x,y),b=max(x,y);
    short count=0;
    long long t,m;
    int maxn=(log(1+(sqrt(8)*sqrt(LLONG_MAX)))/log(2))-1;
    for(short i=2;i<=maxn;i+=(i==2)?1:2)
    {
    	t=(1l<<i)-1;
    	if(isPrime(i) && isPrime(t))
    	{
			m=(1l<<(i-1))*t;
			if(m<=b && a<=m)
                perfect[count++]=m;
    	}
	}
    if(count==0)
    {
        cout<<"NO";
        return 0;
    }
    for(short i=0;i<count;i++)
        cout<<perfect[i]<<(perfect[i+1]?",":"。");
    return 0;
}

网友优秀做法 11,@Antony_pico:

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool isPrime(ll n){
    if(n == 2) return true;
    for(ll i = 2; i * i <= n; i++) if(n % i == 0) return false;
    return true;
}
ll a, b, st, en;
int main(){
    cin >> a >> b;
    st = min(a, b), en = max(a, b);
    string ans;
    for(ll p = 2; p <= 31; p++){
        ll p2 = (1ll << p);
        if(isPrime(p) && isPrime(p2 - 1)){
            ll n = (p2 - 1) * (p2 >> 1);
            if(n >= st && n <= en) ans += to_string(n) + ",";
        }
    }
    if(!ans.empty()){
        ans.pop_back();
        cout << ans << "。";
    } else cout << "NO";
    return 0;
}

网友优秀做法 22,@封禁用户:

C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool isP(long long n)
{
    if(n<=1)
    {
        return false;
    }
    if(n==2)
    {
        return true;
    }
    if(n%2==0)
    {
        return false;
    }
    for(long long i=3;i*i<=n;i+=2)
    {
        if(n%i==0)
        {
            return false;
        }
    }
    return true;
}
vector<long long> genPN(long long a, long long b)
{
    vector<long long> pns;
    vector<int> ps={2, 3, 5, 7, 13, 17, 19, 31};
    for (int p : ps)
    {
        long long m=(1LL<<p)-1;
        if(isP(m))
        {
            long long pn=(1LL<<(p-1))*m;
            if(pn>=a&&pn<=b)
            {
                pns.push_back(pn);
            }
        }
    }
    return pns;
}
int main()
{
    long long a,b;
    cin>>a>>b;
    long long s=min(a,b);
    long long e=max(a,b);
    vector<long long> pns=genPN(s,e);
    if(pns.empty())
    {
        cout<<"NO"<<endl;
    }else{
        sort(pns.begin(),pns.end());
        for(size_t i=0;i<pns.size();++i)
        {
            cout<<pns[i];
            if(i!=pns.size()-1)
            {
                cout<<",";
            }else{
                cout<<"。";
            }
        }
        cout<<endl;
    }
    return 0;
}

网友优秀做法 33,@convie:

C++
#include<iostream>
#include<cstring>
#include<algorithm>
#include <chrono>
#include <random>
#include<unordered_map>
#include<vector>
using namespace std;
#define int long long
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
 #define MAXN 1000010
const double eps=1e-6;
//随机数生成
int primes[MAXN],cnt;
bool st[MAXN];
void gprimes(int n){
	for(int i=2;i<=n;i++){
		if(!st[i])primes[cnt++]=i;
		for(int j=0;primes[j]*i<=n;j++){
			st[i*primes[j]]=1;
			if(i%primes[j]==0)break;
		}
	}
}
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
    std::uniform_int_distribution<int> distribution(l, r);
    return distribution(rng);
}

int qmi(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=ans*a;
        a=a*a;
        b>>=1;
    }
    return ans;
}
unordered_map<int,int> mp;
int f[MAXN];
vector<__int128> v;
bool check(int x){
	for(int i=2;i*i<=x;i++)
	if(x%i==0)return false;
	return true;
}
void init(){
	for(int i=0;i<cnt;i++)
	if(check((1ll<<primes[i])-1)){
		__int128 cn=(__int128)((1ll<<primes[i])-1)*(1ll<<(primes[i]-1));
		v.push_back(cn);
	}
}
__int128 read() {
    string s;
    cin >> s;
    __int128 res = 0;
    int i = s[0] == '-' ? 1 : 0; // 处理负号并确定起始索引
    for (; i < s.size(); res = res * 10 + (s[i++] - '0'));
    return (s[0] == '-' ? -res : res);
}
//输出__int128整形数据
void print(__int128 x) {
    if (x < 0) { cout << '-'; print(-x); }
    else if (x >= 10) { print(x / 10); cout << (char)(x % 10 + '0'); }
    else cout << (char)(x + '0');
}
void solve(){
	// __int128 l,r;
	// l=read();
	// r=read();
    int l,r;
    cin>>l>>r;
	if(l>r)swap(l,r);
    // print(l);
    // cout<<"\n";
    // print(r);
    // cout<<"\n";
	bool flag=0;
	int cn=0;
	for(int i=0;i<v.size();i++)
	if(l<=v[i]&&v[i]<=r){
		cn++;
		if(flag)cout<<",";
		flag=1;
		print(v[i]);
	}
	if(cn)cout<<"。";
	else cout<<"NO";
}

signed main(){
	ios;
	int t=1;
	gprimes(35);
	init();
	while(t--)
	solve();
	return 0;
}

投机取巧(打表):

C++
#include <iostream>
using namespace std;
long long a,b,n[10],t=0;
long long f[10];
void NEW(){
    f[1]=6;
	f[2]=28;
	f[3]=496;
	f[4]=8128;
	f[5]=33550336;
	f[6]=8589869056;
	f[7]=137438691328;
	f[8]=2305843008139952128;
	return ;
}
int main(){
	cin>>a>>b;
	if(a>b)swap(a,b);
	NEW();
	for(int i=1;i<=8;i++)
	    if(f[i]>=a&&f[i]<=b){
	    	t++;
			n[t]=f[i];
		}
	if(t==0){
		cout<<"NO";
		return 0;
	}
	for(int i=1;i<t;i++)
	    cout<<n[i]<<",";
	cout<<n[t]<<"。";
	return 0;
}

U536513 给定区间内质数个数【升级版】

提供一种优化版的埃氏筛做法,理论上能卡线 AC。

C++
#include <bits/stdc++.h>
using namespace std;
bitset<1320000001> isComposite;
int main()
{
    int count,a,b;
    cin>>a>>b;
    isComposite[1]=1;
    for(int i=3;i*i<=b;i+=2)
    {
        while(!isComposite[i])
        {
            for(int j=i*i;j<=b;j+=2*i)
                isComposite[j]=1;
            break;
        }
    }
    count=(a<=2 && b>=2)?1:0;
    for(int k=(a%2==0)?(a+1):a;k<=b;k+=2)
    {
        while(!isComposite[k]) {++count;break;}
    }
    if(count==0)
    {
        cout<<"NO";
        return 0;
    }
    cout<<count;
    return 0;
}

网友优秀做法 11,@Antony_pico:

C++
#include <iostream>
#include <vector>
#include <cmath>
#include <cstring>
using namespace std;

typedef long long ll;

const int SEGMENT_SIZE = 1000000;
const int MAX_SQRT_B = 40000;

vector<int> basePrimes;
bool isBasePrime[MAX_SQRT_B + 1];

void sieveBase(int n) {
    for (int i = 2; i <= n; i++) {
        isBasePrime[i] = true;
    }
    for (int i = 2; i * i <= n; i++) {
        if (isBasePrime[i]) {
            for (int j = i * i; j <= n; j += i) {
                isBasePrime[j] = false;
            }
        }
    }
    for (int i = 2; i <= n; i++) {
        if (isBasePrime[i]) {
            basePrimes.push_back(i);
        }
    }
}

int main() {
    ll a, b;
    cin >> a >> b;
    if (a > b) {
        cout << "NO" << endl;
        return 0;
    }
    int sqrt_b = (int)sqrt(b) + 1;
    if (sqrt_b > MAX_SQRT_B) {
        sqrt_b = MAX_SQRT_B;
    }
    sieveBase(sqrt_b);

    ll count = 0;
    for (ll low = a; low <= b; low += SEGMENT_SIZE) {
        ll high = min(low + SEGMENT_SIZE - 1, b);
        bool* segment = new bool[SEGMENT_SIZE];
        memset(segment, true, SEGMENT_SIZE * sizeof(bool));

        if (low == 1) {
            segment[0] = false;
        }

        for (int p : basePrimes) {
            if (p * p > high) {
                break;
            }
            ll start = max((ll)p * p, low);
            if (start > high) {
                continue;
            }
            if (start % p != 0) {
                start += p - (start % p);
            }
            for (ll j = start; j <= high; j += p) {
                segment[j - low] = false;
            }
        }

        for (ll i = low; i <= high; i++) {
            if (segment[i - low]) {
                count++;
            }
        }
        delete[] segment;
    }

    if (count == 0) {
        cout << "NO" << endl;
    } else {
        cout << count << endl;
    }
    return 0;
}

网友优秀做法 22,@fhcp,@Fall_Dream:

C++
#include<bits/stdc++.h>
#define ctz __builtin_ctz
using namespace std;
const int N=1e5;
int pri[100010],cntpri;bool flg[100010];
int pi[100010],g[10010][21];
long long l,r,ans;
int limn,limk,cntw;
void init(){
    pri[0]=1;
    int sum=0;
    for(int i=2;i<=N;i++){
        if(!flg[i]){
            pri[++cntpri]=i;
            ++sum;
            if(1ll*i*i*i<=r)++cntw;
        }
        pi[i]=sum;
        for(int j=1;j<=cntpri&&pri[j]*i<=N;j++){
            flg[pri[j]*i]=1;
            if(i%pri[j]==0)break;
        }
    }
}
long long getg(long long x,long long k){
    if(x==0)return 0;
    if(k==0)return x-1;
    if(1ll*pri[k]*pri[k]>x){
        if(x<=N)return pi[x];
        k=pi[(int)sqrtl(x)];
    }
    if(x<=limn&&k<=limk)return g[x][k];
    return getg(x,k-1)-getg(x/pri[k],k-1)+pi[pri[k-1]];
}
long long getpi(long long x){
    if(x<=N)return pi[x];
    long long ans=getg(x,cntw);
    for(int i=cntw+1;i<=cntpri&&1ll*pri[i]*pri[i]<=x;i++)
        ans-=getpi(x/pri[i])-getpi(pri[i])+1;
    return ans;
}using ll = long long;
using u64 = unsigned long long;
using u128 = __uint128_t;
std::mt19937_64 rnd(std::chrono::steady_clock::now().time_since_epoch().count());
ll qp(ll a,ll b,ll p){ll r=1;for(;b;b>>=1,a=(u128)a*a%p) if(b&1)r=(u128)r*a%p;return r;}
inline int gcd(int a,int b){if(!b||!a) return 0;int az=ctz(a),bz=ctz(b),z=min(az,bz),d;b>>=bz;while(a) a>>=az,d=b-a,az=ctz(d),b=min(b,a),a=(d<0?-d:d);return b<<z;}
bool checkprime(ll p){for(int i=1;i<=5;i++){ll a=rnd()%(p-2)+2;if(gcd(a,p)!=1||qp(a,p-1,p)!=1)return 0;}return 1;}
int small_solve(ll l, ll r) {
    ll ans=0;
    if (l<=2&&2<=r) ans++; l=max(l,3ll);
    if(l>r)return ans;
    if(l%2==0) l++;
    for(ll i=l;i<=r;i+=2) ans+=checkprime(i);
    return ans;
}
int main(){
    cin>>l>>r;
    int ans;
    if(r-l<=10000) ans=small_solve(l,r);
    else{
        init();
        limn=min((long long)cbrt(r*sqrt(r))+5,(long long)1e4);
        limk=min(cntw,20);
        for(int i=1;i<=limn;i++){
            g[i][0]=i-1;
            for(int j=1;j<=limk;j++){
                if(1ll*pri[j]*pri[j]<=i)g[i][j]=g[i][j-1]-g[i/pri[j]][j-1]+pi[pri[j-1]];
                else g[i][j]=g[i][j-1];
            }
        }
        ans=getpi(r)-getpi(l-1);
    }
    if(ans==0) cout<<"NO";
    else cout<<ans;
    return 0;
}

一位大佬提供的目前最优解,拥有着惊人的速度,@_Kagamine_Rin_:

C++
#include<ios>
#include<vector>
#define int long long
using std::vector;
inline int isqrt(const int n){return __builtin_sqrtl(n);}
inline int prime_pi(const int N){
	if(N<=1)return 0;
	if(N==2)return 1;
	const int v=isqrt(N);
	int s=v+1>>1;
	vector<int>smalls(s);for(int i=1;i<s;++i)smalls[i]=i;
	vector<int>roughs(s);for(int i=0;i<s;++i)roughs[i]=(i<<1)+1;
	vector<int>larges(s);
	for(int i=0;i<s;++i)larges[i]=N/((i<<1)+1)-1>>1;
	vector<bool>skip(v+1);
	const auto divide=[](const int n,const int d)->int{return double(n)/d;};
	const auto half=[](const int n)->int{return n-1>>1;};
	int pc=0;
	for(int p=3;p<=v;p+=2)
		if(!skip[p]){
			int q=p*p;
			if((int)(q)*q>N)break;
			skip[p]=true;
			for(int i=q;i<=v;i+=p<<1)skip[i]=true;
			int ns=0;
			for(int k=0;k<s;++k){
				int i=roughs[k];
				if(skip[i])continue;
				int d=i*p;
				larges[ns]=larges[k]-(d<=v?larges[smalls[d>>1]-pc]:smalls[half(divide(N,d))])+pc;
				roughs[ns++]=i;
			}
			s=ns;
			for(int i=half(v),j=v/p-1|1;j>=p;j-=2){
				int c=smalls[j >> 1]-pc;
				for(int e=j*p>>1;i>=e;--i)smalls[i]-=c;
			}
			++pc;
		}
	larges[0]+=(s+(pc-1<<1))*(s-1)>>1;
	for(int k=1;k<s;++k)larges[0]-=larges[k];
	for(int l=1;l<s;++l){
		int q=roughs[l],M=N/q,e=smalls[half(M/q)]-pc;
		if(e<l+1)break;
		int t=0;
		for(int k=l+1;k<=e;++k)t+=smalls[half(divide(M,roughs[k]))];
		larges[0]+=t-(e-l)*(pc+l-1);
	}
	return larges[0]+1;
}
main(){
	int N, M;
	scanf("%lld%lld",&N,&M);
	const int ans = prime_pi(M)-prime_pi(N-1);
	if (ans == 0) puts("NO");
	else printf("%lld", ans);
}

U576164 大数据运算合集

先说暴力:

C++
#include <bits/stdc++.h>
#include <sys/mman.h>
#include <sys/stat.h>
char *p;
inline __int128 read()
{
    __int128 x=0,f=1;
	char ch=*p++;
	while (!isdigit(ch))
    {
        if(ch=='-')
            f=-1;
		ch=*p++;
    }
	while (isdigit(ch))
    {
		x=x*10+ch-'0';
		ch=*p++;
	}
	return x*f;
}
inline unsigned __int128 uread()
{
    unsigned __int128 x=0;
	char ch=*p++;
    while (!isdigit(ch))
		ch=*p++;
	while (isdigit(ch))
    {
		x=x*10+ch-'0';
		ch=*p++;
	}
	return x;
}
void write(__int128 x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
void uwrite(unsigned __int128 x)
{
    if(x>9) uwrite(x/10);
    putchar(x%10+'0');
}
__int128 pw(__int128 a,__int128 b,__int128 p)
{
	__int128 ans=1;
	while(b)
	{
		if(b&1)
			ans=ans*a%p;
		a=a*a%p;
		b>>=1;
	}
	return ans;
}
inline __int128 power(__int128 a,__int128 b,__int128 c)
{
    if(a==1) return 1%c;
    if(a==-1)
    {
        if(b==-1)
            return (-1)%c;
        return (b%2==0?1:-1)%c;
    }
    if(a==0) return 0;
    __int128 res=1;
    __uint128_t brt=((__uint128_t)1<<64)/c;
    for(__int128 i=1;i<=b;++i)
    {
        res=res*a;
        res=res-c*(brt*res>>64);
    }
    return res;
}
inline unsigned __int128 sum_of_jiecheng(unsigned __int128 a)
{
    unsigned __int128 result=1,temp=1;
    for(unsigned __int128 i=1;i<=a;++i)
    {
        for(unsigned __int128 j=2;j<=i;++j)
            temp*=j;
        result+=temp;
        temp=1;
    }
    return result;
}
int main()
{
    struct stat s;
    fstat(0,&s);
    p=(char*)mmap(NULL,s.st_size,1,2,0,0);
    __int128 a=read(),b=read(),c;
    write(a+b),putchar(','),putchar('\n');
    a=read(),b=read();
    write(a-b),putchar(','),putchar('\n');
    a=read(),b=read();
    write(a*b),putchar(','),putchar('\n');
    a=read(),b=read();
    write(a/b),putchar(','),putchar('\n');
    a=read(),b=read();
    write(a%b),putchar(','),putchar('\n');
    a=read(),b=read(),c=read();
    write(pw(a,b,c)),putchar(','),putchar('\n');
    unsigned __int128 num1=uread(),num2;
    ++p;
    if(*p=='?')
        putchar('1'),putchar(','),putchar('\n');
    else if(*p=='1')
        putchar('2'),putchar(','),putchar('\n'),++p;
    else
    {
        num2=uread();
        uwrite(sum_of_jiecheng(num2)),putchar(','),putchar('\n');
    }
    num1=uread(),num2=uread();
    uwrite(num1&num2),putchar(','),putchar('\n');
    num1=uread(),num2=uread();
    uwrite(num1|num2),putchar(','),putchar('\n');
    num1=uread(),num2=uread();
    uwrite(num1^num2),putchar(','),putchar('\n');
    num1=uread(),num2=uread();
    uwrite(num1<<num2),putchar(','),putchar('\n');
    num1=uread(),num2=uread();
    uwrite(num1>>num2),putchar('.');
    return 0;
}

网友优秀做法,目前最优解。@_Kagamine_Rin_

C++
#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;

// 添加 unsigned __int128 的字符串转换函数
unsigned __int128 string_to_uint(const string& s) {
	unsigned __int128 res = 0;
	for (size_t i = 0; i < s.size(); i++) {
		if (isdigit(s[i])) {
			res = res * 10 + (s[i] - '0');
		} else {
			break;
		}
	}
	return res;
}

// 添加 unsigned __int128 转字符串函数
string uint_to_string(unsigned __int128 n) {
	if (n == 0) return "0";
	string s;
	while (n) {
		s += '0' + n % 10;
		n /= 10;
	}
	reverse(s.begin(), s.end());
	return s;
}

// 修改 parse_number 函数,添加无符号版本
unsigned __int128 parse_unsigned_number(const string& s, int& pos) {
	unsigned __int128 res = 0;
	while (pos < s.size() && isdigit(s[pos])) {
		res = res * 10 + (s[pos] - '0');
		pos++;
	}
	return res;
}

__int128 string_to_int(const string& s) {
	__int128 res = 0;
	int sign = 1;
	size_t start = 0;
	if (s[0] == '-') {
		sign = -1;
		start = 1;
	}
	for (size_t i = start; i < s.size(); i++) {
		if (isdigit(s[i])) {
			res = res * 10 + (s[i] - '0');
		} else {
			break;
		}
	}
	return sign * res;
}

string int_to_string(__int128 n) {
	if (n == 0) return "0";
	bool negative = false;
	if (n < 0) {
		negative = true;
		n = -n;
	}
	string s;
	while (n) {
		s += '0' + n % 10;
		n /= 10;
	}
	if (negative) s += '-';
	reverse(s.begin(), s.end());
	return s;
}

__int128 parse_number(const string& s, int& pos) {
	__int128 res = 0;
	int sign = 1;
	if (pos < s.size() && s[pos] == '-') {
		sign = -1;
		pos++;
	}
	while (pos < s.size() && isdigit(s[pos])) {
		res = res * 10 + (s[pos] - '0');
		pos++;
	}
	return sign * res;
}

__int128 fast_pow_mod(__int128 a, __int128 b, __int128 mod) {
	if (mod == 1) return 0;
	__int128 res = 1;
	a = a % mod;
	if (a < 0) a += mod;
	while (b > 0) {
		if (b & 1) {
			res = (res * a) % mod;
		}
		a = (a * a) % mod;
		b >>= 1;
	}
	return res;
}

__int128 div_round_zero(__int128 a, __int128 b) {
	if (b == 0) return 0;
	return a / b;
}

__int128 mod_custom(__int128 a, __int128 b) {
	if (b == 0) return 0;
	__int128 q = div_round_zero(a, b);
	return a - q * b;
}

// 修改阶乘函数为 unsigned __int128
unsigned __int128 factorial(int n) {
	unsigned __int128 res = 1;
	for (int i = 1; i <= n; i++) {
		res *= i;
	}
	return res;
}

#define endl "\n"
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	for (int line = 1; line <= 12; line++) {
		string s;
		getline(cin, s);
		s = s.substr(0, s.size() - 2); // remove "=?"

		if (line <= 6) { // 前6个操作使用有符号版本
			__int128 result = 0;
			if (line == 1) { // addition
				int pos = 0;
				__int128 a = parse_number(s, pos);
				pos++; // skip '+'
				__int128 b = parse_number(s, pos);
				result = a + b;
			} else if (line == 2) { // subtraction
				int pos = 0;
				__int128 a = parse_number(s, pos);
				pos++; // skip '-'
				__int128 b = parse_number(s, pos);
				result = a - b;
			} else if (line == 3) { // multiplication
				int pos = 0;
				__int128 a = parse_number(s, pos);
				pos++; // skip '*'
				__int128 b = parse_number(s, pos);
				result = a * b;
			} else if (line == 4) { // division
				int pos = 0;
				__int128 a = parse_number(s, pos);
				pos++; // skip '/'
				__int128 b = parse_number(s, pos);
				result = div_round_zero(a, b);
			} else if (line == 5) { // modulus
				int pos = 0;
				__int128 a = parse_number(s, pos);
				pos++; // skip '%'
				__int128 b = parse_number(s, pos);
				result = mod_custom(a, b);
			} else if (line == 6) { // power then modulus
				int pos = 0;
				__int128 a = parse_number(s, pos);
				pos++; // skip '^'
				__int128 b = parse_number(s, pos);
				pos++; // skip '%'
				__int128 c = parse_number(s, pos);
				if (a == 1) {
					result = 1 % c;
				}
				else if (a == -1) {
					result = (b & 1 ? -1 : 1) % c;
				}
				else if (c == 0) {
					result = 0;
				} else {
					__int128 c_abs = c;
					if (c_abs < 0) c_abs = -c_abs;
					if (b < 0) {
						__int128 value;
						if (a == 1) {
							value = 1;
						} else if (a == -1) {
							if (b % 2 == 0) {
								value = 1;
							} else {
								value = -1;
							}
						} else {
							value = 0;
						}
						result = mod_custom(value, c);
					} else {
						__int128 r0 = fast_pow_mod(a, b, c_abs);
						if (a < 0 && (b % 2 == 1)) {
							result = r0 - c_abs;
						} else {
							result = r0;
						}
					}
				}
			}

			string output = int_to_string(result);
			if (line < 12) {
				cout << output << "," << endl;
			} else {
				cout << output << "." << endl;
			}
		} else { // 第7行及以后的操作使用无符号版本
			unsigned __int128 result = 0;
			if (line == 7) { // factorial sum
				int n;
				if (s.find("...") != string::npos) {
					size_t last_plus = s.find_last_of('+');
					size_t exclam = s.find('!', last_plus + 1);
					string n_str = s.substr(last_plus + 1, exclam - last_plus - 1);
					n = stoi(n_str);
				} else {
					if (s.find('+') != string::npos) {
						n = 1;
					} else {
						n = 0;
					}
				}
				result = 0;
				for (int i = 0; i <= n; i++) {
					result += factorial(i);
				}
			} else if (line == 8) { // bitwise AND
				int pos = 0;
				unsigned __int128 a = parse_unsigned_number(s, pos);
				pos++; // skip '&'
				unsigned __int128 b = parse_unsigned_number(s, pos);
				result = a & b;
			} else if (line == 9) { // bitwise OR
				int pos = 0;
				unsigned __int128 a = parse_unsigned_number(s, pos);
				pos++; // skip '|'
				unsigned __int128 b = parse_unsigned_number(s, pos);
				result = a | b;
			} else if (line == 10) { // bitwise XOR
				int pos = 0;
				unsigned __int128 a = parse_unsigned_number(s, pos);
				pos++; // skip '^'
				unsigned __int128 b = parse_unsigned_number(s, pos);
				result = a ^ b;
			} else if (line == 11) { // left shift
				int pos = 0;
				unsigned __int128 a = parse_unsigned_number(s, pos);
				pos += 2; // skip '<<'
				unsigned __int128 b = parse_unsigned_number(s, pos);
				result = a << b;
			} else if (line == 12) { // right shift
				int pos = 0;
				unsigned __int128 a = parse_unsigned_number(s, pos);
				pos += 2; // skip '>>'
				unsigned __int128 b = parse_unsigned_number(s, pos);
				result = a >> b;
			}

			string output = uint_to_string(result);
			if (line < 12) {
				cout << output << "," << endl;
			} else {
				cout << output << "." << endl;
			}
		}
	}
	return 0;
}

U645561 圆锥曲线

详见:https://www.luogu.com.cn/article/9idcyw7r

原文