BZOJ2134: 单选错位

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2134

题解:因为每个答案之间是互不影响的,所以我们可以挨个计算。

         假设当前在做 i 题目,如果a[i+1]>=a[i],那么我们只需要让i+1题目的答案是i的答案即可,ans+=1/a[i+1]

         否则 i 题目的答案必须在1--a[i+1],所以ans+=a[i+1]/a[i]*1/a[i+1]=1/a[i]

          换句话说 ans+=min(1/a[i+1],1/a[i])

代码:

 1 #include<cstdio>
 2 
 3 #include<cstdlib>
 4 
 5 #include<cmath>
 6 
 7 #include<cstring>
 8 
 9 #include<algorithm>
10 
11 #include<iostream>
12 
13 #include<vector>
14 
15 #include<map>
16 
17 #include<set>
18 
19 #include<queue>
20 
21 #include<string>
22 
23 #define inf 1000000000
24 
25 #define maxn 10000000+5
26 
27 #define maxm 500+100
28 
29 #define eps 1e-10
30 
31 #define ll long long
32 
33 #define pa pair<int,int>
34 
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36 
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38 
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40 
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42 
43 #define mod 100000001
44 
45 using namespace std;
46 
47 inline ll read()
48 
49 {
50 
51     ll x=0,f=1;char ch=getchar();
52 
53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
54 
55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
56 
57     return x*f;
58 
59 }
60 ll n,a,b,c,d[maxn];
61 
62 int main()
63 
64 {
65 
66     freopen("input.txt","r",stdin);
67 
68     freopen("output.txt","w",stdout);
69 
70     n=read();a=read();b=read();c=read();d[1]=read();
71     for2(i,2,n)d[i]=(d[i-1]*a+b)%mod;
72     for1(i,n)d[i]=(d[i]%c)+1;d[n+1]=d[1];
73     double ans=0;
74     for1(i,n)
75     if(d[i+1]>=d[i])ans+=1.0/(double)d[i+1];
76     else ans+=1.0/(double)d[i];
77     printf("%.3f\n",ans);
78 
79     return 0;
80 
81 }
View Code

 

posted @ 2014-11-11 16:12  ZYF-ZYF  Views(222)  Comments(0Edit  收藏  举报