1. 程式人生 > >組隊賽(11周)

組隊賽(11周)

7783 Which Base is it Anyway?

Programming languages such as C++ and Java can prefix characters to denote the base of constant
integer values. For example, hexadecimal (base 16) constants are preceded by the string “0x”. Octal
(base 8) values are preceded by the character “0” (zero). Decimal (base 10) values do not have a prefix.
For example, all the following represent the same integer constant, albeit in different bases.
0x1234
011064
4660
The prefix makes it clear to the compiler what base the value is in. Without the “0x” prefix, for
example, it would be impossible for the compiler to determine if 1234 was hexadecimal. It could be
octal or decimal.
For this problem, you will write a program that interprets a string of decimal digits as if it were an
octal value, a decimal value or a hexadecimal value.
Input
The first line of input contains a single decimal integer P , (1 ≤ P ≤ 10000), which is the number of
data sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by a
single space, followed by a string of at most 7 decimal digits.
Output
For each data set there is one line of output. The single output line consists of the data set number,
K, followed by a space followed by 3 space separated decimal integers which are the value of the input
as if it were interpreted to as octal, decimal and hexadecimal respectively. If the input value cannot be
interpreted as an octal value, use the value ‘0’.
Sample Input

4
1 1234
2 9
3 1777
4 129


Sample Output
 

1 668 1234 4660
2 0 9 9
3 1023 1777 6007
4 0 129 297

題意:有一個不確定是幾進位制的數,需要你輸出他是8進位制的時候對應的十進位制,十進位制的時候對應的十進位制,16進位制對應的十進位制。

思路:一個個轉換就好了。

#include<iostream>
#include <algorithm>
#include <cstdlib>
#include<cstdio>
#include<cstring>
#include <cmath>
using namespace std;
const int M=200000+10;
const int MAX=0x3f3f3f3f;
typedef long long ll;
int main()
{
    ll T,i,a,b,c,n,m;
    cin>>T;
    while(T--)
    {
        cin>>m>>n;
        cout<<m<<" ";
        a=1;
        ll ans=0;
        ll p=n;
        ll flag=0;
        while(p)
        {
            if(p%10>=8)
                flag=1;
            ans+=p%10*a;
            p/=10;
            a*=8;
        }
        if(flag==0)
          cout<<ans<<" "<<n<<" ";
        else
            cout<<"0"<<" "<<n<<" ";
        p=n;
        a=1;
        ans=0;
        while(p)
        {
            ans+=p%10*a;
            p/=10;
            a*=16;
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

7786 A Rational Sequence (Take 3)

An infinite full binary tree labeled by positive rational numbers is defined by:
• The label of the root is 1/1.
• The left child of label p/q is p/(p+q).
• The right child of label p/q is (p+q)/q.
The top of the tree is shown in the following figure:
A rational sequence is defined by doing a level order (breadth first) traversal of the tree (indicated
by the light dashed line). So that:
F (1) = 1/1, F (2) = 1/2, F (3) = 2/1, F (4) = 1/3, F (5) = 3/2, F (6) = 2/3, . . .
Write a program to compute the n-th element of the sequence, F (n). Does this problem sound
familiar? Well it should! We had variations of this problem at the 2014 and 2015 Greater NY Regionals.
Input
The first line of input contains a single integer P , (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set should be processed identically and independently. Each data set consists of a single line of input. It contains the data set number, K, and the index,N , of the sequence element to compute (1 ≤ N ≤ 2147483647).
Output
For each data set there is a single line of output. It contains the data set number, K, followed by a single space which is then followed by the numerator of the fraction, followed immediately by a forward slash (‘/’) followed immediately by the denominator of the fraction. Inputs will be chosen so neither the numerator nor the denominator will overflow an 32-bit unsigned integer.ACM-ICPC Live Archive: 7786 – A Rational Sequence (Take 3)
Sample Input

4
1 1
2 4
3 11
4 1431655765


Sample Output
 

1 1/1
2 1/3
3 5/2
4 2178309/1346269

題意:是左子樹應該返回p/(p+q),右子樹應該返回(p+q)/q,其中pq是其父節點的標號。

思路:因為數比較大,陣列根本開不了,可以用遞迴,返回結構體的形式。

#include<iostream>
#include <algorithm>
#include <cstdlib>
#include<cstdio>
#include<cstring>
#include <cmath>
using namespace std;
const int M=2147483647+10;
const int MAX=0x3f3f3f+10;
typedef long long ll;
struct AC
{
    ll a=1;
    ll b=1;
    ll c;
}A,k,kk;
AC f(ll n)
{
    if(n==1)
    {
        A.a=1;
        A.b=1;
        return A;
    }

    if(n%2!=0)
    {
        k=f(n/2);
        A.a=k.a+k.b;
        A.b=k.b;
    }
    else
    {
        k=f(n/2);
        A.a=k.a;
        A.b=k.a+k.b;
    }
    return A;
}
int main()
{
    ll T,i,c,n,m;
    cin>>T;
    while(T--)
    {
        cin>>m>>n;
        cout<<m<<" ";
        kk=f(n);
        cout<<kk.a<<"/"<<kk.b<<endl;
    }
    return 0;
}

7790 DA-Sort

You recently learned a new way to sort an array of numbers in your algorithms course. The algorithm
sorts an array of numbers by repeatedly performing the Delete-and-Append operation. The Delete-and-
Append operation consists of three steps:
1) Choose an element from the array.
2) Delete the chosen element from the array.
3) Append the chosen element to the end of the array.
Being a curious student, you wonder what is the minimum number of Delete-and-Append operations
required to sort a given array.
Input
The first line of input contains a single decimal integer P , (1 ≤ P ≤ 100), which is the number of data
sets that follow. Each data set should be processed identically and independently.
Each data set consists of two or more lines of input. The first line contains the data set number, K,
followed by a single space, followed by an integer N , (1 ≤ N ≤ 1000), which is the length of the array
to sort. The remaining lines in the dataset contains N positive integers that comprise the array to be
sorted, 10 values per line, except for the last line which may have less than 10 values. All the array
elements are no larger than 10 9 . The same value may appear more than once in the array to be sorted.
Output
For each data set there is one line of output. The single output line consists of the data set number, K,
followed by a single space followed by an integer which is the minimum number of Delete-and-Append
operations required to sort the array.

3
1 3
1 3 2
2 6
1 5 2 4 3 6
3 23
67890 56312 999999999 12345 23456
38927 45632 100345 98765 23456
87654 43278 23456 117654 321899
25432 54326 217435 26845 31782
33456 41234 56213

 

1 1
2 3
3 15

題意:將無序的一些數變為有序的,他可以刪除元素也可以在後面加元素。就是選一個放到後面是一步,問最小几步。

思路:先在b陣列中存入a中排好序的數,如果對應位置相等就i++,j++.不是就將j++,計算n-j+1的值。

#include<iostream>
#include <algorithm>
#include <cstdlib>
#include<cstdio>
#include<cstring>
#include <cmath>
using namespace std;
const int M=2147483647+10;
const int MAX=0x3f3f3f+10;
typedef long long ll;
int main()
{
    ll a[2000],b[2000];
    ll ans,i,j,sum,T,m,p,n;
    cin>>T;
    while(T--)
    {
        cin>>m>>n;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
            b[i]=a[i];
        }
        sort(b+1,b+n+1);
        j=1;
        for(i=1;i<=n;i++)
        {
            if(a[i]==b[j])
                j++;
        }
        cout<<m<<" "<<n-j+1<<endl;

    }
    return 0;
}

FBI Universal Control Numbers

The FBI has recently changed its Universal Control Numbers (UCN ) for identifying individuals who
are in the FBI’s fingerprint database to an eight digit base 27 value with a ninth check digit. The digits
used are:
0123456789ACDEFHJKLMNPRTVWX
Some letters are not used because of possible confusion with other digits:
B->8, G->C, I->1, O->0, Q->0, S->5, U->V, Y->V, Z->2
The check digit is computed as:
(2 ∗ D 1 + 4 ∗ D 2 + 5 ∗ D 3 + 7 ∗ D 4 + 8 ∗ D 5 + 10 ∗ D 6 + 11 ∗ D 7 + 13 ∗ D 8 ) mod 27
Where D n is the n-th digit from the left.
This choice of check digit detects any single digit error and any error transposing an adjacent pair
of the original eight digits.
For this problem, you will write a program to parse a UCN input by a user. Your program should
accept decimal digits and any capital letter as digits. If any of the confusing letters appear in the input,
you should replace them with the corresponding valid digit as listed above. Your program should
compute the correct check digit and compare it to the entered check digit. The input is rejected if they
do not match otherwise the decimal (base 10) value corresponding to the first eight digits is returned.
Input
The first line of input contains a single decimal integer P , (1 ≤ P ≤ 10000), which is the number of
data sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by a
single space, followed by 9 decimal digits or capital (alphabetic) characters.
Output
For each data set there is one line of output. The single output line consists of the data set number, K,
followed by a single space followed by the string ‘Invalid!’ (without the quotes) or the decimal value
corresponding to the first eight digits.
Sample Input

3
1 12345678A
2 12435678A
3 12355678A


Sample Output
 

1 11280469652
2 Invalid
3 Invalid 

題意:一個字串長度是9,前8位是一個數字,根據題目中的公式求出ans%27是否與最後一個字元所代表的位置上的數相等,相等就把a[0]到a[7]當做27進位制輸出相應的10進位制,不是輸出Invalid。

思路:有坑就是前8個數字不一定是0到9,有可能是字母,首先還要判斷一下有沒有不應該出現的字元,先更改一下。

#include<iostream>
#include <algorithm>
#include <cstdlib>
#include<cstdio>
#include<cstring>
#include <cmath>
using namespace std;
const int M=2147483647+10;
const int MAX=0x3f3f3f+10;
typedef long long ll;
int main()
{
    char a[10],b[30]="0123456789ACDEFHJKLMNPRTVWX";
    int c[10]={2,4,5,7,8,10,11,13};
    ll ans,i,j,sum,T,m,p;
    cin>>T;
    while(T--)
    {
        memset(a,0,sizeof(a));
        cin>>m>>a;
        cout<<m<<" ";
        for(i=0;i<9;i++)
        {
            if(a[i]=='B')
                a[i]='8';
            else
            if(a[i]=='G')
                a[i]='C';
            else
            if(a[i]=='I')
                a[i]='1';
            else
            if(a[i]=='O'||a[i]=='Q')
                a[i]='0';
            else
            if(a[i]=='S')
                a[i]='5';
            else
            if(a[i]=='U'||a[i]=='Y')
                a[i]='V';
            else
            if(a[i]=='Z')
                a[i]='2';
        }
        ans=0;
        for(i=0;i<8;i++)
        {
            for(j=0;j<27;j++)
                if(a[i]==b[j])
                   ans+=j*c[i];
        }
        for(i=0;i<27;i++)
        {
            if(a[8]==b[i])
            {
                p=i;
                break;
            }
        }
        if(ans%27!=p)
            cout<<"Invalid"<<endl;
        else
        {
            ll f=1,sum=0;
            for(i=7;i>=0;i--)
            {
                for(j=0;j<27;j++)
                {
                    if(a[i]==b[j])
                    {
                        sum+=j*f;
                        f*=27;
                    }
                }
            }
            cout<<sum<<endl;
        }
    }
    return 0;
}

m-ary Partitions

A partition of an integer n is a set of positive integers which sum to n, typically written in descending
order. For example:
10 = 4+3+2+1
A partition is m-ary if each term in the partition is a power of m. For example, the 3-ary partitions
of 9 are:
9
3+3+3
3+3+1+1+1
3+1+1+1+1+1+1
1+1+1+1+1+1+1+1+1
Write a program to find the number of m-ary partitions of an integer n.
Input
The first line of input contains a single decimal integer P , (1 ≤ P ≤ 1000), which is the number of data
sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. The line contains the data set number, K, followed by
the base of powers, m, (3 ≤ m ≤ 100), followed by a space, followed by the integer, n, (3 ≤ n ≤ 10000),
for which the number of m-ary partitions is to be found.
Output
For each data set there is one line of output. The output line contains the data set number, K, a space,
and the number of m-ary partitions of n. The result should fit in a 32-bit unsigned integer. 

5
1 3 9
2 3 47
3 5 123
4 7 4321
5 97 9999



OUT
1 5
2 63
3 75
4 144236
5 111

 題意:輸入兩個數,計算用a的幾次方所有的數下,有幾種方式和相加為b.

思路:完全揹包的思想,先計算有多少件商品,就是計算a的幾次方最接近b(小於b的前提下),求揹包容量。

揹包模板

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#define MAX 1000000
using namespace std;
typedef long long ll;
ll dp[10100];
ll fast_pow(ll a,ll b)
{
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=ans*a;
        a=a*a;
        b>>=1;
    }
    return ans;
}
int main()
{
      ll m=1,n,i,j,t,o,c;
      cin>>t;
      while(t--)
      {
          cin>>o>>c>>n;
          m=1;
          while(1)
          {
              if(fast_pow(c,m)>n)
                break;
              else
                m++;
          }
          for(i=0;i<=n;i++)
            dp[i]=1;
          for(i=1;i<=m;i++)
          {
              ll p=fast_pow(c,i);
              for(j=p;j<=n;j++)
              {
                  dp[j]+=dp[j-p];
              }

          }
          cout<<o<<" "<<dp[n]<<endl;
      }
      return 0;
}

Permutation Descent Counts

INPUT
4
1 3 1
2 5 2
3 8 3
4 99 50
OUT
1 4
2 66
3 15619
4 325091

 題意:計算P(a,b)的值,在1到a個數中找到前面比後面大的數有b次有多少種。

eg:P(3, 0) = 1{[123]}
P(3, 1) = 4{[132], [213], [231], 312]}
P(3, 2) = 1{[321]}

思路:dp[i][j]表示第i個數的排列是逆序數對的個數為j個。然後找出狀態轉移方程。dp[i][j] = (dp[i-1][j]*(j+1)+dp[i-1][j-1]*(i-j));dp[i][0]總是1,dp[i][i]是0。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
#define MAX 1000000
using namespace std;
typedef long long ll;
ll dp[110][110];
int main()
{
    ll T,cnt,c=1001113;
    cin>>T;
    while(T--)
    {
       ll m,n,i,j;
       cin>>cnt>>n>>m;
       memset(dp,0,sizeof(dp));
       for(i=1;i<=n;i++)
       {
           for(j=0;j<=m;j++)
           {
               if(j==0)
                  dp[i][j]=1;
               else
                if(j==i)
                   dp[i][j]=0;
               else
                dp[i][j]=(dp[i-1][j]*(j+1)+dp[i-1][j-1]*(i-j))%c;
           }
       }
       cout<<cnt<<" "<<dp[n][m]%c<<endl;
    }
}