A题
GREAT+SWERC=PORTO
We want to have a great SWERC at Porto this year and we approached this challenge in several ways. We even framed it as a word addition problem, similar to the classic SEND+MORE=MONEY, where each letter stands for a single digit (0, 1, 2, ..., 8, 9) that makes the arithmetic operation correct. In word additions different letters cannot be assigned the same digit and the leftmost letter in a word cannot be zero (0). In particular, a single letter term cannot be zero.
To solve this word addition problem we had to find positive digits for G, S and P, and digits for R, E, A, T, W, C, O, so that each letter has a different digit and the sum is correct. It turns out that, unlike the classical SEND+MORE=MONEY which has a single solution, GREAT+SWERC=PORTO has six solutions. T=7, E=3, W=9, G=1, A=0, P=4, S=2, C=8, R=6, O=5 T=7, E=3, W=9, G=2, A=0, P=4, S=1, C=8, R=6, O=5 T=8, E=5, W=1, G=3, A=7, P=9, S=6, C=4, R=0, O=2 T=8, E=5, W=1, G=6, A=7, P=9, S=3, C=4, R=0, O=2 T=9, E=5, W=2, G=1, A=8, P=7, S=6, C=4, R=0, O=3 T=9, E=5, W=2, G=6, A=8, P=7, S=1, C=4, R=0, O=3 HavingmorethanonesolutiondoesnotmakeGREAT+SWERC=PORTOagoodproblem tosolvebyhand, butitisstillapieceofcakeforaprogramer. Moreover, itgivesusanother reason to organize SWERC again next year and, who knows, in years to come!
Task Given a word addition problem, compute the number of solutions (possibly zero).
Input A line with an integer n, followed by n lines containing a word each with maximum length of 10 letters. The first n−1 words are the terms to be added and the last line is the result. Universidade do Porto Computer Science Department 3
Problem A Problem A
Wordscontainonlycapitalletters. Ifwordshavedifferentlengths, theymustbeinterpreted as aligning to the right. For instance, in the SEND+MORE=MONEY problem, the D of the first word and E of the second word align with the Y of the final word. You can also assume that the size of the last word is greater than or equal to the maximum size of the preceding words, and moreover, at most ten distinct letters are involved in a word problem.
Constraints 3 ≤ n ≤ 10 Each word has at most 10 symbols (capital letters). A word problem has at most 10 distinct letters.
Output A single line with an integer: the number of solutions of the word addition problem given as input.
Sample Input 1 3 GREAT SWERC PORTO
Sample Output 1 6
Sample Input 2 3 SEND MORE MONEY
Sample Output 2 1
Sample Input 3
5 TOO GOOD TO BE TRUE
Sample Output 3
93
A题算是一道小水题:就是对字母对应字数的枚举,写了一个cal计算字符串的值,和dp()枚举:枚举所有情况所以需要回溯。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#define MOD 10000000
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
ULL base=233;
long long quickpow(long long n, long long base) {
long long res = 1;
while(n) {
if(n & 1) {
res = res * base % MOD;
}
n >>= 1;
base = base * base % MOD;
}
return res;
}//快速幂
ULL HASH(char s[])
{
int l=strlen(s);
ULL ans=0;
for(int i=0;i<l;i++)
{
ans=ans*base+(ULL)s[i];
}
return ans;
}
set<ULL>hash0;//手动哈希
set<ULL>::iterator it;//STL迭代器
set<int >set0;
char A[200];
int B[200];
char C[150][200];
int data0[100];
int data1[300];
int data2[300];
LL ans=0;
map<int ,int>map0;
LL cal(char s[])
{
int len=strlen(s+1);
LL t=1;
LL sum=0;
for(int i=len;i>=1;i--)
{
sum+=t*data0[s[i]-'A'+1];
t*=10;
}
return sum;
}
void dp(int index,int x,int n)
{
if(index==x+1)
{
LL SUM=0;
for(int i=1;i<=n-1;i++)
{
SUM+=cal(C[i]);
}
if(SUM==cal(C[n]))
{ans++;
}
return;
}
if(data2[A[index]-'A'+1]==1)
{for(int i=1;i<=9;i++)
{
if(!data1[i])
{
data1[i]=1;
data0[A[index]-'A'+1]=i;
dp(index+1,x,n);
data1[i]=0;
}
}}
else
{
for(int i=0;i<=9;i++)
{
if(!data1[i])
{
data1[i]=1;
data0[A[index]-'A'+1]=i;
dp(index+1,x,n);
data1[i]=0;
}
}
}
return ;
}
int main()
{
int n,k=0;
cin>>n;
int x=0;
for(int i=1;i<=n;i++)
{
cin>>C[i]+1;
}
memset(data2,0,sizeof(data2));
for(int i=1;i<=n;i++)
{
int len=strlen(C[i]+1);
for(int j=1;j<=len;j++)
{
if(j==1)data2[C[i][1]-'A'+1]=1;
if(!map0[C[i][j]-'A'+1])
{
map0[C[i][j]-'A'+1]=1;
A[++k]=C[i][j];
}
}
}
x=map0.size();
dp(1,x,n);
cout<<ans<<endl;
}
单独还要判断那些处于首字母的数字不能为0。(我忘了有很多的首字母一开始)
B题
Flowery Trails
!"
#$%&'$()"
*+,-).%" /)'0"
122"
1!2"
342"
522"
!22"
652"
!42"
72" 72"
5!2"
!12" 622"
162"18!"
!12"
6"
7"
4"
9"
3"
5"
8"
1"
2"
In order to attract more visitors, the manager of a national park had the idea of planting flowers along both sides of the popular trails, which are the trails used by common people. Common people only go from the park entrance to its highest peak, where views are breathtaking, by a shortest path. So, he wants to know how many metres of flowers are needed to materialize his idea. For instance, in the park whose map is depicted in the figure, common people make only one of the three following paths (which are the shortest paths from the entrance to the highest peak). • At the entrance, some start in the rightmost trail to reach the point of interest 3 (after 100 metres), follow directly to point 7 (200 metres) and then pick the direct trail to the highest peak (620 metres). • The others go to the left at the entrance and reach point 1 (after 580 metres). Then, they take one of the two trails that lead to point 4 (both have 90 metres). At point 4, they follow the direct trail to the highest peak (250 metres).
Notice that popular trails (i.e., the trails followed by common people) are highlighted in yellow. Since the sum of their lengths is 1930 metres, the extent of flowers needed to cover both sides of the popular trails is 3860 metres (3860 = 2×1930).
Task Given a description of the park, with its points of interest and (two-way) trails, the goal is to find out the extent of flowers needed to cover both sides of the popular trails. It is guaranteed that, for the given inputs, there is some path from the park entrance to the highest peak.
Input The first line of the input has two integers: P and T. P is the number of points of interest and T is the number of trails. Points are identified by integers, ranging from 0 to P −1. The entrance point is 0 and the highest peak is point P −1. Universidade do Porto Computer Science Department 5
Problem B Problem B
Each of the following T lines characterises a different trail. It contains three integers, p1, p2, and l, which indicate that the (two-way) trail links directly points p1 and p2 (not necessarily distinct) and has length l (in metres). Integers in the same line are separated by a single space.
Constraints 2 ≤ P ≤ 10000 Number of points. 1 ≤ T ≤ 250000 Number of trails. 1 ≤ l ≤ 1000 Length of a trail. Output The output has a single line with the extent of flowers (in metres) needed to cover both sides of the popular trails.
Sample Input 1 10 15 0 1 580 1 4 90 1 4 90 4 9 250 4 2 510 2 7 600 7 3 200 3 3 380 3 0 150 0 3 100 7 8 500 7 9 620 9 6 510 6 5 145 5 9 160
Sample Output 1 3860
Sample Input 2 4 7 0 1 1 0 2 2 0 3 10 0 3 3 1 3 2 2 3 1 1 1 1
Sample Output 2 18
重边+多条最短路+筛选的题目。
EMMM感觉很难。单独会讲:
C题
Golf Bot
Do you like golf? I hate it. I hate golf so much that I decided to build the ultimate golf robot, a robot that will never miss a shot. I simply place it over the ball, choose the right direction and distance and, flawlessly, it will strike the ball across the air and into the hole. Golf will never be played again. Unfortunately, it doesn’t work as planned. So, here I am, standing in the green and preparing my first strike when I realize that the distance-selector knob built-in doesn’t have all the distance options! Not everything is lost, as I have 2 shots.
Task Given my current robot, how many holes will I be able to complete in 2 strokes or less?
Input The first line has one integer: N, the number of different distances the Golf Bot can shoot. Each of the following N lines has one integer, ki, the distance marked in position i of the knob. Next line has one integer: M, the number of holes in this course. Each of the following M lines has one integer, dj, the distance from Golf Bot to hole j.
Constraints 1 ≤ N,M ≤ 200000 1 ≤ ki,dj ≤ 200000
Output You should output a single integer, the number of holes Golf Bot will be able to complete. Golf Bot cannot shoot over a hole on purpose and then shoot backwards.
Universidade do Porto Computer Science Department 7
Problem C Problem C
Sample Input
3 1 3 5 6 2 4 5 7 8 9
Sample Output
4
Sample Output Explanation Golf Bot can shoot 3 different distances (1, 3 and 5) and there are 6 holes in this course at distances 2, 4, 5, 7, 8 and 9. Golf Bot will be able to put the ball in 4 of these: • The 1st hole, at distance 2, can be reached by striking two times a distance of 1. • The 2nd hole, at distance 4, can be reached by striking with strength 3 and then strength 1 (or vice-versa). • The 3rd hole can be reached with just one stroke of strength 5. • The 5th hole can be reached with two strikes of strengths 3 and 5. Holes 4 and 6 can never be reached.
题意很简单:写起来简单:TLE也很简单。
要用到FFT来简化:单独会讲:
PS:想到做题的小朋友可以直接在Vjudge上搜哦:如果有密码就是zxynb nbzxy.(因为我题目可能看不大清楚)