2014 The fifth annual ACM University Student Program Design contest Problem Solving report

Source: Internet
Author: User
Tags cmath

A Angry_birds_again_and_again


Mathematical problems, to find the parabolic line and the area of the line, with integral to do.

Set the equation Y=ax^2+bx+c, the curve passes through the origin point, so c=0.
The derivation of the equation y ' =2ax+b, y ' stands for the slope, then the origin (0,0) This point, Generation Y ' =b, that is, the slope of the point, according to test instructions B=tan (α)
In the title X=tx this point, easy to mix, remember that TX is T, the figure of the curve x=t this point, the slope of 2at+b. Notice that the slope is negative.
The opposite number of the triangle's vertical right-angled edge divided by the horizontal right-angled edge (P-T) is the slope 2at+b
The vertical right-angled edge value is AT^2+BT (the T is brought into the original equation), and the horizontal right side is p-t.
Solve A, so that the value of a, b in the equation has.
So the area of the topic is the curve covering area from 0 to T integral integral function (AX^2+BX), plus the area of the Triangle 0.5* (p-t) * (AT^2+BT)

#include <iostream> #include <cmath> #include <iomanip>using namespace Std;double px,tx,jiao;double A , B;int Main () {    int t;cin>>t;    while (t--)    {        cin>>px>>tx>>jiao;        B=tan (jiao);        Double m=px-tx;        A= (-b*tx-b*m)/(2*TX*M+TX*TX);        Double ans;        Ans= (1/3.0) *a*tx*tx*tx+0.5*b*tx*tx+0.5* (PX-TX) * (A*TX*TX+B*TX);        Cout<<setiosflags (ios::fixed) <<setprecision (3) <<ans<<endl;    }    return 0;}

B Circle


The desired topic, with Gauss elimination of the element can be.

e[x]=0.5* (E[x-1]+1] + 0.5* (e[x+1]+1), e[x] are the average number of steps that need to go for the X-point distance to the destination, the end point is expected to be 0, and the answer is the e[starting point].

#include <iostream> #include <string.h> #include <iomanip> #include <cmath> using namespace St  D  const int maxn=1002;  const double eps=1e-12;  Double A[MAXN][MAXN];  int equ,var;//equ an equation, var variable double x[maxn];//solution set bool FREE_X[MAXN];    int n;  int SGN (double x) {return (x>eps)-(x<-eps);      } int Gauss () {equ=n,var=n;      int i,j,k; int max_r;      The row with the highest absolute value for the current column. int col;      The currently processed column.      Double temp;      int free_x_num;      int free_index;      Converted to a stepped array. col=0;      The currently processed column.      memset (free_x,true,sizeof (free_x));          for (k=0;k<equ&&col<var;k++,col++) {max_r=k; for (i=k+1;i<equ;i++) {if (SGN (Fabs (A[i][col))-fabs (A[max_r][col]) >0) max_r=i          ;              } if (max_r!=k) {//is exchanged with line K.          for (j=k;j<var+1;j++) swap (a[k][j],a[max_r][j]); } if (SGN (A[k][col)) ==0) {//indicates that the COL column is below line KIs all 0, the next column of the current row is processed. k--;          Continue              } for (i=k+1;i<equ;i++) {//enumerates the rows to be deleted.                  if (SGN (A[i][col))!=0) {Temp=a[i][col]/a[k][col];                  for (j=col;j<var+1;j++) {a[i][j]=a[i][j]-a[k][j]*temp;  }}}} for (i=k;i<equ;i++) {if (SGN (A[i][col])!=0) return      0;              } if (K<var) {for (i=k-1;i>=0;i--) {free_x_num=0; for (j=0;j<var;j++) {if (SGN (A[i][j])!=0&&free_x[j]) free_x_              Num++,free_index=j;              } if (free_x_num>1) continue;              Temp=a[i][var]; for (j=0;j<var;j++) {if (SGN (a[i][j))!=0&&j!=free_index) Temp              -=A[I][J]*X[J]; } x[free_index]=tEmp/a[i][free_index];          free_x[free_index]=0;      } return var-k;          } for (i=var-1;i>=0;i--) {Temp=a[i][var];          for (j=i+1;j<var;j++) {if (SGN (a[i][j))!=0) temp-=a[i][j]*x[j];      } X[i]=temp/a[i][i];  } return 1;    } int t,xx;      int main () {cin>>t;          while (t--) {cin>>n>>xx;          memset (A,0,sizeof (a));                  for (int i=0;i<n;i++) {if (i==xx) {a[i][i]=1;                  a[i][n]=0;              Continue              } a[i][i]=1;              A[i][n]=1;              a[i][(i-1+n)%n]=-0.5;          a[i][(i+1)%n]=-0.5;          } gauss ();      Cout<<setiosflags (ios::fixed) <<setprecision (4) <<x[0]<<endl;  } return 0; }

D Devour Magic


Segment tree, the operation is: the whole interval [1,n] together with a number, query the specified interval of the and, the specified interval clear 0.

Use two lazy, one is increment, one is whether clear 0,pushdown, first see whether clear 0, because the interval clear 0 marked after, the following sub-node increment mark even if there is, also does not work, want to remove.

Output can not be used i64d, replaced by cout ...

#include <iostream> #include <stdio.h> #include <algorithm> #include <string.h> #include < stdlib.h> #include <cmath> #include <iomanip> #include <vector> #include <set> #include < map> #include <stack> #include <queue> #include <cctype>using namespace std;typedef long Long ll;    const int maxn=100010;struct st{int l,r;    ll sum; ll lazy;//Lazy Mark ll lazy0;//whether the interval is clear 0}st[maxn<<2];void pushup (int i) {st[i].sum=st[i<<1].sum+st[(i<<1 ) |1].sum;}         void pushdown (int i,int len) {if (st[i].lazy0!=0) {st[i<<1].lazy0=st[(i<<1) |1].lazy0=st[i].lazy0;        st[i<<1].sum=0;        st[(i<<1) |1].sum=0;        st[i<<1].lazy=0;        st[(i<<1) |1].lazy=0;    st[i].lazy0=0;        } if (st[i].lazy!=0) {st[i<<1].lazy+=st[i].lazy;        st[(i<<1) |1].lazy+=st[i].lazy;        St[i<<1].sum+=ll (len-(len>>1)) *st[i].lazy; st[(i<< 1) |1].sum+=ll (len>>1) *st[i].lazy;    st[i].lazy=0;    }}void Build (int i,int l,int r) {st[i].l=l;st[i].r=r;    st[i].lazy=st[i].lazy0=0;    st[i].sum=0;    if (ST[I].L==ST[I].R) return;    int mid= (ST[I].L+ST[I].R) >>1;    Build (I<<1,l,mid); Build ((i<<1) |1,mid+1,r);} void update (int i,int l,int r,int val) {if (val!=0) {if (st[i].l==l&&st[i].r==r) {s            T[i].lazy+=val;            St[i].sum+=ll (r-l+1) *val;        Return            }} else {if (st[i].l==l&&st[i].r==r) {st[i].sum=0;            St[i].lazy0=1;        st[i].lazy=0;//ensure that the current node maintenance value is correct, do not forget this sentence return;        } pushdown (i,st[i].r-st[i].l+1);        int mid= (ST[I].L+ST[I].R) >>1;        if (r<=mid) update (i<<1,l,r,val);        else if (l>mid) update ((i<<1) |1,l,r,val);            else {update (i<<1,l,mid,val); Update ((i<<1) |1,mid+1,r,val);    } pushup (i);    }}ll query (int i,int l,int R) {if (st[i].l==l&&st[i].r==r) {return st[i].sum;    } int mid= (ST[I].L+ST[I].R) >>1;    Pushdown (i,st[i].r-st[i].l+1);    if (r<=mid) return query (I&LT;&LT;1,L,R);    else if (l>mid) return query ((i<<1) |1,l,r); else return query (I<<1,l,mid) +query ((i<<1) |1,mid+1,r);}    int T[maxn];int n,q;ll Ans;int main () {int cas;    t[0]=0;    scanf ("%d", &cas);        while (cas--) {ans=0;        scanf ("%d%d", &n,&q);        Build (1,1,n);            for (int i=1;i<=q;i++) {int l,r;            scanf ("%d%d%d", &t[i],&l,&r);            Update (1,1,n,t[i]-t[i-1]);            Ans+=query (1,L,R);        Update (1,l,r,0);        }//printf ("%i64d\n", ans);    cout<<ans<<endl; } return 0;}

E factorial

Water problem, ask for factorial within 10

#include <iostream>  using namespace std;  int f[11];    void Pre ()  {      f[0]=1;      for (int i=1;i<=10;i++)          f[i]=f[i-1]*i;  }    int main ()  {      pre ();      int t,n;      cin>>t;      while (t--)      {          cin>>n;          cout<<f[n]<<endl;      }      return 0;  }  

F Full Binary Tree


Full two fork tree, the shortest distance of two nodes, first let the following node jump to the top of the node on the layer, record the number of steps, and then two nodes a jump, until meet, record steps, two steps to add on it.

#include <iostream> using namespace std;  int f[10000];    int Len;      void Pre () {f[0]=1; for (int i=1;;          i++) {f[i]=f[i-1]*2;              if (f[i]>1e9) {len=i-1;          Break  }}} int a, b;    int t;      int main () {pre ();      cin>>t;          while (t--) {cin>>a>>b;          if (a>b) swap (A, b);          int cnt=0;          int l;                   for (int i=0;i<len;i++) {if (a>=f[i]&&a<f[i+1]) {l=i;              Break              }} and (1) {if (b>=f[l]&&b<f[l+1]) break;              b/=2;          cnt++;              } while (a!=b) {a/=2;              b/=2;          cnt+=2;      } cout<<cnt<<endl;  } return 0; }

G Hearthstone II


Test instructions for n races to use the M-table, each table is used at least once, the table is different, ask how many kinds of arrangement method.
That is, to divide n elements into m non-empty and indistinguishable sets. The second class of stiring s (n,m) means to divide n elements into m non-empty and indistinguishable sets. The subject set (table) is distinguishable, then the answer is m! *s (n,m).

#include <iostream>  #include <string.h>  using namespace std;  const int maxn=102;  const int mod=1e9+7;  typedef long long LL;  ll S[MAXN][MAXN];  int n, m;    void init ()  {      memset (s,0,sizeof (s));      S[1][1]=1;      for (int i=2;i<=100;i++) for          (int j=1;j<=i;j++)              {                  s[i][j]=s[i-1][j-1]+j*s[i-1][j];                  if (s[i][j]>=mod)                      s[i][j]%=mod;              }  }    ll solve (int n,int m)  {      ll ans=s[n][m];      for (int i=2;i<=m;i++)      {          ans*=i;          if (ans>=mod)              ans%=mod;      }      return ans;  }    int main ()  {      init ();      while (cin>>n>>m)      {          cout<<solve (n,m) <<endl;      }      return 0;  }

J Weighted Median


Use the sort of water over.

#include <iostream> #include <algorithm> #include <stdio.h>using namespace std;const int maxn=1e7+2; int n;struct node{int x,w;}    Node[maxn];bool CMP (Node A,node b) {if (a.x<b.x) return true; return false;}        int main () {while (scanf ("%d", &n)!=eof) {Long long sum=0;        for (int i=1;i<=n;i++) scanf ("%d", &node[i].x);            for (int i=1;i<=n;i++) {scanf ("%d", &AMP;NODE[I].W);        SUM+=NODE[I].W;        } long double s=sum*0.5;        Sort (node+1,node+1+n,cmp);        Long Long xiao=0,da=0;        int ans;            for (int i=1;i<=n-1;i++) {XIAO+=NODE[I].W;            DA=SUM-XIAO-NODE[I+1].W;                if (xiao<s&&da<=s) {ans=node[i+1].x;            Break    }} cout<<ans<<endl; } return 0;}

2014 Shandong Province Fifth annual ACM University Student Program Design contest problem Solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.