CSU-ACM2016暑假集训比赛7

来源:互联网 发布:java 批处理框架 编辑:程序博客网 时间:2024/06/05 10:31

A - A
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
Submit

Status

Practice

LightOJ 1038

uDebug
Description
Rimi learned a new thing about integers, which is - any positive integer greater than 1 can be divided by its divisors. So, he is now playing with this property. He selects a number N. And he calls this D.

In each turn he randomly chooses a divisor of D (1 to D). Then he divides D by the number to obtain new D. He repeats this procedure until D becomes 1. What is the expected number of moves required for N to become 1.

Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case begins with an integer N (1 ≤ N ≤ 105).

Output
For each case of input you have to print the case number and the expected value. Errors less than 10-6 will be ignored.

Sample Input
3
1
2
50
Sample Output
Case 1: 0
Case 2: 2.00
Case 3: 3.0333333333

A:F[i]代表从i除到1的期望步数 m个约数为num[1]...num[m] (num[m]=i)F[i]=F[num[1]]/m+F[num[2]]/m+...+F[num[m]]/m+1m*F[i]=F[num[1]]+F[num[2]]+...+F[num[m]]+mF[i]=(sum(F[num[1]]...F[num[m-1]])+m)/(m-1)#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<set>using namespace std;const int MAX=100005;double F[MAX];int main(){    cout<<fixed<<setprecision(10);    int T;    cin>>T;    memset(F,0,sizeof(F));    for (int i=2; i<MAX; i++)    {        double sum=0;        int num=0;        for (int j=1; j*j<=i; j++)        {            if (i%j==0)            {                sum+=F[j];                num++;                if (j!=i/j)                {                    sum+=F[i/j];                    num++;                }            }        }        F[i]=(sum+num)/(num-1);    }    for (int a=1; a<=T; a++)    {        int n;        cin>>n;        cout<<"Case "<<a<<": "<<F[n]<<endl;    }    return 0;}

B - B
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

CodeForces 527B
Description
Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings S and T of equal length to be “similar”. After a brief search on the Internet, he learned about the Hamming distance between two strings S and T of the same length, which is defined as the number of positions in which S and T have different characters. For example, the Hamming distance between words “permanent” and “pergament” is two, as these words differ in the fourth and sixth letters.

Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn’t know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string S, so that the Hamming distance between a new string S and string T would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings.

Help him do this!

Input
The first line contains integer n (1 ≤ n ≤ 200 000) — the length of strings S and T.

The second line contains string S.

The third line contains string T.

Each of the lines only contains lowercase Latin letters.

Output
In the first line, print number x — the minimum possible Hamming distance between strings S and T if you swap at most one pair of letters in S.

In the second line, either print the indexes i and j (1 ≤ i, j ≤ n, i ≠ j), if reaching the minimum possible distance is possible by swapping letters on positions i and j, or print “-1 -1”, if it is not necessary to swap characters.

If there are multiple possible answers, print any of them.

Sample Input
Input
9
pergament
permanent
Output
1
4 6
Input
6
wookie
cookie
Output
1
-1 -1
Input
4
petr
egor
Output
2
1 2
Input
6
double
bundle
Output
2
4 1
Hint
In the second test it is acceptable to print i = 2, j = 3.

B: chST[i][j]记录当S串字母为i而T串字母为j时的位置   先找能减少两个的 之后找减少一个的 没有结果的话就是无法减少#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<set>using namespace std;string S,T;int chST[26][26],len,x,pos1,pos2;int main(){    //freopen("C://Users//hss//Documents//ACM//duipai//data.txt","r",stdin);    //freopen("C://Users//hss//Documents//ACM//duipai//MyOut.txt","w",stdout);    while (cin>>len>>S>>T)    {        memset(chST,0,sizeof(chST));        x=0;        for (int i=0; i<len; i++)            if (S[i]!=T[i])            {                x++;                chST[S[i]-'a'][T[i]-'a']=i+1;            }        bool flag=true;        for (int i=0; i<26; i++)            for (int j=0; j<26; j++)                if (chST[i][j]&&chST[j][i]&&flag)                {                    x-=2;                    pos1=chST[i][j];                    pos2=chST[j][i];                    flag=false;                    break;                }        if (flag)            for (int i=0; i<26; i++)                for (int j=0; j<26; j++)                    for (int k=0; k<26; k++)                        if (chST[i][j]&&chST[j][k]&&flag)                        {                            x-=1;                            pos1=chST[i][j];                            pos2=chST[j][k];                            flag=false;                            break;                        }        if (flag)        {            pos1=-1;            pos2=-1;        }        cout<<x<<endl<<pos1<<' '<<pos2<<endl;    }    return 0;}

D - D
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 5791
Description
Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A’ and sequence B’ are same. For example, {1,2} and {1,2} are same. {1,2,4} and {1,4,2} are not same. A’ is a subsequence of A. B’ is a subsequence of B. The subsequnce can be not continuous. For example, {1,1,2} has 7 subsequences {1},{1},{2},{1,1},{1,2},{1,2},{1,1,2}. The answer can be very large. Output the answer mod 1000000007.
Input
The input contains multiple test cases.

For each test case, the first line cantains two integers N, M(1\leq N,M\leq 1000). The next line contains N integers. The next line followed M integers. All integers are between 1 and 1000.
Output
For each test case, output the answer mod 1000000007.
Sample Input
3 2
1 2 3
2 1
3 2
1 2 3
1 2
Sample Output
2
3

D:a[i]!=b[j]时:dp[i][j]=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1])a[i]==b[j]时:dp[i][j]=(dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1])+dp[i-1][j-1]+1=dp[i-1][j]+dp[i][j-1]+1因为要模1e9+7,所以在减去dp[i-1][j-1]时可能出现负数,所以dp<0时要加上mod#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<set>using namespace std;const int MAX=1005;const int MOD=1000000007;int N,M,A[MAX],B[MAX],F[MAX][MAX];int main(){    while(cin>>N>>M)    {        for (int i=1; i<=N; i++)            cin>>A[i];        for (int j=1; j<=M; j++)            cin>>B[j];        memset(F,0,sizeof(F));        for (int i=1; i<=N; i++)            for (int j=1; j<=M; j++)            {                F[i][j]=(F[i-1][j]+F[i][j-1]-F[i-1][j-1])%MOD;                if (A[i]==B[j])                    F[i][j]=(F[i][j]+F[i-1][j-1]+1)%MOD;                if (F[i][j]<0)                    F[i][j]+=MOD;            }        cout<<F[N][M]<<endl;    }    return 0;}

E - E
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 1515
Description
How can anagrams result from sequences of stack operations? There are two sequences of stack operators which can convert TROT to TORT:

[
i i i i o o o o
i o i i o o i o
]

where i stands for Push and o stands for Pop. Your program should, given pairs of words produce sequences of stack operations which convert the first word to the second.

A stack is a data storage and retrieval structure permitting two operations:

Push - to insert an item and
Pop - to retrieve the most recently pushed item
We will use the symbol i (in) for push and o (out) for pop operations for an initially empty stack of characters. Given an input word, some sequences of push and pop operations are valid in that every character of the word is both pushed and popped, and furthermore, no attempt is ever made to pop the empty stack. For example, if the word FOO is input, then the sequence:

i i o i o o is valid, but
i i o is not (it’s too short), neither is
i i o o o i (there’s an illegal pop of an empty stack)

Valid sequences yield rearrangements of the letters in an input word. For example, the input word FOO and the sequence i i o i o o produce the anagram OOF. So also would the sequence i i i o o o. You are to write a program to input pairs of words and output all the valid sequences of i and o which will produce the second member of each pair from the first.
Input
The input will consist of several lines of input. The first line of each pair of input lines is to be considered as a source word (which does not include the end-of-line character). The second line (again, not including the end-of-line character) of each pair is a target word. The end of input is marked by end of file.
Output
For each input pair, your program should produce a sorted list of valid sequences of i and o which produce the target word from the source word. Each list should be delimited by

[
]

and the sequences should be printed in “dictionary order”. Within each sequence, each i and o is followed by a single space and each sequence is terminated by a new line.
Sample Input
madam
adamm
bahama
bahama
long
short
eric
rice
Sample Output
[
i i i i o o o i o o
i i i i o o o o i o
i i o i o i o i o o
i i o i o i o o i o
]
[
i o i i i o o i i o o o
i o i i i o o o i o i o
i o i o i o i i i o o o
i o i o i o i o i o i o
]
[
]
[
i i o i o i o o
]

E: dfs+回溯#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<stack>#include<set>using namespace std;stack<char> sta; char s1[100],s2[100],way[200];//s1存给定序列 s2存目标序列 way记录操作  int cnt;  void print()//如果搜索到满足条件的方法则输出  {      int i;      for(i=0; i<cnt; i++)          printf("%c ",way[i]);      printf("\n");  }  void dfs(int p1,int p2)//p1表示s1待入栈位置 p2表示s2待匹配位置  {      if(s2[p2]=='\0')//如果目标序列已完全匹配输出答案返回      {          print();          return;      }      char c;      if(s1[p1]!='\0')//如果s1还没有完全进栈    {          c=s1[p1];          sta.push(c);          way[cnt++]='i';          dfs(p1+1,p2);          sta.pop();          cnt--;      }      if(!sta.empty())//出栈     {          c=sta.top();          if(c==s2[p2])          {              sta.pop();              way[cnt++]='o';              dfs(p1,p2+1);              cnt--;              sta.push(c);          }      }  }  int main()  {      while(~scanf("%s%s",s1,s2))      {          while(!sta.empty())              sta.pop();          cnt=0;          printf("[\n");          dfs(0,0);          printf("]\n");      }      return 0;  } 

F - F
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

HDU 5025
Description
《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng’en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts.

During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.

Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace. But to rescue Tang Monk, Sun Wukong might need to get some keys and kill some snakes in his way.

The palace can be described as a matrix of characters. Each character stands for a room. In the matrix, ‘K’ represents the original position of Sun Wukong, ‘T’ represents the location of Tang Monk and ‘S’ stands for a room with a snake in it. Please note that there are only one ‘K’ and one ‘T’, and at most five snakes in the palace. And, ‘.’ means a clear room as well ‘#’ means a deadly room which Sun Wukong couldn’t get in.

There may be some keys of different kinds scattered in the rooms, but there is at most one key in one room. There are at most 9 kinds of keys. A room with a key in it is represented by a digit(from ‘1’ to ‘9’). For example, ‘1’ means a room with a first kind key, ‘2’ means a room with a second kind key, ‘3’ means a room with a third kind key… etc. To save Tang Monk, Sun Wukong must get ALL kinds of keys(in other words, at least one key for each kind).

For each step, Sun Wukong could move to the adjacent rooms(except deadly rooms) in 4 directions(north, west, south and east), and each step took him one minute. If he entered a room in which a living snake stayed, he must kill the snake. Killing a snake also took one minute. If Sun Wukong entered a room where there is a key of kind N, Sun would get that key if and only if he had already got keys of kind 1,kind 2 … and kind N-1. In other words, Sun Wukong must get a key of kind N before he could get a key of kind N+1 (N>=1). If Sun Wukong got all keys he needed and entered the room in which Tang Monk was cuffed, the rescue mission is completed. If Sun Wukong didn’t get enough keys, he still could pass through Tang Monk’s room. Since Sun Wukong was a impatient monkey, he wanted to save Tang Monk as quickly as possible. Please figure out the minimum time Sun Wukong needed to rescue Tang Monk.
Input
There are several test cases.

For each case, the first line includes two integers N and M(0 < N <= 100, 0<=M<=9), meaning that the palace is a N×N matrix and Sun Wukong needed M kinds of keys(kind 1, kind 2, … kind M).

Then the N × N matrix follows.

The input ends with N = 0 and M = 0.
Output
For each test case, print the minimum time (in minutes) Sun Wukong needed to save Tang Monk. If it’s impossible for Sun Wukong to complete the mission, print “impossible”(no quotes).
Sample Input
3 1
K.S

1

1#T
3 1
K#T
.S#
1#.
3 2
K#T
.S.
21.
0 0
Sample Output
5
impossible
8

F:状态压缩BFS因为蛇只能杀一次 最多有5条蛇 将蛇的状态压缩vis[x][y][key][snake] 在(x,y)处拿到key个钥匙击杀蛇的状态为snake时是否走过该格子#include<stdio.h>#include<string>#include<cstring>#include<queue>#include<algorithm>#include<functional>#include<vector>#include<iomanip>#include<math.h>#include<iostream>#include<sstream>#include<set>using namespace std;const int MAX=105;const int dX[4]= {-1,0,0,1};const int dY[4]= {0,1,-1,0};struct node{    int x,y,k,s,t;    node() {};    node(int a,int b,int c,int d,int e):x(a),y(b),k(c),s(d),t(e) {};};node u,v;int N,M,startX,startY,snake[MAX][MAX],sNum,Ans;char pic[MAX][MAX];int vis[MAX][MAX][10][1<<5];struct Comparer{    bool operator()(node& a,node& b)    {        return a.t>b.t;    }};void bfs(){    u.x=startX;    u.y=startY;    u.k=0;    u.s=0;    u.t=0;    vis[u.x][u.y][0][0]=1;    priority_queue<node,vector<node>,Comparer> que;    que.push(u);    while(!que.empty())    {        u=que.top();        que.pop();        //printf("(%d,%d) Key=%d Snake=%d Time=%d \n",u.x,u.y,u.s,u.k,u.t);        for (int i=0; i<4; i++)        {            v.x=u.x+dX[i];            v.y=u.y+dY[i];            v.k=u.k;            v.s=u.s;            v.t=u.t+1;            if (v.x>=0&&v.x<N&&v.y>=0&&v.y<N&&pic[v.x][v.y]!='#'&&!vis[v.x][v.y][v.k][v.s])            {                vis[v.x][v.y][v.k][v.s]=1;                if (pic[v.x][v.y]=='S')                {                    if (!(v.s&(1<<snake[v.x][v.y])))                    {                        v.s|=(1<<snake[v.x][v.y]);                        v.t+=1;                    }                    que.push(v);                }                else if (pic[v.x][v.y]==(char)(v.k+1+'0'))                {                    v.k+=1;                    que.push(v);                }                else if (pic[v.x][v.y]=='T'&&v.k==M)                {                    Ans=v.t;                    return ;                }                else                {                    que.push(v);                }            }        }    }}int main(){    while(cin>>N>>M)    {        memset(vis,0,sizeof(vis));        sNum=0;        Ans=99999999;        if (N==0&&M==0) break;        for (int i=0; i<N; i++)            for (int j=0; j<N; j++)            {                cin>>pic[i][j];                if (pic[i][j]=='K')                {                    startX=i;                    startY=j;                }                if (pic[i][j]=='S')                {                    snake[i][j]=sNum++;                }            }        bfs();        if (Ans==99999999)            cout<<"impossible"<<endl;        else            cout<<Ans<<endl;    }    return 0;}
0 0