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 }
复制代码

 

 
分类:  数学 数论
 
 
 
« 上一篇: POJ 2115 单变元模线性方程
» 下一篇: HDU 5988 最小费用流
posted @  2017-10-15 09:44  Flowersea 阅读( 27) 评论( 0编辑  收藏

链接:

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 }
复制代码

 

猜你喜欢

转载自www.cnblogs.com/baocong/p/9270959.html