# APAC Practice

2016 round A

A. Googol String

small && large

LL a[65];
int dfs(LL pos, int id, bool f)
{
if(pos==1 || pos==a[id-1]+1)
return f? 0:1;
if(pos>a[id-1])
return dfs(a[id]-pos+1, id-1, f^1);
else
return dfs(pos, id-1, f);
}
int main()
{
freopen("A-small-practice.in", "r", stdin);
freopen("A.out", "w", stdout);
a[1]=1;
for(int i=2;i<=60;i++)
a[i]=a[i-1]*2+1;
int t, ca=1;
scanf("%d", &t);
while(t--)
{
LL n;
scanf("%I64d", &n);
int pos=lower_bound(a, a+61, n)-a;
printf("Case #%d: %d\n", ca++, dfs(n, pos, 1));
}
return 0;
}

B. gCube

n个数，m个询问

smal

public class Main
{
static BigDecimal quick(BigDecimal a, BigInteger b)
{
BigDecimal ans=BigDecimal.ONE;
while(b.compareTo(BigInteger.ZERO)!=0)
{
if(b.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO)!=0)
ans=ans.multiply(a);
b=b.divide(BigInteger.valueOf(2));
a=a.multiply(a);
}
return ans;
}
static BigDecimal sqrt(BigDecimal x, int n)
{
BigDecimal l=BigDecimal.ZERO, r=x;
while(r.subtract(l).abs().compareTo(BigDecimal.valueOf(1e-10))>0)
{
BigDecimal cur=quick(m, BigInteger.valueOf(n));
cur=cur.subtract(x);
if(cur.abs().compareTo(BigDecimal.valueOf(1e-10))<0)
return m;
else if(cur.compareTo(BigDecimal.ZERO)<0)
l=m;
else
r=m;
}
return l;
}
public static void main(String[] args) throws FileNotFoundException
{
//        PrintWriter out=new PrintWriter(System.out);
Scanner in=new Scanner(new File("B-large-practice.in"));
PrintWriter out = new PrintWriter(new File("B.out"));
int t=in.nextInt();
int ca=1;
while((t--)!=0)
{
int n=in.nextInt();
int m=in.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
a[i]=in.nextInt();
BigDecimal []b=new BigDecimal[n];
b[0]=BigDecimal.valueOf(a[0]);
for(int i=1;i<n;i++)
b[i]=b[i-1].multiply(BigDecimal.valueOf(a[i]));
out.println("Case #"+ca+":");ca++;
while((m--)!=0)
{
int l=in.nextInt();
int r=in.nextInt();
BigDecimal pre=BigDecimal.ONE;
if(l!=0) pre=b[l-1];
BigDecimal s=b[r].divide(pre);
out.printf("%.9f", sqrt(s, r-l+1));
out.println("");
}
}
out.close();
}
}

large

const LD eps=1e-10; // eps<1e9了要long double！！！

int a[1005];
LD sqrtn(int x, int y)
{
LD l=1, r=a[y];
for(int i=x;i<=y;i++)     // 数列不递增！！注意取最大值！
r=max(r, (LD)a[i]);
while(fabs(l-r)>eps)
{
LD mid=(l+r)/2.0;
LD cur=1.0;
for(int i=x;i<=y;i++)
cur*=a[i]/mid;　　　　// 要注意这里不能fabs(cur-1)<eps了就结束 因为cur与1<eps了 不代表mid与ans的误差也小于eps了！！
if(cur>1)
l=mid;
else
r=mid;
}
return l;
}
int main()
{
freopen("B-large-practice.in", "r", stdin);
freopen("B.out", "w", stdout);
int t, ca=1;
scanf("%d", &t);
while(t--)
{
int n, m;
scanf("%d%d", &n, &m);
for(int i=0;i<n;i++)
scanf("%d", &a[i]);
printf("Case #%d:\n", ca++);
while(m--)
{
int l, r;
scanf("%d%d", &l, &r);
printf("%.9Lf\n", sqrtn(l, r));
}
}
return 0;
}

2017 round A

 1 char s[25];
2 bool vis[1005];
3 int main()
4 {
5     freopen("A-large-practice.in", "r", stdin);
6     freopen("A.out", "w", stdout);
7     int t, ca=1;
8     scanf("%d", &t);
9     while(t--)
10     {
11         printf("Case #%d: ", ca++);
12         int n;
13         scanf("%d", &n);
14         getchar();
15         string ans="";
16         int maxn=0;
17         for(int i=0;i<n;i++)
18         {
19             gets(s);
20             string ss=s;
21             memset(vis, 0, sizeof(vis));
22             int num=0;
23             int len=strlen(s);
24             for(int j=0;j<len;j++)
25                 if(s[j]!=' ' && !vis[s[j]])
26                     num++, vis[s[j]]=1;
27             if(num>maxn)
28                 maxn=num, ans=s;
29             else if(num==maxn)
30                 if(ss<ans)
31                     ans=ss;
32         }
33         cout<<ans<<endl;
34     }
35     return 0;
36 }
A

B. Rain

 1 vector<int> v;
2 int mp[55][55];
3 bool vis[55][55];
4 int ans, n, m;
5 bool can(int x, int y, int val)
6 {
7 //    int num=0;
8     if(!vis[x][y] && mp[x][y]==val)
9     {
10         if(x==0 || x==n-1 || y==0 || y==m-1)
11             return false;
12         vis[x][y]=1;
13 //        num=1;
14         if(x>0){
15             if(!can(x-1, y, val)) return false;}
16         if(x<n-1){
17             if(!can(x+1, y, val)) return false;}
18         if(y>0){
19             if(!can(x, y-1, val)) return false;}
20         if(y<m-1){
21             if(!can(x, y+1, val)) return false;}
22     }
23     else if(mp[x][y]<val)
24         return false;
25     return true;
26 }
27 void dfs(int x, int y, int num)
28 {
29     for(int i=0;i<n;i++)
30         for(int j=0;j<m;j++)
31             if(vis[i][j])
32                 ans+=num-mp[i][j], mp[i][j]=num, vis[i][j]=0;
33 }
34 int main()
35 {
36     freopen("B-large.in", "r", stdin);
37     freopen("B.out", "w", stdout);
38     int t, ca=1;
39     scanf("%d", &t);
40     while(t--)
41     {
42         printf("Case #%d: ", ca++);
43         scanf("%d%d", &n, &m);
44         v.clear();
45         for(int i=0;i<n;i++)
46             for(int j=0;j<m;j++)
47                 scanf("%d", &mp[i][j]), v.push_back(mp[i][j]);
48         sort(v.begin(), v.end());
49         int d=unique(v.begin(), v.end())-v.begin();
50         if(d<2)
51         {
52             puts("0");
53             continue;
54         }
55         ans=0;
56         for(int ii=1;ii<d;ii++)
57         {
58             for(int i=0;i<n;i++)
59             {
60                 for(int j=0;j<m;j++)
61                 {
62                     memset(vis, 0, sizeof(vis));
63                     if(mp[i][j]<v[ii] && can(i, j, mp[i][j]))
64                     {
65                         dfs(i, j, v[ii]);
66                     }
67                 }
68             }
69         }
70 //        for(int i=0;i<n;i++){
71 //            for(int j=0;j<m;j++)
72 //                printf("%d ", mp[i][j]);puts("");}
73         printf("%d\n", ans);
74     }
75     return 0;
76 }
B

C. Jane's Flower Shop

 1 typedef long double LD;
2 const LD eps=1e-10;
3
4 int a[105];
5 int n;
6 LD cal(LD r)
7 {
8     LD cur=1, ans=0;
9     for(int i=0;i<=n;i++, cur*=(1+r))
10         ans+=a[n-i]*cur;
11     return ans;
12 }
13 int main()
14 {
15     freopen("C-large.in", "r", stdin);
16     freopen("C.out", "w", stdout);
17     int t, ca=1;
18     scanf("%d", &t);
19     while(t--)
20     {
21         printf("Case #%d: ", ca++);
22         scanf("%d", &n);
23             for(int i=0;i<=n;i++)
24                 scanf("%d", &a[i]);
25             a[0]=-a[0];
26             LD l=-1, r=1;
27             LD ll=cal(l), rr=cal(r);
28             while(fabs(l-r)>eps)
29             {
30                 LD m=(r+l)*0.5;
31                 LD mm=cal(m);
32                 if(mm<0 && ll<0)
33                     l=m;
34                 else if(mm<0 && rr<0)
35                     r=m;
36                 else if(mm>0 && ll>0)
37                     l=m;
38                 else if(mm>0 && rr>0)
39                     r=m;
40                 else
41                     l=m;
42             }
43             printf("%.12Lf\n", l);
44     }
45     return 0;
46 }
C

posted @ 2016-07-03 12:07  Empress  阅读(...)  评论(...编辑  收藏