问题 B: Das blinkenlights

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

There are two lights that blink at regular intervals. When each one blinks, it turns on and then immediately back off; they don’t toggle. They are both off at time t=0. The first one blinks at t=p,2p,3p,… seconds; the second one blinks at t=q,2q,3q,… seconds. Once they start, they both keep blinking forever. It is very exciting to see them blink at the same time (on the same second). But your patience is going to run out eventually, in s seconds. Will they blink at same time between t=1 and t=s (inclusive)? Write a program that can answer this question, quick, before they start blinking again!

Figure 1: Illustration of the sample inputs. A black circle means the light is off, a white circle means the light blinks at that second. The arrows above point out times when both lights blink.

输入

Input consists of one line containing three space-separated integers p, q, and s. The bounds are 1≤p,q≤100 and 1≤s≤10000. The first light blinks every p seconds, the second every q seconds. The value of s represents the maximum number of seconds to consider when determining if the two lights blink at the same time.

输出

Output yes if the two lights blink on the same second between time 1 and time s, or no otherwise.

样例输入

2 5 20

样例输出

yes

暴力模拟大水题

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
 
using namespace std;
typedef  long long ll;
const int N  = 10010;
typedef pair<int,int>P;
 
bool vis1[N],vis2[N];
int main()
{
    int p,q,s;
    scanf("%d%d%d",&p,&q,&s);
    for(int i=p;i<N;i+=p) vis1[i]=true;
    for(int i=q;i<N;i+=q) vis2[i]=true;
    bool flag=false;
    for(int i=1;i<=s;i++)
    {
        if(vis1[i] && vis2[i])
        {
            flag=true;
            break;
        }
    }
    if(flag) puts("yes");
    else puts("no");
    return 0;
}

问题 A: Bingo Ties

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

Bingo is a game of chance played by a group of players. Each player has his or her own bingo card with a 5-by-5 grid of numbers. Each number appears at most once per card. The bingo caller calls out a sequence of randomly drawn numbers, and the players mark the numbers that appear on their card as they are called out. The winner is the player that completes a line of five marked numbers (horizontally, vertically or diagonally) on his or her card. The winning player then yells “bingo” and the game ends.

You have been volunteering with a local youth group and have been running bingo games for the children. They love bingo, but every time two or more kids yell “bingo” at the same time, a spirited “disagreement” breaks out. You’ve created a slightly modified version of bingo (in the hopes of introducing fewer ties): the cards are 5-by-5 grids with a number from 1 to 3000 in each of the 25 cells, and a winner is only declared when a player has 5 numbers in a row. Note that in this new game, players cannot win via columns or diagonals.

Alas, these changes did not eliminate ties or the subsequent disagreements. To prevent further disagreements, you’ve decided to analyze the sets of cards to determine if there is any possibility that a tie (where two kids can yell bingo at the same time) can occur. Write a program that takes a collection of bingo cards and determines if there is any possible sequence of numbers that could be called so that the game ends, and a tie between two or more players occurs, when the last number in the sequence is called.

For example, consider the following two bingo cards:

Then this set of two cards could result in a tie if the sequence of numbers called was :40 61 64 10 57 49 11 31 25

This sequence would result in the card on the left completing the third row and the card on the right completing the fourth row when the number 25 is called.

输入

The first line of the input is an integer n (2≤n≤100), the number of bingo cards. After the first line are the n bingo cards, each separated from the next by a blank line of input.

Each bingo card consists of five lines of input. Each line consists of five integers in the range from 1 to 3000. The numbers on each bingo card are unique.

输出

If no ties are possible between any two cards, output “no ties”. Otherwise, output the two numbers a and b (1≤a<b≤n) identifying the two cards for which a tie could occur, where the cards are numbered from 1 to n in the order that they appear in the input. If multiple pairs of cards can tie, output the pair with the smallest a, breaking any remaining ties with the smallest b.

样例输入

2

3 29 45 56 68

1 19 43 50 72

11 25 40 49 61

9 23 31 58 63

4 27 42 54 71

14 23 39 59 63

8 17 35 55 61

15 26 42 53 71

10 25 31 57 64

6 20 44 52 68

样例输出

1 2

暴力大模拟

两个相等确实可以一起叫bingo,但其他八个可能会使得其他的情况有人提前叫bingo了,所以要check一下

#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef  long long ll;
struct node
{
    int numbers[6][6];
}card[110];
void getacard(int n){
    for(int i=1;i<=5;i++){
        for(int j=1;j<=5;j++){
            scanf("%d",&card[n].numbers[i][j]);
        }
    }
}
int n;
bool check(int i,int j,int x,int y,int val1,int val2){
    map<int,bool>mmap;
    for(int o=1;o<=5;o++){
        if(card[i].numbers[x][val1]!=card[i].numbers[x][o])
            mmap[card[i].numbers[x][o]]=true;
        if(card[j].numbers[y][val2]!=card[j].numbers[y][o])
            mmap[card[j].numbers[x][o]]=true;
    }
    if(mmap.size()<5)return true;
    for(int a=0;a<n;a++){
        for(int b=1;b<=5;b++){
            int cnt=0;
            for(int c=1;c<=5;c++){
                if(mmap[card[a].numbers[b][c]])cnt++;
            }
            if(cnt==5) return false;
        }
    }
    return true;
}
bool judge(int i,int j,int x,int y){
    int val1=0,val2=0;
    for(val1=1;val1<=5;val1++){
    for(val2=1;val2<=5;val2++){
        if(card[i].numbers[x][val1]==card[j].numbers[y][val2]){
            if(check(i,j,x,y,val1,val2))
                return true;
        }
    }
    }
    return false;
}
int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++) getacard(i);
    for(int i=0;i<n-1;i++){
    for(int j=i+1;j<n;j++){
        for(int x=1;x<=5;x++){
        for(int y=1;y<=5;y++){
            if(judge(i,j,x,y)){
                printf("%d %d\n",i+1,j+1);
                return 0;
            }
        }
        }
    }
    }
    printf("no ties\n");
    return 0;
}

问题 F: The King’s Ups and Downs

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

The king has guards of all different heights. Rather than line them up in increasing or decreasing height order, he wants to line them up so each guard is either shorter than the guards next to him or taller than the guards next to him (so the heights go up and down along the line). For example, seven guards of heights 160, 162, 164, 166, 168, 170 and 172 cm. could be arranged as:

or perhaps:

The king wants to know how many guards he needs so he can have a different up and down order at each changing of the guard for rest of his reign. To be able to do this, he needs to know for a given number of guards, n, how many different up and down orders there are:

For example, if there are four guards: 1, 2, 3, 4 can be arranged as:

1324, 2143, 3142, 2314, 3412, 4231, 4132, 2413, 3241, 1423

For this problem, you will write a program that takes as input a positive integer n, the number of guards and returns the number of up and down orders for n guards of differing heights.

输入

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 consists of single line of input containing two integers. The first integer, D is the data set number. The second integer, n (1 ≤ n ≤ 20), is the number of guards of differing heights.

输出

For each data set there is one line of output. It contains the data set number (D) followed by a single space, followed by the number of up and down orders for the n guards.

样例输入

4

1 1

2 3

3 4

4 20

样例输出

1 1

2 4

3 10

4 740742376475050

20 打表过题

#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef  long long ll;
const int MAX=30;
int main()
{
    ll ans[21]={
        0,1,2,4,10,32,122,544,2770,15872,101042,707584,5405530,44736512,
        398721962,
        3807514624,
        38783024290,
        419730685952,
        4809759350882,
        58177770225664,
        740742376475050
    };
    int _;
    scanf("%d",&_);
    int a,b;
    while(_--){
        cin>>a>>b;
        cout<<a<<" "<<ans[b]<<endl;
    }
    return 0;
}

打表就是一个dp加组合数

#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef  long long ll;
const int MAX=30;
ll ans[100],dp[30][3],C[30][30];
void yanghui(int n){
    int i,j;
    memset(C,0,sizeof(C));
    for(i=0;i<=20;i++)
    {
        C[i][0]=1;
    }
    for(i=1;i<=20;i++)
    {
        for(j=1;j<=20;j++)
        {
            C[i][j]=C[i-1][j]+C[i-1][j-1];
        }
    }

}
int main()
{
    yanghui(30);
    ans[1]=1;
    dp[0][0]=dp[0][1]=dp[1][0]=dp[1][1]=1;
    for(int k=2;k<=20;k++)
    {
        ll cnt=0;
        for(int i=0;i<k;i++)
        {
            int j=k-1-i;
            cnt+=dp[i][0]*dp[j][1]*C[k-1][i];
        }
        ans[k]=cnt;                 
        dp[k][0]=dp[k][1]=cnt>>1;
    }   

    for(int i=1;i<=20;i++){
        cout<<ans[i]<<endl;
    }
    return 0;
}

问题 E: Faulhaber’s Triangle

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

The sum of the m-th powers of the first n integers

can be written as a polynomial of degree m + 1 in n:

For example:

S(n, 1) = (1 + . . . + n) = (1/2) ∗ n2+ (1/2) ∗ n

S(n, 2) = (1 + . . . + n2) = (1/3) ∗ n3+ (1/2) ∗ n2+ (1/6) ∗ n

S(n, 3) = (1 + . . . + n3) = (1/4) ∗ n4+ (1/2) ∗ n3) + (1/4) ∗ n2

S(n, 4) = (1 + . . . + n4) = (1/5) ∗ n5+ (1/2) ∗ n4) + (1/3) ∗ n3 − (1/30) ∗ n

The coefficients F(m, k) of these formulas form Faulhaber’s Triangle:

1

1/2 1/2

1/6 1/2 1/3

0 1/4 1/2 1/4

-1/30 0 1/3 1/2 1/5

0 -1/12 0 5/12 1/2 1/6

1/42 0 -1/6 0 1/2 1/2 1/7

where rows m start with 0 (at the top) and columns k go from 1 to m + 1

Each row of Faulhaber’s Triangle can be computed from the previous row by:

a) The element in row i and column j (j > 1) is (i/j) ∗ (theelementaboveleft); that is: F(i, j) =(i/j) ∗ F(i − 1, j − 1)

b) The first element in each row F(i, 1) is chosen so the sum of the elements in the row is 1.

Write a program to find entries in Faulhaber’s Triangle as decimal fractions in lowest terms .

输入

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 consisting of three space separated decimal integers.

The first integer is the data set number. The second integer is row number m, and the third integer is the index k within the row of the entry for which you are to find F(m, k), the Faulhaber’s Triangle entry (0 ≤ m ≤ 400, 1 ≤ k ≤ m + 1).

输出

For each data set there is a single line of output. It contains the data set number, followed by a single space which is then followed by either the value if it is an integer OR by the numerator of the entry, a forward slash and the denominator of the entry.

样例输入

4

1 4 1

2 4 3

3 86 79

4 400 401

样例输出

1 -1/30

2 1/3

3 -22388337

4 1/401

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
  
public class Main{
    static BigInteger ansx[][] = new BigInteger [405][405];
    static BigInteger ansy[][] = new BigInteger [405][405];
    //static int anss [][] = new int [405][405];
    static BigInteger l = new BigInteger("1");
    static BigInteger ll = new BigInteger("2");
    static BigInteger lll = new BigInteger("400");
    static BigInteger o = new BigInteger("0");
    static void getans() {
        ansx[0][1]=l;ansy[0][1]=l;
        ansx[1][1]=l;ansy[1][1]=ll;
        ansx[1][2]=l;ansy[1][2]=ll;
        int ii=2;
        for(BigInteger i = ll;i.compareTo(lll)<=0;i=i.add(l)) {
            int jj=2;
            BigInteger x = l;
            BigInteger y = l;
            for(BigInteger j = ll;j.compareTo(i.add(l))<=0;j=j.add(l)) {
                ansx[ii][jj]=i.multiply(ansx[ii-1][jj-1]);
                ansy[ii][jj]=j.multiply(ansy[ii-1][jj-1]);
                //anss[ii][jj]=1;
                if(!ansx[ii][jj].equals(o)) {
                    x=x.multiply(ansy[ii][jj]).subtract(y.multiply(ansx[ii][jj]));
                    y=y.multiply(ansy[ii][jj]);
                }
                if(!ansx[ii][jj].equals(o)&&!ansy[ii][jj].equals(0)) {
                    BigInteger d = ansx[ii][jj].gcd(ansy[ii][jj]);
                    ansx[ii][jj] = ansx[ii][jj].divide(d);
                    ansy[ii][jj] = ansy[ii][jj].divide(d);
                }
                jj++;
            }
            BigInteger d = x.gcd(y);
            x=x.divide(d);
            y=y.divide(d);
            ansx[ii][1]=x;
            ansy[ii][1]=y;
            ii++;
        }
         
    }
    public static void main(String[] args) {
        Scanner cin=new Scanner(System.in);
        getans();
        int t = cin.nextInt();
        while (t-->0) {
            int id=cin.nextInt();
            int x=cin.nextInt();
            int y=cin.nextInt();
            if(ansx[x][y].equals(o)||ansy[x][y].equals(l)) {
                System.out.print(id);
                System.out.print(" ");
                System.out.println(ansx[x][y]);
            }
            else {
                System.out.print(id);
                System.out.print(" ");
                if(ansx[x][y].multiply(ansy[x][y]).compareTo(o)<0) {
                    System.out.print("-");
                    if(ansx[x][y].compareTo(o)<0) ansx[x][y]=ansx[x][y].negate();
                    if(ansy[x][y].compareTo(o)<0) ansy[x][y]=ansy[x][y].negate();
                     
                }
                System.out.print(ansx[x][y]);
                System.out.print("/");
                System.out.println(ansy[x][y]);
            }
        }
    }
}

问题 D: Maximum Random Walk

时间限制: 10 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

Consider the classic random walk: at each step, you have a 1/2 chance of taking a step to the left and a 1/2 chance of taking a step to the right. Your expected position after a period of time is zero; that is, the average over many such random walks is that you end up where you started. A more interesting question is what is the expected rightmost position you will attain during the walk.

输入

The first line of input contains a single integer P, (1 ≤ P ≤ 15), 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 consisting of four space-separated values. The first value is an integer K, which is the data set number. Next is an integer n, which is the number of steps to take (1 ≤ n ≤ 1000). The final two are double precision floating-point values L and R which are the probabilities of taking a step left or right respectively at each step (0 ≤ L ≤ 1, 0 ≤ R ≤ 1, 0 ≤ L+R ≤ 1).

Note: the probably of not taking a step would be 1 − L − R.

输出

For each data set there is a single line of output. It contains the data set number, followed by a single space which is then followed by the expected (average) rightmost position you will obtain during the walk, as a double precision floating point value to four decimal places.

样例输入

4

1 1 0.5 0.5

2 4 0.5 0.5

3 10 0.5 0.4

4 1000 0.5 0.4

样例输出

1 0.5000

2 1.1875

3 1.4965

4 3.9995

O(n^3)概率dp,被卡掉了,但其他oj应该能过去,自家oj应该是加了一组15*1000的数据

//O(n^3)
#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef  long long ll;
const int maxn=1010;
int n,s;
double l,r,inp;
double dp[3][maxn<<1][maxn<<1];
int main()
{
    int _;
    scanf("%d",&_);
    while(_--){
        scanf("%d%d%lf%lf",&n,&s,&l,&r);
        inp=1.0-l-r;
        memset(dp,0,sizeof(dp));
        dp[0][1000][1000]=1;
        int f=0;
        for(int i=1;i<=s;i++,f^=1)
            for(int j=1000-i;j<=1000+i;j++){
                for(int k=j>1000?j:1000;k<=1000+i;++k){
                    if(j==k)
                        dp[f^1][j][k]=dp[f][j][k]*inp+dp[f][j-1][k-1]*r+dp[f][j-1][k]*r;
                    else
                        dp[f^1][j][k]=dp[f][j][k]*inp+dp[f][j-1][k]*r+dp[f][j+1][k]*l;
                }
            }
        double ans=0;
        for(int i=1000-s;i<=1000+s;++i)
            for(int j=1000;j<=1000+s;++j)
                ans+=dp[f][i][j]*(j-1000);
        printf("%d %.4f\n",n,ans);
    }
    return 0;
}
//O(n^2)
#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef  long long ll;
const int maxn=1010;
double l,r,inp;
double dp[maxn][maxn];
double solve(int n){
    dp[0][0]=1;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=i;j++){
            dp[i][j]=0;
            if(j) dp[i][j]+=dp[i-1][j-1]*r;
            if(j<i) dp[i][j]+=dp[i-1][j]*inp;
            else dp[i][j]+=inp;
            if(j<i-1) dp[i][j]+=dp[i-1][j+1]*l;
            else dp[i][j]+=l;
        }
    }
    double ans=0;
    for(int i=1;i<=n;i++) ans+=(dp[n][i]-dp[n][i-1])*i;
    return ans;
}
int main()
{
    int _;
    scanf("%d",&_);
    while(_--){
        int n,num;
        scanf("%d %d %lf %lf",&num,&n,&l,&r);
        inp=1-l-r;
        printf("%d %.4lf\n",num,solve(n) );
    }
    return 0;
}

问题 C: Pen Counts

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

Chicken farmer Xiaoyan is getting three new chickens, Lucy, Charlie and CC. She wants to build a chicken pen so that each chicken has its own, unobstructed view of the countryside. The pen will have three straight sides; this will give each chicken its own side so it can pace back and forth without interfering with the other chickens. Xiaoyan finds a roll of chicken wire (fencing) in the barn that is exactly N feet long. She wants to figure out how many different ways she can make a three sided chicken pen such that each side is an integral number of feet, and she uses the entire roll offence. Different rotations of the same pen are the same, however, reflections of a pen may be different (see below).

输入

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 length of the roll of fence, N, (3 ≤ N ≤ l0000).

输出

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 an integer which is the total number of different three-sided chicken pen configurations that can be made using the entire roll offence.

样例输入

5

1 3

2 11

3 12

4 100

5 9999

样例输出

1 1

2 5

3 4

4 392

5 4165834

打表打出来的,有通项公式

a(n) = floor((n^2+3*n+20)/24+(2*n+3)*(-1)^n/16)

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
 
using namespace std;
typedef  long long ll;
const int N  = 10100;
typedef pair<int,int>P;
 
int dp[N];
int main()
{
    int cnt=0;
    dp[3]=1;
    for(int i=3;i<N-50;i+=6,cnt++)
    {
        dp[i+2]=dp[i]+cnt;
        dp[i+4]=dp[i+2]+cnt+1;
        dp[i+6]=dp[i+4]+cnt+2;
    }
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int id,n;
        scanf("%d%d",&id,&n);
        if(n%2==0) n-=3;
        printf("%d %d\n",id,dp[n]);
    }
    return 0;
}

问题 B: Casting

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

Casting around for problems leads us to combine modular arithmetic with different integer bases,particularly the problem of computing values modulo b − 1, where b is the base in which the value is represented. For example,

782910 mod 9 = 8

377777777777777738 mod 7 = 6

1234567 mod 6 = 3

(Note that 377777777777777738 = 112589990684261910 and 1234567 = 2287510.)

Your job is to write a program that reads integer values in various bases and computes the remainder after dividing these values by one less than the input base.

输入

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 containing three space-separated values. The first is an integer which is the data set number. The second is an integer which is the number, B (2 ≤ B ≤ 10),denoting a numeric base. The third is an unsigned number, D, in base B representation. For this

problem, the number of numeric characters in D will be limited to 10,000,000.

输出

For each data set there is a single line of output. It contains the data set number followed by a single space which is then followed by the remainder resulting from dividing D by (B − l).

样例输入

5

1 10 7829

2 7 123456

3 6 432504023545112

4 8 37777777777777773

5 2 10110100010101010101101110001010001010101010101010111

样例输出

1 8

2 3

3 1

4 6

5 0

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
const int maxn = 1e7+10;
char s[maxn];
int main()
{
    int t;
    scanf ("%d",&t);
    while (t--) {
        int id,b;
        scanf ("%d%d ",&id,&b);
        int ans=0,mod=b-1;
        scanf ("%s",s);
        if(b==2) {
            printf ("%d 0\n",id);
            continue;
        }
        int l=strlen (s);
        for(int i=0;i<l;i++) {
            ans=ans+s[i]-'0';
            ans%=mod;
        }
        printf ("%d %d\n",id,ans%mod);
    }
    return 0;
}

问题 A: Hailstone HOTPO

时间限制: 1 Sec 内存限制: 128 MB

提交 状态 题面

题目描述

The hailstone sequence is formed in the following way:

• If n is even, divide it by 2 to get n′

• if n is odd, multiply it by 3 and add l to get n′

It is conjectured that for any positive integer number n, the sequence will always end in the repeating cycle: 4, 2, 1, 4, 2, 1, . . .. Suffice to say, when n == 1, we will say the sequence has ended.

Write a program to determine the largest value in the sequence for a given n.

输入

The first line of input contains a single integer P, (1 ≤ P ≤ 100000), 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 consisting of two space separated decimal integers.

The first integer is the data set number. The second integer is n, (l ≤ n ≤ 100, 000), which is the starting value.

输出

For each data set there is a single line of output consisting of the data set number, a single space, and the largest value in the sequence starting at and including n.

样例输入

4

1 1

2 3

3 9999

4 100000

样例输出

1 1

2 16

3 101248

4 100000

暴力就可以了,队友说可以用筛法优化可以更快

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
 
using namespace std;
typedef  long long ll;
const int N  = 100010;
typedef pair<int,int>P;
 
int val[N];
int main()
{
    for(int i=1;i<N;i++)
    {
        int temp=i;
        val[i]=i;
        while(1)
        {
            if(temp==1) break;
            if(temp&1) temp=temp*3+1;
            else temp/=2;
            val[i]=max(val[i],temp);
        }
    }
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int id,n;
        scanf("%d%d",&id,&n);
        printf("%d %d\n",id,val[n]);
    }
    return 0;
}

问题 J: Jurassic Jigsaw

时间限制: 1 Sec 内存限制: 128 MB Special Judge

提交 状态 题面

题目描述

The famous Jurassic park biologist Dean O’Saur has discovered new samples of what he expects to be the DNA of a dinosaur. With the help of his assistant Petra Dactil, he managed to sequence the samples, and now they are ready

for analysis. Dean thinks this dinosaur was affected with a particular disease mutating the DNA of some cells.

To verify his theory, he needs to compute the most likely evolutionary tree from the samples, where the nodes are the samples of DNA. Because there is no temporal data of the DNA samples, he is not concerned where the root of

the tree is.

Dean considers the most likely evolutionary tree, the tree with smallest unlikeliness: the unlikeliness of a tree is defined as the sum of the weights of all edges, where the weight of an edge is the number of positions at which the two DNA strings are different.

As a world expert in data trees, he asks you to reconstruct the most likely evolutionary tree.

In the first sample, the optimal tree is AA – AT – TT – TC . The unlikeliness of the edge between AA and AT edge is 1, because the strings AA and AT differ in exactly 1 position. The weights of the other two edges are also 1, so that the unlikeliness of the entire tree is 3. Since there is no tree of unlikeliness less than 3, the minimal unlikeliness of an evolutionary tree for this case is 3.

输入

• The first line consists of two integers 1 ≤ n ≤ 1000 and 1 ≤ k ≤ 10, the number of samples and the length of each sample respectively.

• Each of the next n lines contains a string of length k consisting of the characters in ACTG.

输出

• On the first line, print the minimal unlikeliness of the evolutionary tree.

• Then, print n − 1 lines, each consisting of two integers 0 ≤ u, v < n, indicating that in the most likely evolutionary tree, there is an edge between DNA string u and v. If there are multiple answers possible, any of them will be accepted.

样例输入

4 2

AA

AT

TT

TC

样例输出

3

0 1

1 2

2 3

最小生成树裸题

#pragma GCC optimize(3,"Ofast","inline")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <queue>
#include <vector>
#include <map>
#include <cmath>
#include <stack>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1010;
const int MAXN = 1000010;
struct Edge
{
    int u,v,w;
}edge[MAXN];
int F[maxn];
int tol;
int n,k;
char str[maxn][20];
void addEdge(int u,int v,int w)
{
    edge[tol].u=u;
    edge[tol].v=v;
    edge[tol].w=w;
    tol++;
}
bool cmp(Edge a,Edge b)
{
    return a.w<b.w;
}
int find(int x){
    if(F[x]==x) return x;
    else return F[x]=find(F[x]);
}
vector<int> ansans[2];
 
int Kruskal(int n){
    for(int i=0;i<=n;i++) F[i]=i;
    sort(edge,edge+tol,cmp);
    int cnt=0;
    int ans=0;
    for(int i=0;i<tol;i++)
    {
        int u=edge[i].u;
        int v=edge[i].v;
        int w=edge[i].w;
        int tONE = find(u);
        int tTWO = find(v);
        if(tONE!=tTWO)
        {
            ans+=w;
            F[tONE]=tTWO;
            cnt++;
            ansans[0].push_back(v);
            ansans[1].push_back(u);
        }
        if(cnt==n-1) break;
    }
    if(cnt<n-1) return -1;
    else return ans;
}
int getw(int x,int y)
{
    int ans=0;
    for(int i=0;i<k;i++)
        if(str[x][i]!=str[y][i])
            ans++;
    return ans;
}
int main()
{
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%s",str[i]);
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            addEdge(i,j,getw(i,j));
        }
    }
    int ans = Kruskal(n);
    cout<<ans<<endl;
    for(int i=0;i<ansans[1].size();i++){
        printf("%d %d\n",ansans[0][i],ansans[1][i] );
    }
    return 0;
}
通过 WordPress.com 设计一个这样的站点
从这里开始