ACM-ICPC 2017 Asia Urumqi(第八场)

A. Coins

Alice and Bob are playing a simple game. They line up a row of nnn identical coins, all with the heads facing down onto the table and the tails upward.

For exactly mmm times they select any kkk of the coins and toss them into the air, replacing each of them either heads-up or heads-down with the same possibility. Their purpose is to gain as many coins heads-up as they can.

Input

The input has several test cases and the first line contains the integer t(1≤t≤1000) which is the total number of cases.

For each case, a line contains three space-separated integers n, m(1≤n,m≤100) and k(1≤k≤n.

Output

For each test case, output the expected number of coins heads-up which you could have at the end under the optimal strategy, as a real number with the precision of 3 digits.

样例输入

6
2 1 1
2 3 1
5 4 3
6 2 3
6 100 1
6 100 2

样例输出

0.500
1.250
3.479
3.000
5.500
5.000

题目来源

概率DP.

设dp[i][j]是第i次操作后正面朝上的概率,则反面朝上还有n-j枚,若n-j大于k,则k中任取

若n-j小于k,则必须从已经正面朝上的硬币中取j-(k-(n-j))枚

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?0:x<0?-1:1;}
typedef long long ll;
int n,m,k,T;
double dp[106][106];
double p[106],c[106][106];
void solve()
{
    c[0][0]=1;
    for(int i=1;i<=100;i++)
    {
        c[i][0]=1;
        for(int j=1;j<=100;j++)
            c[i][j]=c[i-1][j-1]+c[i-1][j];
    }
    p[0]=1;
    for(int i=1;i<=100;i++)
        p[i]=p[i-1]/2.0;
}
int main()
{
    solve();
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        memset(dp,0,sizeof(dp));
        dp[0][0]=1;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<=n;j++)
            {
                for(int t=0;t<=k;t++)
                {
                    if(n-j>=k) dp[i+1][j+t]+=dp[i][j]*c[k][t]*p[k];
                    else dp[i+1][n-k+t]+=dp[i][j]*c[k][t]*p[k];
                }
            }
        }
        double ans=0.0;
        for(int i=1;i<=n;i++)
            ans+=i*dp[m][i];
        printf("%.3lf\n",ans);
    }
    return 0;
}    

B. The Difference

Alice was always good at math. Her only weak points were multiplication and subtraction. To help her with that, Bob presented her with the following problem.

He gave her four positive integers. Alice can change their order optionally. Her task is to find an order, denoted by A1,A2,A and A4?, with the maximum value of A1×A2?A3×A4?.

Input

The input contains several test cases and the first line provides an integer t(1≤t≤100) indicating the number of cases.

Each of the following t lines contains four space-separated integers.

All integers are positive and not greater than 100.

Output

For each test case, output a line with a single integer, the maximum value of A1×A2?A3×A4?.

样例输入

5
1 2 3 4
2 2 2 2
7 4 3 8
100 99 98 97
100 100 1 2

样例输出

10
0
44
394
9998签到题
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?0:x<0?-1:1;}
typedef long long ll;
int main()
{
    int n,a[4];
    scanf("%d",&n);
    while(n--){
        for(int i=0;i<4;i++)
            scanf("%d",&a[i]);
        int ans=a[0]*a[1]-a[2]*a[3];
        for(int i=0;i<4;i++)
            for(int j=0;j<4;j++)
                for(int k=0;k<3;k++)
                    if(!(i==j || j==k || i==k))
                        ans=max(ans,a[i]*a[j]-a[k]*a[6-i-j-k]);
        printf("%d\n",ans);
    }
    return 0;
}

D. Fence Building

Farmer John owns a farm. He first builds a circle fence. Then, he will choose n points and build some straight fences connecting them. Next, he will feed a cow in each region so that cows cannot play with each other without breaking the fences. In order to feed more cows, he also wants to have as many regions as possible. However, he is busy building fences now, so he needs your help to determine what is the maximum number of cows he can feed if he chooses these n points properly.

Input

The first line contains an integer 1≤T≤100000, the number of test cases. For each test case, there is one line that contains an integer n. It is guaranteed that 1≤T≤105 and 1≤n≤1018.

Output

For each test case, you should output a line ”Case #i: ans” where i is the test caseS number, starting from 1 and ans is the remainder of the maximum number of cows farmer John can feed when divided by 109+7.

样例输入

3
1
3
5

样例输出

Case #1: 1
Case #2: 4
Case #3: 16结论题w=C(n,2)+C(n,4)+1;
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?0:x<0?-1:1;}
typedef long long ll;
ll n,t;
void exgcd(ll a,ll b,ll &x,ll &y)
{
    ll t;
    if(!b){x=1;y=0;return ;}
    exgcd(b,a%b,x,y);
    t=x;x=y;
    y=t-a/b*x;
}
ll inv(ll n)
{
    ll x,y;
    exgcd(n,MOD,x,y);
    return (x+MOD)%MOD;
}
ll C(ll n,ll m)
{
    if(m>n) return 0;
    ll ans=1;
    for(ll i=1;i<=m;i++)
    {
        ans=(ans*(n-i+1)%MOD*inv(i)%MOD)%MOD;
    }
    return ans;

}
ll solve(ll n,ll m)
{
    if(m==0) return 1;
    return C(n%MOD,m%MOD)*solve(n/MOD,m/MOD)%MOD;
}
int main()
{
    scanf("%lld",&t);
    ll o=t;
    while(t--)
    {
        scanf("%lld",&n);
        printf("Case #%lld: %lld\n",o-t,(solve(n,2)+solve(n,4)+1)%MOD);
    }
    return 0;
}

G. The Mountain

All as we know, a mountain is a large landform that stretches above the surrounding land in a limited area. If we as the tourists take a picture of a distant mountain and print it out, the image on the surface of paper will be in the shape of a particular polygon.

From mathematics angle we can describe the range of the mountain in the picture as a list of distinct points, denoted by (x1,y1) to (xn,yn). The first point is at the original point of the coordinate system and the last point is lying on the x-axis. All points else have positive y coordinates and incremental xxx coordinates. Specifically, all x coordinates satisfy 0=x1<x2<x3<...<xn. All y coordinates are positive except the first and the last points whose yyy coordinates are zeroes.

The range of the mountain is the polygon whose boundary passes through points (x1,y1) to (xn,yn) in turn and goes back to the first point. In this problem, your task is to calculate the area of the range of a mountain in the picture.

Input

The input has several test cases and the first line describes an integer t(1≤t≤20) which is the total number of cases.

In each case, the first line provides the integer n(1≤n≤100) which is the number of points used to describe the range of a mountain. Following n lines describe all points and the iii-th line contains two integers xix_ixi? and yi(0≤xi,yi≤1000) indicating the coordinate of the i-th point.

Output

For each test case, output the area in a line with the precision of 666 digits.

样例输入

3
3
0 0
1 1
2 0
4
0 0
5 10
10 15
15 0
5
0 0
3 7
7 2
9 10
13 0

样例输出

1.000000
125.000000
60.500000计算不规则多边形面积,因为x有序,y>0所以切成梯形来计算
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#pragma GCC diagnostic error "-std=c++11"
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define esp 1e-9
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
int dcmp(double x){return fabs(x)<esp?0:x<0?-1:1;}
typedef long long ll;
struct Point{
    double x,y;
}e[106];
int n,t;
int main()
{
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&e[i].x,&e[i].y);
        double ans=0.0;
        for(int i=1;i<n;i++)
            ans+=(e[i].y+e[i-1].y)*(e[i].x-e[i-1].x);
        printf("%.6lf\n",ans/2);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/9386681.html

时间: 2024-08-05 03:54:18

ACM-ICPC 2017 Asia Urumqi(第八场)的相关文章

ACM-ICPC 2017 Asia Urumqi:A. Coins(DP) 组合数学

Alice and Bob are playing a simple game. They line up a row of nn identical coins, all with the heads facing down onto the table and the tails upward. For exactly mm times they select any kk of the coins and toss them into the air, replacing each of

ACM-ICPC 2017 Asia Urumqi A. Coins

Alice and Bob are playing a simple game. They line up a row of n identical coins, all with the heads facing down onto the table and the tails upward. For exactly mm times they select any k of the coins and toss them into the air, replacing each of th

UPC5431/acm icpc 2017 Tehran Column Addition

题目链接:http://exam.upc.edu.cn/problem.php?cid=1326&pid=7 题意:给你一个可能存在错误的加法等式,问最少删除多少列能使等式成立. eg: 思考:如果某一列已经成立,如上图的1+4=5,他一定可以加到前面最长的成立的等式上,类似于最长上升子序列,不过要n^2扫. 做题时WA了几发,因为没考虑到等式的最高位不能有进位,以及可能某一列一开始没有进位,但是后来由于前面低位的进位导致了自己的进位(这种情况只会出现在初始和为9,低位又进一的情况). 1 #i

ACM-ICPC 2017 Asia Urumqi A. Coins【期望dp】

题目链接:https://www.jisuanke.com/contest/2870?view=challenges 题目大意:给出n个都正面朝下的硬币,操作m次,每次都选取k枚硬币抛到空中,求操作m次后,硬币向上的期望值. 思路: 1.期望跟概率还是有点不同的,期望要枚举出抛的所有的情况,然后求sigma(i * dp[][]) 2.dp[i][j]表示进行i次操作后,有j枚硬币向上的概率.这样就可以求最后的硬币向上的期望了. 3.值得注意的是,预处理的组合数要开 double 型. 代码:

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6206 题目: Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 530    Accepted Submission(s): 172 Problem Description Apple is Taotao's favouri

2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 1496    Accepted Submission(s): 723 Problem Description Kelukin is a businessman. Every day, he travels arou

2014 ACM/ICPC Asia Regional Guangzhou Online Wang Xifeng&#39;s Little Plot HDU5024

一道好枚举+模拟题目.转换思维视角 这道题是我做的,规模不大N<=100,以为正常DFS搜索,于是傻乎乎的写了起来.各种条件限制模拟过程 但仔细一分析发现对每个点进行全部八个方向的遍历100X100X100^8 .100X100个点,每个点在走的时候8中选择,TLE 于是改为另一个角度: 以符合要求的点为拐弯点,朝两个垂直的方向走,求出最远的距离.这样只要对每个点各个方向的长度知道,组合一下对应的就OK. 避免了每个点深搜. PS:搜索的时候x,y写反了,导致构图出现问题,以后用[dy][dx]

poj 5024&amp;&amp;&amp;2014 ACM/ICPC Asia Regional Guangzhou Online 1003(预处理)

http://acm.hdu.edu.cn/showproblem.php?pid=5024 分析:预处理每个点在八个方向的射线长度,再枚举八种L形状的路,取最大值. 注意题意是求一条最长路,要么一条直线,要么只有一个90角,即L型.其实直线就是L形的一个方向长度为0. 代码: #include<iostream> #include<map> #include<cstdio> #include<string> #include<cstring>

HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 309 Problem Description A mysterious country will hold a football world championships---Abnormal Cup