Round B APAC Test 2016
Posted dumeichen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Round B APAC Test 2016相关的知识,希望对你有一定的参考价值。
Problem A
There are N cities in Chelsea‘s state (numbered starting from 1, which is Chelsea‘s city), and M bidirectional roads directly connect them. (A pair of cities may even be directly connected by more than one road.) Because of changes in traffic patterns, it may take different amounts of time to use a road at different times of day, depending on when the journey starts. (However, the direction traveled on the road does not matter -- traffic is always equally bad in both directions!) All trips on a road start (and end) exactly on the hour, and a trip on one road can be started instantaneously after finishing a trip on another road.
Chelsea loves to travel and is deciding where to go for her winter holiday trip. She wonders how quickly she can get from her city to various other destination cities, depending on what time she leaves her city. (Her route to her destination may include other intermediate cities on the way.) Can you answer all of her questions?Input
The first line of the input gives the number of test cases, T. T test cases follow.
The first line of each test case contains three integers: the number N of cities, the number M of roads, and the number K of Chelsea‘s questions.
2M lines -- M pairs of two lines -- follow. In each pair, the first line contains two different integers x and y that describe one bidirectional road between the x-th city and the y-th city. The second line contains 24 integers Cost[t] (0 ≤ t ≤ 23) that indicate the time cost, in hours, to use the road when departing at t o‘clock on that road. It is guaranteed that Cost[t] ≤ Cost[t+1]+1 (0 ≤ t ≤ 22) and Cost[23] ≤ Cost[0]+1.
Then, an additional K lines follow. Each contains two integers D and S that comprise a question: what is the fewest number of hours it will take to get from city 1 to city D, if Chelsea departs city 1 at S o‘clock?
思路:问题的关键在于可能不可能先停留在原地一会儿之后出发反而更早到达别的地方。实际上由条件:Cost[t] ≤ Cost[t+1]+1可知这是不可能的,所以仍然是一个最短路问题。那么只要预求出从时间0~23的结点1出发,到达所有其他结点的最短路,然后进行查找即可。
#include <cstdio> #include <queue> #include <cmath> #include <algorithm> using namespace std; #define N 505 int s[N][N][25],n,m,q,T; int dis[N][25],used[N]; int relax(int b,int x,int y,int w){ if(dis[x][b] + w < dis[y][b]){ dis[y][b] = dis[x][b] + w; return 1; } return 0; } void spfa(int b){//b时刻从1出发 dis[1][b] = 0; queue<int> q; memset(used, 0, sizeof(used)); q.push(1); used[1] = 1; while(!q.empty()){ int now = q.front(); q.pop(); used[now] = 0; for(int i = 1;i<=n;i++) if(s[now][i][(dis[now][b]+b)%24]!=0x3fffffff) if(relax(b,now,i,s[now][i][(dis[now][b]+b)%24]) && !used[i]){ used[i] = 1; q.push(i); } } } int main(){ scanf("%d",&T); int a,b,x; for(int c = 1;c<=T;c++){ scanf("%d %d %d",&n,&m,&q); for(int i = 1;i<=n;i++) for(int j = 1;j<=n;j++) for(int k = 0;k<24;k++) s[i][j][k] = (i==j)?0:0x3fffffff; for(int i = 1;i<=n;i++) for(int j = 0;j<24;j++) dis[i][j] = 0x3fffffff; while(m--){ scanf("%d %d",&a,&b); for(int i = 0;i<24;i++){ scanf("%d",&x); s[a][b][i] = s[b][a][i] = min(s[a][b][i], x); } } for(int i = 0;i<24;i++) spfa(i); printf("Case #%d:",c); while(q--){ scanf("%d %d",&a,&b); if(dis[a][b]==0x3fffffff) printf(" -1"); else printf(" %d",dis[a][b]); } printf("\n"); } return 0; }
Problem B
A typical mountain bike has two groups of gears: one group connected to the pedals, and one group connected to the rear tire. A gear group consists of many gears, which usually have different numbers of teeth. A chain connects one of the gears in the pedal
gear group to one of the gears in the tire gear group, and this determines the ratio between the cyclist‘s pedaling speed and the tire speed. For example, if the chain connects a gear with 5 teeth on the pedals to a gear with 10 teeth on the tires, the ratio
will be 1/2, since the cyclist needs to make the pedal gear rotate twice to make the tire rotate once. The cyclist can change the chain to connect any one gear from the pedal group to any one gear from the tire group.
You have just bought a special new mountain bike with three groups of gears: one connected to the pedals, one connected to the tire, and one extra group in between. This mountain bike has two chains; the first chain must always connect one gear from the pedal
gear group to one gear on the extra gear group, and the second chain must always connect one gear from the extra gear group to one gear on the tire gear group. Moreover, the two chains cannot both use the same gear from the extra gear group.
Given the numbers of teeth on the available gears on the pedals, extra, and tire groups, is it possible to make the ratio (between pedaling speed and tire speed) be exactly P/Q? For a given set of gears, you may need to answer multiple such questions.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each begins with one line with 3 integers Np, Ne, and Nt, representing the numbers of gears on the pedals, extra, and tire groups. Then, three more lines follow. These contain
Np, Ne, and Nt integers, respectively, representing the numbers of teeth on the different gears on the pedals, extra, and tire gear groups, respectively. (It is guaranteed that the numbers of teeth on the gears within a group are all distinct.) The next line
after that consists of one integer, M, the number of questions. Finally, there are M lines, each with 2 integers, P and Q, representing the target ratio. (It is not guaranteed that this ratio is a reduced fraction.)
题意:翻译一下题意就是,给定三个集合A,B,C(每个集合不超过2000个元素,每个元素不超过10000),还有两个整数P和Q(不超过10^9),问能否从A和C中各选出一个元素a和c,B中选出两个元素b1和b2,使得(a/b1)*(b2/c) = p/q。
思路:首先枚举b1/b2的值,然后枚举AC集合的元素对,求出其对应的b1/b2的值是否出现过。用set<pair<int,int>>来进行查找。时间复杂度是O(n*n*logn)。
#include <cstdio> #include <set> #include <unordered_set> #include <cmath> #include <algorithm> using namespace std; #define N 2005 int T,a,b,c,m; long long p,q; int A[N],B[N],C[N]; set<pair<int, int>> hh; long long gcd(long long a,long long b){ return !b ? a : gcd(b, a%b); } bool solve(){ for(int i = 0;i<a;i++) for(int j = 0;j<c;j++){ long long x = p*C[j]; long long y = q*A[i]; long long tmp = gcd(x, y); x /= tmp; y /= tmp; if(x>10000 || y>10000) continue; if(hh.find(make_pair((int)x, (int)y)) != hh.end()) return true; } return false; } int main(){ scanf("%d",&T); for(int z = 1;z<=T;z++){ scanf("%d %d %d",&a,&b,&c); for(int i = 0;i<a;i++) scanf("%d",&A[i]); for(int i = 0;i<b;i++) scanf("%d",&B[i]); for(int i = 0;i<c;i++) scanf("%d",&C[i]); hh.clear(); for(int i = 0;i<b;i++) for(int j = i+1;j<b;j++){ int tmp = (int)gcd(B[i], B[j]); hh.insert(make_pair(B[i]/tmp, B[j]/tmp)); hh.insert(make_pair(B[j]/tmp, B[i]/tmp)); } printf("Case #%d:\n",z); scanf("%d",&m); while(m--){ scanf("%lld %lld",&p,&q); long long tmp = gcd(p,q); p /= tmp; q /= tmp; if(solve()) printf("Yes\n"); else printf("No\n"); } } return 0; }
Problem C. gNumbers
Googlers are crazy about numbers and games, especially number games! Two Googlers, Laurence and Seymour, have invented a new two-player game based on "gNumbers". A number is a gNumber if and only if the sum of the number‘s digits has no positive divisors
other than 1 and itself. (In particular, note that 1 is a gNumber.)
The game works as follows: First, someone who is not playing the game chooses a starting number N. Then, the two players take turns. On a player‘s turn, the player checks whether the current number C is a gNumber. If it is, the player loses the game immediately.
Otherwise, the player chooses a prime factor P of C, and keeps dividing C by P until P is no longer a factor of C. (For example, if the current number were 72, the player could either choose 2 and repeatedly divide by 2 until reaching 9, or choose 3 and repeatedly
divide by 3 until reaching 8.) Then the result of the division becomes the new current number, and the other player‘s turn begins.
Laurence always gets to go first, and he hates to lose. Given a number N, he wants you to tell him which player is certain to win, assuming that both players play optimally.
Input
The first line of the input gives the number of test cases, T. T test cases follow; each consists of a starting number N.
Output
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the winner‘s name: either Laurence or Seymour.
题意:定义了gNumber(各位数字和为质数,1也算),初始一个数,如果是gNumber则输,不是的话可以选择其任意一个质因子,一直除知道没法除未知,交换。问谁有必胜策略。
思路:一开始找规律,发现找不到。搜题解看到直接暴力就能过。于是直接暴力,但是少考虑了大于根号n的那个质因子导致一直错误。具体来说,对于一个数n来说,其可能出现一个质因子大于根号n,如果有,最多只有一个(显然)。所以只需要在2~根号n范围内的质数中扫,然后保存一个剩余的数,如果最后不等于1,那么就是这个大于根号n的质因子。
当然,后来看到提交页面排名第一的做法,实际上还可以用动归状态压缩优化。首先在n的范围内,n的质因子个数少于20个(没仔细算,就是看看2*3*5多少个开始大于10的15次方)。举例来说,将n写成a^s1 * b^s2 *c^s3...g^s6,用第一位表示a^s1,第二位表示a^s2等等等。从0遍历到1<<6,比如i,dp[i]表示i对应的数是否为必胜态,由于i的后继都已经算出(因为i中去掉每个位上的1必然比i小),所以可以从那些位置推得dp[i]的值)。最后看看dp[全1]是1还是0即可。
#include <cstdio> #include <set> #include <unordered_set> #include <cmath> #include <algorithm> using namespace std; #define N 40000000 int T,len = 0; long long prime[2000000],n; bool hh[160]; bool s[N]; void init(){ memset(s, false, sizeof(s)); memset(hh, false, sizeof(hh)); hh[1] = true; for(long long i = 2;i*i<=1000000000000000;i++) if(!s[i]){ prime[len++] = i; for(long long j = i;j*j<=1000000000000000;j+=i) s[j] = true; if(i<160)//按照输入范围15*9就够了 hh[i] = true; } } bool testG(long long x){//判断一个数是否为gNumber int sum = 0; while(x){ sum += x%10; x /= 10; } return hh[sum]; } bool win(long long x){ if(testG(x)) return false; long long left = x;//这个left有没有考虑进去是关键,错了很多次 for(int i = 0;i<len && prime[i]*prime[i]<=x;i++) if(x%prime[i] == 0){ long long next = x; while(next % prime[i] == 0) next /= prime[i]; while(left % prime[i] == 0) left /= prime[i]; if(!win(next)) return true; } if(left!=1 && !win(x/left)) return true; return false; } int main(){ init(); scanf("%d",&T); for(int z = 1;z<=T;z++){ scanf("%lld",&n); if(win(n)) printf("Case #%d: Laurence\n",z); else printf("Case #%d: Seymour\n",z); } return 0; }
Problem D. Albocede DNA
The DNA of the Albocede alien species is made up of 4 types of nucleotides: a, b, c, and d. Different Albocedes may have different sequences of these nucleotides, but any Albocede‘s DNA sequence obeys all of the following rules:
It contains at least one copy of each of a, b, c, and d.All as come before all bs, which come before all cs, which come before all ds.
There are exactly as many ‘a‘s as ‘c‘s.
There are exactly as many ‘b‘s as ‘d‘s.
For example, abcd and aabbbccddd are valid Albocede DNA sequences. acbd, abc, and abbccd are not.
The Albocede-n is an evolved species of Albocede. The DNA sequence of an Albocede-n consists of one or more valid Albocede DNA sequences, concatenated together end-to-end. For example, abcd and aaabcccdaabbbccdddabcd are valid Albocede-n DNA sequences. (Observe that a valid Albocede-n DNA sequence is not necessarily also a valid Albocede DNA sequence.)
From one of your alien expeditions, you retrieved an interesting sequence of DNA made up of only as, bs, cs, and ds. You are interested in how many of the different subsequences of that sequence would be valid Albocede-n DNA sequences. (Even if multiple different selections of nucleotides from the sequence produce the same valid subsequence, they still all count as distinct subsequences.) Since the result may be very large, please find it modulo 1000000007 (109 + 7).
Input
The first line of the input gives the number of test cases, T. Each of the next T lines contains a string S that consists only of the characters a, b, c, and d.
题意:给定一个只含有abcd的串,求满足条件的子序列个数。
思路:动态规划。dp[i][j][k][s]表示前s[1...i]中,处理到还有j个a,k个b而且最后一个处理的是s(s取0~3,0表示a,1表示b,2表示c,d表示d)。
一开始没有加第四维,是错误的。比如处理dp[x][1][2]的时候,它可能最后已经添加c了,那么我们不能再在后面添加b了。
其中一个bug调试了近一个小时(一开始数组维度开的dp[2][N/2][N/2][4],这样写循环的时候用j<i判断则会越界。虽然N/2足够了(只需要在判断的地方改一下即可),但是这样的内存也够了。
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; #define N 505 #define M 1000000007 char s[N]; int dp[2][N][N][4]; int T; int main(){ scanf("%d",&T); for(int z = 1;z<=T;z++){ memset(dp, 0, sizeof(dp)); dp[0][0][0][3] = 1; scanf("%s",s+1); int p,q; for(int i = 1;s[i];i++){ q = i&1; p = 1-q; memcpy(dp[q], dp[p], sizeof(dp[p])); if(s[i] == 'a'){ for(int j = 2;j<=i;j++) dp[q][j][0][0] += dp[p][j-1][0][0] , dp[q][j][0][0]%=M; dp[q][1][0][0] += dp[p][0][0][3] , dp[q][1][0][0]%=M; }else if(s[i] == 'b'){ for(int k = 1;k<i;k++){ for(int j = 2;j<=i;j++) dp[q][k][j][1] += dp[p][k][j-1][1] , dp[q][k][j][1]%=M; dp[q][k][1][1] += dp[p][k][0][0] , dp[q][k][1][1]%=M; } }else if(s[i] == 'c'){ for(int j = 0;j<i;j++) for(int k = 1;k<i;k++){ dp[q][j][k][2] += dp[p][j+1][k][2] , dp[q][j][k][2]%=M; dp[q][j][k][2] += dp[p][j+1][k][1] , dp[q][j][k][2]%=M; } }else{ for(int j = 0;j<i;j++){ dp[q][0][j][3] += dp[p][0][j+1][3] , dp[q][0][j][3]%=M; dp[q][0][j][3] += dp[p][0][j+1][2] , dp[q][0][j][3]%=M; } } } printf("Case #%d: %d\n",z,dp[q][0][0][3]%M-1); } return 0; }
以上是关于Round B APAC Test 2016的主要内容,如果未能解决你的问题,请参考以下文章
google Round D APAC Test 2017 题解
Deltix Round, Summer 2021 (open for everyone, rated, Div. 1 + Div. 2) D. Take a Guess (交互,位运算性质)(代码片
RR调度(Round-robin scheduling)简单介绍
Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) C. Ordering Pizza
Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2) E. Buy Low Sell High(代码片