Pentium.Labs

System全家桶:https://zhuanlan.zhihu.com/c_1238468913098731520

导航

bc#29 做题笔记

昨天的bc被坑惨了= =

本来能涨rating的大好机会又浪费了。。。大号已弃号

 

A:第一反应是高精度,结果模板找不到了= =,然后现学现卖拍了个java的BigInteger+快速幂,调了好半天不说还TLE。貌似这题就在卡java

实际上尼玛等号两边取log不就完了么。。。

 

B:A题调了半天,开始做B的时候已经没多少时间了。。。

找出了斐波那契数列+前缀和的规律,结果把用矩阵快速幂求斐波那契前n项和的那个梗又忘了

最后out of submit time

 

事实证明还是要多做成套的题,这样才能发现很多平时难以察觉到的问题。

 

-------------------------------------------------

补上AC Code:

A:高你妹妹的高精度

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #define eps 1e-8
 5 using namespace std;
 6 
 7 int fcmp(double a,double b)
 8 {
 9     double tm=fabs(a-b);
10     if (tm<eps) return 0;   //a==b
11     else
12         return a<b?-1:1;
13 }
14 
15 int a,b,c,d;
16 
17 int main()
18 {
19     while(~scanf("%d%d%d%d",&a,&b,&c,&d))
20     {
21         double aa=log(a),cc=log(c);
22         aa=aa*b;    cc=cc*d;
23         switch (fcmp(aa,cc))
24         {
25             case  0:printf("=\n");
26                     break;
27             case -1:printf("<\n");
28                     break;
29             case  1:printf(">\n");
30                     break;
31         }
32     }
33 
34     return 0;
35 }
View Code

 

B:事实证明即使只有三组数据也可能出现cin TLE而scanf AC的情况= =

  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstdio>
  4 #include <vector>
  5 using namespace std;
  6 #define ULL long long
  7 #define MOD 10000007
  8 
  9 ULL ans,mx1,mx2;
 10 ULL n,k,x;
 11 typedef vector<ULL> vec;
 12 typedef vector<vec> mat;
 13 int a[100010];
 14 
 15 mat mul(mat &A,mat &B)      //return A*B
 16 {
 17     mat C(A.size(),vec(B[0].size()));
 18     for (int i=0;i<(int)A.size();i++)
 19     {
 20         for (int k=0;k<(int)B.size();k++)
 21         {
 22             for (int j=0;j<(int)B[0].size();j++)
 23             {
 24                 C[i][j]=(C[i][j]+A[i][k]*B[k][j])%MOD;
 25             }
 26         }
 27     }
 28     return C;
 29 }
 30 
 31 mat m_pow(mat A,int m)      //return A^m
 32 {
 33     mat B(A.size(),vec(A.size()));
 34     for (int i=0;i<(int)A.size();i++)
 35         B[i][i]=1;
 36     while (m>0)
 37     {
 38         if (m&1)    B=mul(B,A);
 39         A=mul(A,A);
 40         m>>=1;
 41     }
 42     return B;
 43 }
 44 
 45 int main()
 46 {
 47 
 48     //while (cin>>n>>k)
 49     while (~scanf("%d%d",&n,&k))
 50     {
 51         mat AA(3,vec(3));
 52         mat A(3,vec(3));
 53         mat B(3,vec(3));
 54         mat C(3,vec(1));
 55         mat D(3,vec(1));
 56         mat T(3,vec(1));
 57         T[0][0]=2;  T[1][0]=1;  T[2][0]=1;
 58         A[0][0]=1;  A[0][1]=1;  A[0][2]=1;
 59         A[1][0]=0;  A[1][1]=1;  A[1][2]=1;
 60         A[2][0]=0;  A[2][1]=1;  A[2][2]=0;
 61         AA=A;
 62         A=m_pow(A,k-2);
 63         C=mul(A,T);
 64         B=mul(A,AA);
 65         //B=m_pow(B,k-1);
 66         D=mul(B,T);
 67         ULL FK=C[0][0],FKK=D[0][0]-1;
 68         if (k==2)   {FK=2;  FKK=3;}
 69         if (k==1)   {FK=1;  FKK=1;}
 70         //cout<<FK<<"  --  "<<FKK<<endl;
 71 
 72         /*
 73         LL fk=1,fkk=2,FK=2,FKK=4;
 74         for (int i=3;i<=k;i++)
 75         {
 76             //fk:f[k]   fkk:f[k+1]
 77             LL tmp=fk+fkk;
 78             fk=fkk; fkk=tmp;
 79             fk=fk%MOD;
 80             fkk=fkk%MOD;
 81             FK+=fk;
 82             FKK=FK+fkk;
 83             FK=FK%MOD;
 84             FKK=FKK%MOD;
 85         }
 86         FKK-=1;   if (FKK<0)    FKK+=MOD;
 87         */
 88 
 89 
 90         ans=0;
 91         mx1=0;  mx2=0;
 92         for (int i=1;i<=n;i++)
 93         {
 94             cin>>x;
 95             ans+=x;
 96             if (x>mx1)
 97             {
 98                 mx2=mx1;
 99                 mx1=x;
100             }
101             else if ((x<=mx1)&&(x>mx2))
102             {
103                 mx2=x;
104             }
105         }
106         /*
107         for (int i=1;i<=n;i++)
108         {
109             cin>>a[i];
110             ans+=a[i];
111         }
112         sort(a+1,a+n+1);
113         mx1=a[n];   mx2=a[n-1];
114         */
115 
116         //cout<<mx1<<"   "<<mx2<<" = "<<ans<<endl;
117         /*
118         for (int i=1;i<=k;i++)
119         {
120             ans=ans+mx1+mx2;
121             ans=ans%MOD;
122             LL tmp=mx1;
123             mx1=mx1+mx2;
124             mx2=tmp;
125         }
126         */
127         //cout<<ans<<" - "<<FK<<" "<<FKK<<endl;
128         mx1=mx1*FKK;    mx1=mx1%MOD;
129         mx2=mx2*FK;   mx2=mx2%MOD;
130         ans=ans+mx1+mx2;
131         ans=ans%MOD;
132         //cout<<ans<<endl;
133         printf("%I64d\n",ans);
134     }
135     return 0;
136 }
View Code

 

posted on 2015-02-08 16:55  Pentium.Labs  阅读(184)  评论(0编辑  收藏  举报



Pentium.Lab Since 1998