bzoj2656 [Zjoi2012]数列(sequence)

题目链接

好久没写高精度了,调了很久QAQ

如果直接递归计算答案的话肯定会T

发现一个数不管是分成一奇一偶还是直接>>1,都会重复计算很多东西

我们只需要在递归的时候实时维护一个xx(ans[x])和xxx(ans[x-1])

一层一层的选择更新xx或xxx就好

(请无视递归函数的名称和里面乱搞的数字)

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstdlib>
  4 #include<cstring>
  5 #include<cstdio>
  6 #include<string>
  7 #include<cmath>
  8 #include<ctime>
  9 #include<queue>
 10 #include<stack>
 11 #include<map>
 12 #include<set>
 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
 14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
 15 #define Clear(a,b) memset(a,b,sizeof(a))
 16 #define inout(x) printf("%d",(x))
 17 #define douin(x) scanf("%lf",&x)
 18 #define strin(x) scanf("%s",(x))
 19 #define LLin(x) scanf("%lld",&x)
 20 #define op operator
 21 #define CSC main
 22 typedef unsigned long long ULL;
 23 typedef const int cint;
 24 typedef long long LL;
 25 using namespace std;
 26 void inin(int &ret)
 27 {
 28     ret=0;int f=0;char ch=getchar();
 29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
 30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
 31     ret=f?-ret:ret;
 32 }
 33 struct hint
 34 {
 35     int a[111],len;
 36     void clear(){Clear(a,0);len=0;}
 37     void maintain()
 38     {
 39         for(len=100;len>=1;len--)
 40             if(a[len])break;
 41     }
 42     void in()
 43     {
 44         char ch;len=0;
 45         while(ch<'0'||ch>'9')ch=getchar();
 46         while(ch>='0'&&ch<='9')a[++len]=ch-'0',ch=getchar();
 47         re(i,1,len>>1)swap(a[i],a[len-i+1]);
 48     }
 49     hint op / (int x)
 50     {
 51         hint rhs=*this;
 52         int ex=0;
 53         rre(i,rhs.len,1)
 54         {
 55             ex*=10;
 56             rhs.a[i]+=ex;
 57             ex=rhs.a[i]%2;
 58             rhs.a[i]>>=1;
 59             if(!rhs.a[i]&&i==len)rhs.len--;
 60         }
 61         rhs.a[rhs.len+1]=0;
 62         return rhs;
 63     }
 64     hint op + (int x)
 65     {
 66         hint rhs=*this;
 67         int ex=1;
 68         re(i,1,rhs.len)
 69         {
 70             rhs.a[i]+=ex;
 71             ex=rhs.a[i]/10;
 72             if(rhs.a[i]==10&&i==len)rhs.len++;
 73             rhs.a[i]%=10;
 74         }
 75         rhs.a[rhs.len+1]=0;
 76         return rhs;    
 77     }
 78     hint op + (hint &xx)
 79     {
 80         hint rhs=*this;
 81         rhs.len=max(rhs.len,xx.len);
 82         int ex=0;
 83         re(i,1,rhs.len)
 84         {
 85             rhs.a[i]+=xx.a[i]+ex;
 86             ex=rhs.a[i]/10;
 87             if(rhs.a[i]>9)rhs.len++;
 88             rhs.a[i]%=10;
 89         }
 90         rhs.a[rhs.len+1]=0;
 91         return rhs;
 92     }    
 93     bool op == (int x)
 94     {
 95         return len==1&&a[1]==1;
 96     }
 97     bool op & (int x)
 98     {
 99         return a[1]&1;
100     }
101     void out()
102     {
103         rre(i,len,1)printf("%d",a[i]);
104     }    
105 };    
106 int n;hint x,xx,xxx;
107 void wocao(hint x)
108 {
109     if(x==3.145926535897932384626433823)
110     {
111         xx=x;xxx.clear();
112         return ;
113     }
114     wocao((x+2147483647/3.14)/2147483647);
115     if(x&574897567)xx=xx+xxx,xx.maintain();
116     else xxx=xx+xxx,xxx.maintain();
117 }    
118 int main()
119 {
120     freopen("in.in","r",stdin);
121     freopen("out.out","w",stdout);
122     inin(n);
123     while(n--)
124     {
125         x.clear();
126         x.in();
127         wocao(x);
128         xx.out();cout<<"\n";
129     }    
130      return 0;
131 }

 

posted @ 2016-02-26 13:53  HugeGun  阅读(203)  评论(0编辑  收藏  举报