Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 25 Accepted Submission(s) : 8
One of them is selected as the first, and should say the number 1. Then they continue to count number from 1 one by one (clockwise). The game is interesting in that, once someone counts a number which is a multiple of 7 (e.g. 7, 14, 28, ...) or contains the digit '7' (e.g. 7, 17, 27, ...), he shall say "CocaCola" instead of the number itself.
For example, 4 students play this game. At some time, the first one says 25, then the second should say 26. The third should say "CocaCola" because 27 contains the digit '7'. The fourth one should say "CocaCola" too, because 28 is a multiple of 7. Then the first one says 29, and the game goes on. When someone makes a mistake, the game ends.
During a game, you may hear a consecutive of p "CocaCola"s. So what is the minimum number that can make this situation happen?
For example p = 2, that means there are a consecutive of 2 "CocaCola"s. This situation happens in 27-28 as stated above. 27 is then the minimum number to make this situation happen.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 100) which is the number of test cases. And it will be followed by T consecutive test cases.
There is only one line for each case. The line contains only one integer p (1 <= p <= 99).
Output
Results should be directed to standard output. The output of each test case should be a single integer in one line, which is the minimum possible number for the first of the p "CocaCola"s stands for.
Sample Input
2 2 3
Sample Output
27 70
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxm=800;
const int maxn=802;
int t,n,res[maxn];
int main()
{
int i,num=0;
memset(res,0,sizeof(res));
for(i=7;i<=800;i++)
{
int cnt=0;
int pre=i;
while(i<=800)
{
int tmp=i;
int flag=0;
if(i%7==0)
{
cnt++;
flag=1;
}
else while(tmp)
{
if(tmp%10==7){cnt++;flag=1;break;}
tmp/=10;
}
if(cnt&&!res[cnt])res[cnt]=pre;
if(flag) i++;
else break;
}
}
cin>>t;
while(t--)
{
cin>>n;
cout<<res[n]<<endl;
}
return 0;
}
1002
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 3
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.
In each test case, the first line contains two positive integers N and E (2 <= N <= 500, N <= E <= N * (N - 1) / 2), representing the number of the villages and the number of the original power lines between villages. There follow E lines, and each of them contains three integers, A, B, K (0 <= A, B < N, 0 <= K < 1000). A and B respectively means the index of the starting village and ending village of the power line. If K is 0, it means this line still works fine after the snow storm. If K is a positive integer, it means this line will cost K to reconstruct. There will be at most one line between any two villages, and there will not be any line from one village to itself.
Output
For each test case in the input, there's only one line that contains the minimum cost to recover the electric system to make sure that there's at least one way between every two villages.
Sample Input
1 3 3 0 1 5 0 2 0 1 2 9
Sample Output
5
#include<iostream>
#include<cstring>
using namespace std;
#define maxN 1027
#define INF 0x7fffffff
int mat[maxN][maxN];
int t,n,m,d[maxN];
int mark[maxN];
int prim(int s)
{
int i,j;
memset(mark,0,sizeof(mark));
for(i=1; i<n; i++)
d[i]=mat[i][s];
d[s]=0;
mark[s]=1;
int ans=0;
/*for(i=1;i<=26;i++)
printf("%d ",d[i]);
printf("\n");*/
for(i=1; i<n; i++)
{
int flag=0;
int min=INF;
int now=1;
for(j=1; j<n; j++)
{
if(!mark[j] && d[j]<min && d[j]<INF)
{
min=d[j];
now=j;
flag=1;
}
}
//printf("%d %d %d\n",i,now,d[now]);
if(flag==0) break;
mark[now]=1;
ans+=d[now];
for(j=1; j<n; j++)
{
if(!mark[j] && mat[now][j]<d[j] && mat[now][j]<INF)
{
d[j]=mat[now][j];
}
}
/*for(i=1;i<=26;i++)
printf("%d ",d[i]);
printf("\n");*/
}
return ans;
}
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
int a,b,i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
mat[i][j]=INF;
mat[i][i]=0;
}
while(m--)
{
cin>>a>>b>>k;
mat[a][b]=mat[b][a]=k;
}
int ans=prim(0);
cout<<ans<<endl;
}
return 0;
}
1005
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 11 Accepted Submission(s) : 7
(1) (C)'=0 where C is a constant.
(2) (Cx^n)'=C*n*x^(n-1) where n>=1 and C is a constant.
(3) (f1(x)+f2(x))'=(f1(x))'+(f2(x))'.
It is easy to prove that the derivation a polynomial is also a polynomial.
Here comes the problem, given a polynomial f(x) with non-negative coefficients, can you write a program to calculate the derivation of it?
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 1000) which is the number of test cases. And it will be followed by T consecutive test cases.
There are exactly 2 lines in each test case. The first line of each test case is a single line containing an integer N (0 <= N <= 100). The second line contains N + 1 non-negative integers, CN, CN-1, ..., C1, C0, ( 0 <= Ci <= 1000), which are the coefficients of f(x). Ci is the coefficient of the term with degree i in f(x). (CN!=0)
Output
For each test case calculate the result polynomial g(x) also in a single line.
(1) If g(x) = 0 just output integer 0.otherwise
(2) suppose g(x)= Cmx^m+Cm-1x^(m-1)+...+C0 (Cm!=0),then output the integers Cm,Cm-1,...C0.
(3) There is a single space between two integers but no spaces after the last integer.
Sample Input
3 0 10 2 3 2 1 3 10 0 1 2
Sample Output
0 6 2 30 0 1
代码:
#include<stdio.h>
int b[110];
int main()
{
int t,n,a;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int nn=n;
int j=0;
for(int i=1;i<=n+1;i++)
{
scanf("%d",&a);
b[j++]=a*nn;
nn--;
}
if(n==0)
printf("0\n");
else{
for(int i=0;i<j-1;i++)
{
if(i==0)
printf("%d",b[i]);
else
printf(" %d",b[i]);
}
printf("\n");
}
}
return 0;
}
1006
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 22 Accepted Submission(s) : 8
The motto of Olympic Games is "Citius, Altius, Fortius", which means "Faster, Higher, Stronger".
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F2970%2Fflag.jpg)
In this problem, there are some records in the Olympic Games. Your task is to find out which one is faster, which one is higher and which one is stronger.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.
Each test case has 3 lines. The first line is the type of the records, which can only be "Faster" "Higher" or "Stronger". The second line is a positive integer N meaning the number of the records in this test case. The third line has N positive integers, i.e. the records data. All the integers in this problem are smaller than 2008.
Output
Results should be directed to standard output. The output of each test case should be a single integer in one line. If the type is "Faster", the records are time records and you should output the fastest one. If the type is "Higher", the records are length records. You should output the highest one. And if the type is "Stronger", the records are weight records. You should output the strongest one.
Sample Input
3 Faster 3 10 11 12 Higher 4 3 5 4 2 Stronger 2 200 200
Sample Output
10 5 200
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[3000];
char str[15];
int cmp(int a,int b)
{
return a<b;
}
int main()
{
int t,n,max;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
if(strcmp(str,"Faster")==0)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1,cmp);
printf("%d\n",a[1]);
}
if(strcmp(str,"Higher")==0)
{
scanf("%d",&n);
max=-9999;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]>max)
max=a[i];
}
printf("%d\n",max);
}
if(strcmp(str,"Stronger")==0)
{
scanf("%d",&n);
max=-9999;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]>max)
max=a[i];
}
printf("%d\n",max);
}
}
return 0;
}
1007
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9 Accepted Submission(s) : 2
In the written down number the part "[abc] million" is omitted if abc = 0 , "[def] thousand" is omitted if def = 0 , and "[ghi] " is omitted if ghi = 0 . If the whole number is equal to 0 it is written down as "zero". Note that words "million" and "thousand" are singular even if the number of millions or thousands respectively is greater than one.
Numbers under one thousand are written down in the following way. The numberxyz is written as "[x] hundred and [yz] ”. ( If yz = 0 it should be only “[x] hundred”. Otherwise if y = 0 it should be only “[x] hundred and [z]”.) Here "[x] hundred and" is omitted if x = 0 . Note that "hundred" is also always singular.
Numbers under 20 are written down as "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", and "nineteen" respectively. Numbers from 20 to 99 are written down in the following way. Number xy is written as "[x0] [y] ", and numbers divisible by ten are written as "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", and "ninety" respectively.
For example, number 987,654,312 is written down as "nine hundred and eighty seven million six hundred and fifty four thousand three hundred and twelve", number 100,000,037 as "one hundred million thirty seven", number 1,000as "one thousand". Note that "one" is never omitted for millions, thousands and hundreds.
Give you the written down words of a number, please give out the original number.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 1900) which is the number of test cases. It will be followed by T consecutive test cases.
Each test case contains only one line consisting of a sequence of English words representing a number.
Output
For each line of the English words output the corresponding integer in a single line. You can assume that the integer is smaller than 109.
Sample Input
3 one eleven one hundred and two
Sample Output
1 11 102
代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char ss[50][20]= {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen","fifteen", "sixteen", "seventeen", "eighteen","nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty","ninety"
};
int tmpp[50]= {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90};
int find(char s[100])
{
for(int i=0; i<28; i++)
{
if(strcmp(s,ss[i])==0)
return tmpp[i];
}
return -1;
}
int main()
{
int n;
char str[50][20];
char s[1000];
int index=0;
int wei;
scanf("%d",&n);
getchar();
while(n--)
{
wei=0;
memset(str,0,sizeof(str));
memset(s,0,sizeof(s));
gets(s);
index=0;
int tm=0;
for(int i=0; i<strlen(s); i++)
{
if(s[i]!=' ')
{
str[index][tm]=s[i];
tm++;
}
else
{
index++;
tm=0;
}
}
int pos1=-1;
int pos2=-1;
for(int i=0; i<=index; i++)
{
if(strcmp(str[i],"million")==0)
pos1=i;
else if(strcmp(str[i],"thousand")==0)
pos2=i;
}
int sum=0;
int tmp=0;
if(pos1>0)
{
for(int i=wei; i<pos1; i++)
{
if(strcmp(str[i],"hundred")==0)
{
tmp=tmp*100;
}
else if(find(str[i])!=-1)
{
tmp=tmp+find(str[i]);
}
}
wei=pos1+1;
}
sum=sum+tmp*1000000;
//printf("%d\n",sum);
tmp=0;
if(pos2>0)
{
for(int i=wei; i<pos2; i++)
{
if(strcmp(str[i],"hundred")==0)
{
tmp=tmp*100;
}
else if(find(str[i])!=-1)
{
tmp=tmp+find(str[i]);
}
}
wei=pos2+1;
}
sum=sum+tmp*1000;
//printf("%d\n",sum);
tmp=0;
for(int i=wei; i<=index; i++)
{
if(strcmp(str[i],"hundred")==0)
{
tmp=tmp*100;
}
else if(find(str[i])!=-1)
{
tmp=tmp+find(str[i]);
}
}
sum=sum+tmp;
printf("%d\n",sum);
}
return 0;
}
1008
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 8 Accepted Submission(s) : 1
Liu Xiang is one of the famous Olympic athletes in China. In 2002 Liu broke Renaldo Nehemiah's 24-year-old world junior record for the 110m hurdles. At the 2004 Athens Olympics Games, he won the gold medal in the end. Although he was not considered as a favorite for the gold, in the final, Liu's technique was nearly perfect as he barely touched the sixth hurdle and cleared all of the others cleanly. He powered to a victory of almost three meters. In doing so, he tied the 11-year-old world record of 12.91 seconds. Liu was the first Chinese man to win an Olympic gold medal in track and field. Only 21 years old at the time of his victory, Liu vowed to defend his title when the Olympics come to Beijing in 2008.
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F2972%2FLX.jpg)
In the 110m hurdle competition, the track was divided into N parts by the hurdle. In each part, the player has to run in the same speed; otherwise he may hit the hurdle. In fact, there are 3 modes to choose in each part for an athlete -- Fast Mode, Normal Mode and Slow Mode. Fast Mode costs the player T1 time to pass the part. However, he cannot always use this mode in all parts, because he needs to consume F1 force at the same time. If he doesn't have enough force, he cannot run in the part at the Fast Mode. Normal Mode costs the player T2 time for the part. And at this mode, the player's force will remain unchanged. Slow Mode costs the player T3 time to pass the part. Meanwhile, the player will earn F2 force as compensation. The maximal force of a player is M. If he already has M force, he cannot earn any more force. At the beginning of the competition, the player has the maximal force.
The input of this problem is detail data for Liu Xiang. Your task is to help him to choose proper mode in each part to finish the competition in the shortest time.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.
Each test case begins with two positive integers N and M. And following N lines denote the data for the N parts. Each line has five positive integers T1 T2 T3 F1 F2. All the integers in this problem are less than or equal to 110.
Output
Results should be directed to standard output. The output of each test case should be a single integer in one line, which is the shortest time that Liu Xiang can finish the competition.
Sample Input
2 1 10 1 2 3 10 10 4 10 1 2 3 10 10 1 10 10 10 10 1 1 2 10 10 1 10 10 10 10
Sample Output
1 6
Hint
For the second sample test case, Liu Xiang should run with the sequence of Normal Mode, Fast Mode, Slow Mode and Fast Mode.
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int INF=1<<29;
struct node
{
int t1,t2,t3,f1,f3;
}b[120];
int dp[120][120];
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d%d%d%d%d",&b[i].t1,&b[i].t2,&b[i].t3,&b[i].f1,&b[i].f3);
}
//memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
for(int j=0;j<=m;j++)
dp[i][j]=INF;
}
for(int i=0;i<=m;i++)dp[0][i]=0;
int j;
for(int i=1;i<=n;i++)
{
for(j=m;j>=0;j--)
{
if(dp[i-1][j]!=INF) dp[i][j]=dp[i-1][j]+b[i].t2;
if(j+b[i].f3>=m&&dp[i-1][m]!=INF)
{
dp[i][j]=min(dp[i][j],dp[i-1][m]+b[i].t3);
}
else if(dp[i-1][j+b[i].f3]!=INF)
{
dp[i][j]=min(dp[i][j],dp[i-1][j+b[i].f3]+b[i].t3);
}
if(j-b[i].f1>=0&&dp[i-1][j-b[i].f1]!=INF)
{
dp[i][j]=min(dp[i][j],dp[i-1][j-b[i].f1]+b[i].t1);
}
}
}
int minn=INF;
for(int i=0;i<=m;i++)
{
if(dp[n][i]<minn)
minn=dp[n][i];
}
printf("%d\n",minn);
}
return 0;
}
/*
3
2 10
1 2 3 10 10
1 1 2 10 10
1 10
1 2 3 10 10
4 10
1 2 3 10 10
1 10 10 10 10
1 1 2 10 10
1 10 10 10 10
*/
1011
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 6 Accepted Submission(s) : 1
The official mascots of Beijing 2008 Olympic Games are Fuwa, which are named as Beibei, Jingjing, Haunhuan, Yingying and Nini. Fuwa embodies the natural characteristics of the four most popular animals in China -- Fish, Panda, Tibetan Antelope, Swallow -- and the Olympic Flame. To popularize the official mascots of Beijing 2008 Olympic Games, some volunteers make a PC game with Fuwa.
![](http://acm.zju.edu.cn/onlinejudge/showImage.do?name=0000%2F2975%2FBJHYN.jpg)
As shown in the picture, the game has a matrix of Fuwa. The player is to find out all the rectangles whose four corners have the same kind of Fuwa. You should make a program to help the player calculate how many such rectangles exist in the Fuwa matrix.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.
The first line of each test case has two integers M and N (1 <= M, N <= 250), which means the number of rows and columns of the Fuwa matrix. And then there are M lines, each has N characters, denote the matrix. The characters -- 'B' 'J' 'H' 'Y' 'N' -- each denotes one kind of Fuwa.
Output
Results should be directed to standard output. The output of each test case should be a single integer in one line, which is the number of the rectangles whose four corners have the same kind of Fuwa.
Sample Input
2 2 2 BB BB 5 6 BJHYNB BHBYYH BNBYNN JNBYNN BHBYYH
Sample Output
1 8
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=252;
char str[maxn][maxn];
int map[maxn][maxn],cnt[6];
int main()
{
int t,n,m;
cin>>t;
while(t--)
{
int i,j,k;
cin>>n>>m;
for(i=1;i<=n;i++)
{
scanf("%s",&str[i]);
for(j=0;j<m;j++)
{
if(str[i][j]=='B')map[i][j]=1;
else if(str[i][j]=='J')map[i][j]=2;
else if(str[i][j]=='H')map[i][j]=3;
else if(str[i][j]=='Y')map[i][j]=4;
else if(str[i][j]=='N') map[i][j]=5;
}
}
int ans=0;
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
memset(cnt,0,sizeof(cnt));
for(k=0;k<m;k++)
{
if(map[i][k]==map[j][k])
{
cnt[map[i][k]]++;
// cout<<map[i][k]<<" "<<cnt[map[i][k]]<<endl;
}
}
for(k=1;k<=5;k++)
{
ans+=cnt[k]*(cnt[k]-1)/2;
}
}
}
cout<<ans<<endl;
}
return 0;
}
/*
2
2 2
BB
BB
5 6
BJHYNB
BHBYYH
BNBYNN
JNBYNN
BHBYYH
*/