跳到正文

题解

题解:P1001 A+B Problem

langmouren约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年5月10日
收录
2026年5月27日
更新
2026年11月14日
篇幅
约 1 分钟
获赞
28
题目
P1001

版本

  1. v1当前

A+B Problem

模拟退火练手题。

可以不断退火,使其接近 a+ba+b,最后输出答案。

代码

C++
#include<bits/stdc++.h>
using namespace std;

int a,b,ans;
mt19937 Rand(time(0));
uniform_int_distribution<int>dist(-1e9,1e9);

void SA(int sum){
    double beginT=5000,endT=1e-6,k=0.9997;
    
    for(double T=beginT;T>endT;T*=k){
        int A=ans+dist(Rand)%2001-1000;
        A=max(-1000000000,min(1000000000,A));
        int B=sum-A,cur=A+B;
        
        if(abs(cur-sum)<abs(ans-sum)) ans=cur;
        else if(exp((abs(ans-sum)-abs(cur-sum))/T)>(double)Rand()/INT_MAX) ans=cur;
    }
}

int main(){
    cin>>a>>b;
    int sum=a+b;
    ans=dist(Rand);
    int cnt=10;
    while(cnt--) SA(sum);
    cout<<ans;
    return 0;
}
原文
存档评论31 条抓取于