hdu 4291 && hdu 4296

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4291

题意和解释网上一搜一大片,以前也做过一个用矩阵乘法求这种题的(这个还要求出嵌套的循环节),可是看到题的时候还是什么也没有想到,对以前的知识掌握的不好,也不会灵活用。。

View Code
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <math.h>
 7 #define _clr(a,val) (memset(a,val,sizeof(a)))
 8 
 9 using namespace std;
10 
11 typedef long long ll;
12 const ll mod1 = 183120;
13 const ll mod2 = 222222224;
14 const ll mod3 = 1000000007;
15 ll mod;
16 /*void tcal(ll mod)  找循环节
17 {
18     ll i;
19     ll tem = 0, kem = 1, tt = 0;
20     i = 1;
21     while(1)
22     {
23         tt = (3 * kem % mod + tem % mod) % mod;
24         tem = kem;
25         kem = tt;
26         if(tem == 0 && kem == 1)
27         {
28             cout<<i<<endl;
29             break;
30         }
31         i++;
32     }
33 }*/
34 struct node
35 {
36     ll a[2][2];
37     void init()
38     {
39         for(int i = 0; i < 2; i++)
40         {
41             for(int j = 0; j < 2; j++)
42             a[i][j] = (i == j);
43         }
44     }
45 };
46 node cal(node t,node b)
47 {
48     node c;
49     _clr(c.a,0);
50     int i,j,k;
51     for(k = 0; k < 2; k++)
52     {
53         for(i = 0; i < 2; i++)
54         {
55             for(j = 0; j < 2; j++)
56             {
57                 c.a[i][j] += ((t.a[i][k] * b.a[k][j]) % mod);
58             }
59         }
60     }
61     for(i = 0; i < 2; i++)
62     {
63         for(j = 0; j < 2; j++)
64         c.a[i][j] %= mod;
65     }
66     return c;
67 }
68 ll ca(node t,ll n,ll tmod)
69 {
70     node c;
71     c.init();
72     mod = tmod;
73     while(n)
74     {
75         if(n & 1) c = cal(c,t);
76         t = cal(t,t);
77         n /= 2;
78     }
79     return c.a[0][1];
80 }
81 int main()
82 {
83     ll n;
84     //tcal(mod3);  // 找出 10 ^ 9 + 7 的循环节
85     //tcal(mod2);  // 找出 (10^9+7)的循环节的循环节
86     //freopen("data.txt","r",stdin);
87     while(cin>>n)
88     {
89         if(n < 0) break;
90         node t;
91         t.a[0][0] = 3, t.a[0][1] = 1;
92         t.a[1][0] = 1, t.a[1][1] = 0;
93         ll ans = ca(t,ca(t,ca(t,n,mod1),mod2),mod3);
94         printf("%lld\n",ans);
95     }
96     return 0;
97 }

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4296

这个题悲剧就是我理解错了,然后给他们也说错了,悲剧的到最后才理解题目的真正意思

View Code
 1 typedef long long ll;
 2 const int N = 100010;
 3 struct node
 4 {
 5     int w;
 6     int h;
 7 }b[N];
 8 int cmp(node a,node b)
 9 {
10     return (a.w + a.h) > (b.w + b.h);
11 }
12 int main()
13 {
14     int i,j;
15     ll tsum;
16     int n;
17     //freopen("data.txt","r",stdin);
18     while(scanf("%d",&n) != EOF)
19     {
20         tsum = 0;
21         for(i = 0; i < n; i++)
22         {
23             scanf("%d%d",&b[i].w,&b[i].h);
24             tsum += b[i].w;
25         }
26         sort(b, b + n,cmp);
27         ll phd = 0;
28         ll maxx = 0;
29         for(j = 0; j < n - 1; j++)
30         {
31             tsum -= b[j].w;
32             phd = (tsum - b[j].h);
33             if(maxx < phd) maxx = phd;
34         }
35         cout<<maxx<<endl;
36     }
37     return 0;
38 }
posted @ 2012-09-17 11:20  AC_Girl  阅读(287)  评论(0编辑  收藏  举报