POJ 2420 模拟退火

链接:

http://poj.org/problem?id=2420

题意:

给出n个点,找到一个点,使得它到所有的点的距离最小。

题解:

最近要做一个排课系统,需要用到模拟退火算法,之前虽然了解过这个算法,但是没有写过题。就先在POJ上找了一道学习一下。

代码:

 1 #include <iomanip>
 2 struct Point { double x, y; };
 3
 4 const double eps = 1e-8;        //搜索条件阀值
 5 const double T = 100;        //初始温度
 6 const double delta = 0.98;    //温度下降速度
 7 int dx[4] = { 1,-1,0,0 };
 8 int dy[4] = { 0,0,1,-1 };
 9 int n;
10 Point p[MAXN];
11
12 double sqr(double x) {
13     return x * x;
14 }
15
16 double dist(Point a, Point b) {
17     return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
18 }
19
20 double sum(Point point) {
21     double res = 0;
22     rep(i, 0, n) res += dist(point, p[i]);
23     return res;
24 }
25
26 int main() {
27     ios::sync_with_stdio(false), cin.tie(0);
28     cin >> n;
29     rep(i, 0, n) cin >> p[i].x >> p[i].y;
30     Point s = p[0];        //随机初始化一个点开始搜索
31     double t = T;        //初始化温度
32     double ans = INF;
33     while (t > eps) {
34         int fg = 1;
35         while (fg) {
36             fg = 0;
37             rep(i, 0, 4) {
38                 Point point = Point{ s.x + dx[i],s.y + dy[i] };
39                 double t = sum(point);
40                 if (ans > t) {
41                     ans = t;
42                     s = point;
43                     fg = 1;
44                 }
45             }
46         }
47         t *= delta;
48     }
49     cout << std::fixed << setprecision(0) << ans << endl;
50     return 0;
51 }

链接:

http://poj.org/problem?id=1808

题意:

判断x^2同余a(modn)是否存在

题解:

平方剩余

代码:

31 ll mod_pow(ll x, ll n, ll mod) {
32     int res = 1;
33     while (n) {
34         if (n & 1) res = res * x % mod;
35         x = x * x % mod;
36         n >>= 1;
37     }
38     return res;
39 }
40
41 ll mod_sqr(ll a, ll n) {
42     ll b, k, i, x;
43     //if (n == 2) return a%n;
44     if(mod_pow(a, (n - 1) / 2, n) == 1) {
45         return 1;
46         if (n % 4 == 3) x = mod_pow(a, (n + 1) / 4, n);
47         else {
48             for (b = 1; mod_pow(b, (n - 1) / 2, n) == 1; b++);
49             i = (n - 1) / 2;
50             k = 0;
51             do {
52                 i /= 2;
53                 k /= 2;
54                 if ((mod_pow(a, i, n)*mod_pow(b, k, n) + 1) % n == 0)
55                     k += (n - 1) / 2;
56             } while (i % 2 == 0);
57             x = mod_pow(a, (i + 1) / 2, n)*mod_pow(b, k / 2, n) % n;
58         }
59         if (x * 2 > n) x = n - x;
60     }
61     return -1;
62 }
63
64 int main() {
65     ios::sync_with_stdio(false), cin.tie(0);
66     int T;
67     cin >> T;
68     rep(cas, 1, T + 1) {
69         ll a, p;
70         cin >> a >> p;
71         a = (a%p + p) % p;
72         cout << "Scenario #" << cas << ":" << endl;
73         cout << mod_sqr(a, p) << endl << endl;
74     }
75     return 0;
76 }

分类: 数学 数论

Flowersea
关注 - 2
粉丝 - 6

«上一篇:POJ 2115 单变元模线性方程
»下一篇:HDU 5988 最小费用流

posted @ 2017-10-15 09:44 Flowersea 阅读(27) 评论(0) 编辑 收藏

原文地址:https://www.cnblogs.com/baocong/p/9270959.html

时间: 2024-08-07 22:50:49

POJ 2420 模拟退火的相关文章

POJ 2420 A Star not a Tree? (模拟退火)

题目地址:POJ 2420 今天在比赛遇到了这题..于是现场学了一下模拟退火.... 这题是先初始化为一个点,然后不断趋近距离和最短的点.还是挺简单的.. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h&g

模拟退火 (poj 2420, poj 2069)

模拟退火基本知识 其伪代码例如以下: Let s = s0 For k = 0 through k_max (exclusive): T := temperature(k / k_max) Pick a random neighbour, s_new := neighbour(s) If P(E(s), E(s_new), T) > random(0, 1), move to the new state: s := s_new Output: the final state s 样例: poj

POJ 2420 模拟退火法

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3272   Accepted: 1664 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you

POJ 2420

模拟退火算法.昨天看了PPT,原来模拟退火算法涉及到马尔什么链,开始理解,它其实就是一个关于抽样的问题.随机抽样,选取足够多的样本,然后逐步逼近.而在平面上,由于T的下降,使得逐渐缩小至一点.然后,就可以了. 算法: 在平面上随机选取一些点,当然这些点应当有一点相关的性吧.我猜的. 然后在这些点随机移动,若移动后更优,则移动,否则,留待下一次循环. #include <iostream> #include <cstdio> #include <cstring> #inc

POJ 1379 模拟退火

模拟退火算法,很久之前就写过一篇文章了.双倍经验题(POJ 2420) 题意: 在一个矩形区域内,求一个点的距离到所有点的距离最短的那个,最大. 这个题意,很像二分定义,但是毫无思路,也不能暴力枚举,那就模拟退火. #include <stdio.h> #include <math.h> #include <algorithm> using namespace std; const int maxn = 1005; int X,Y,M; int Kase; struct

POJ 1379 模拟退火算法

求规定平面上一点到已知点的最小距离最大的点. 模拟退火的流程是,随机构造几组解作为初始解空间,每次对当前解空间进行随机扩展,若发现更优解则替换. 进行的次数由参数人为控制,而随机扩展的幅度也是随着次数逐渐减小的. #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<cmath> #include<string> #include&

三分 POJ 2420 A Star not a Tree?

题目传送门 1 /* 2 题意:求费马点 3 三分:对x轴和y轴求极值,使到每个点的距离和最小 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 10 const int MAXN = 1e2 + 10; 11 const int INF = 0x3f3f3f3f; 12 double x[MAXN], y[MAXN]; 13 i

poj 2420,模拟退火算法,费马点

题目链接:http://poj.org/problem?id=2420 题意:给n个点,找出一个点,使这个点到其他所有点的距离之和最小,也就是求费马点. 参考链接:http://www.cnblogs.com/heaad/archive/2010/12/20/1911614.html 这一篇文章写的很好,我这个小白都有一点小明白了. 记一下笔记: 模拟退火: 就是在一定范围内接受一些不是最优的解,来跳出局部最优,靠近整体最优. 贴一下伪代码: //#pragma comment(linker,

poj 2420 A Star not a Tree?——模拟退火

题目:http://poj.org/problem?id=2420 精度设成1e-17,做三遍.ans设成double,最后再取整. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<ctime> #define db double usin