A
HDU - 2106
Description
As we know , we always use the decimal system in our common life, even using the computer. If we want to calculate the value that 3 plus 9, we just import 3 and 9.after calculation of computer, we will get the result of 12.
But after learning <<The Principle Of Computer>>,we know that the computer will do the calculation as the following steps: 1 computer change the 3 into binary formality like 11; 2 computer change the 9 into binary formality like 1001; 3 computer plus the two number and get the result 1100; 4 computer change the result into decimal formality like 12; 5 computer export the result; In the computer system there are other formalities to deal with the number such as hexadecimal. Now I will give several number with a kind of change method, for example, if I give you 1011(2), it means 1011 is a number in the binary system, and 123(10) means 123 if a number in the decimal system. Now I will give you some numbers with any kind of system, you guys should tell me the sum of the number in the decimal system. Input
There will be several cases. The first line of each case contains one integers N, and N means there will be N numbers to import, then there will be N numbers at the next N lines, each line contains a number with such form : X1….Xn.(Y), and 0<=Xi<Y, 1<Y<=10. I promise you that the sum will not exceed the 100000000, and there will be at most 100 cases and the 0<N<=1000.
Output
There is only one line output case for each input case, which is the sum of all the number. The sum must be expressed using the decimal system.
Sample Input
Sample Output
|
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 10010
#define M 1000000007
using namespace std;
int main()
{
int t,n,m;
int i,j,k;
ll nn,mm;
while(scanf("%d",&t)!=EOF)
{
ll sum=0;
while(t--)
{
scanf("%d(%d)",&n,&m);
nn=0;k=0;
while(n)
{
mm=n%10;
nn+=mm*pow(m,k);
n/=10;
k++;
}
sum+=nn;
}
printf("%lld\n",sum);
}
return 0;
}
HDU - 1087
Description
Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.
The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path. Your task is to output the maximum value according to the given chessmen list. Input
Input contains multiple test cases. Each test case is described in a line as follow:
N value_1 value_2 …value_N It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int. A test case starting with 0 terminates the input and this test case is not to be processed. Output
For each case, print the maximum according to rules, and one line one case.
Sample Input
Sample Output
|
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 1010
#define M 1000000007
using namespace std;
ll dp[N];
ll a[N];
int main()
{
int t,n,m;
int i,j,k;
while(scanf("%d",&n),n)
{
memset(dp,0,sizeof(dp));
for(i=0;i<n;i++)
scanf("%d",&a[i]),dp[i]=a[i];
dp[0]=a[0];
ll maxx=-1;
for(i=0;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[j]<a[i])
dp[i]=max(dp[i],dp[j]+a[i]);
}
maxx=max(maxx,dp[i]);
}
printf("%lld\n",maxx);
}
return 0;
}
POJ - 2549
Description
Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S.
Input
Several S, each consisting of a line containing an integer 1 <= n <= 1000 indicating the number of elements in S, followed by the elements of S, one per line. Each element of S is a distinct integer between -536870912 and +536870911 inclusive. The last line of input contains 0.
Output
For each S, a single line containing d, or a single line containing "no solution".
Sample Input 5 2 3 5 7 12 5 2 16 64 256 1024 0 Sample Output 12 no solution |
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF -0x3f3f3f3f
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 10010
#define M 1000000007
using namespace std;
int a[1010];
int main()
{
int n;
int i,j;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
int ans=INF;
for(i=n-1;i>=0;i--)
{
for(j=n-1;j>=0;j--)
{
if(i==j)
continue;
int sum=a[i]-a[j];
int l=0,r=j-1;
while(l<r)
{
if(a[l]+a[r]==sum&&l!=j&&r!=i)
{
ans=a[i];
break;
}
if(a[l]+a[r]>sum)
r--;
else
l++;
}
if(ans!=INF)
break;
}
if(ans!=INF)
break;
}
if(ans==INF)
printf("no solution\n");
else
printf("%d\n",ans);
}
return 0;
}
HDU - 1443
Description
The Joseph's problem is notoriously known. For those who are not familiar with the original problem: from among n people, numbered 1, 2, . . ., n, standing in circle every mth is going to be executed and only the life of the last remaining person will be saved. Joseph was smart enough to choose the position of the last remaining person, thus saving his life to give us the message about the incident. For example when n = 6 and m = 5 then the people will be executed in the order 5, 4, 6, 2, 3 and 1 will be saved.
Suppose that there are k good guys and k bad guys. In the circle the first k are good guys and the last k bad guys. You have to determine such minimal m that all the bad guys will be executed before the first good guy. Input
The input file consists of separate lines containing k. The last line in the input file contains 0. You can suppose that 0 < k < 14.
Output
The output file will consist of separate lines containing m corresponding to k in the input file.
Sample Input
Sample Output
|
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#define INF 0x3f3f3f3f
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 10010
#define M 1000000007
using namespace std;
int k;
int dp[15];
bool solve(int m)
{
int cur=0;
for(int i=1;i<=k;i++)
{
if((cur+m-1)%(2*k-i+1)<k)
return false;
cur=(cur+m-1)%(2*k-i+1);
}
return true;
}
int main()
{
for(k=1;k<15;k++)
{
int i;
for(i=1;;i++)
{
if(solve(i))
break;
}
dp[k]=i;
}
while(~scanf("%d",&k),k)
{
printf("%d\n",dp[k]);
}
return 0;
}
CodeForces - 618B
Description Bob has a permutation of integers from 1 to n. Denote this permutation as p. The i-th element of p will be denoted as pi. For all pairs of distinct integers i, j between 1 and n, he wrote the number ai, j = min(pi, pj). He writes ai, i = 0 for all integeri from 1 to n. Bob gave you all the values of ai, j that he wrote down. Your job is to reconstruct any permutation that could have generated these values. The input will be formed so that it is guaranteed that there is at least one solution that is consistent with the information given. Input The first line of the input will contain a single integer n (2 ≤ n ≤ 50). The next n lines will contain the values of ai, j. The j-th number on the i-th line will represent ai, j. The i-th number on the i-th line will be 0. It's guaranteed that ai, j = aj, i and there is at least one solution consistent with the information given. Output Print n space separated integers, which represents a permutation that could have generated these values. If there are multiple possible solutions, print any of them. Sample Input
Input
2 0 1 1 0
Output
2 1
Input
5 0 2 2 1 2 2 0 4 1 3 2 4 0 1 3 1 1 1 0 1 2 3 3 1 0
Output
2 5 4 1 3 Hint In the first case, the answer can be {1, 2} or {2, 1}. In the second case, another possible answer is {2, 4, 5, 1, 3}. //模拟:
Description
Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.
Input
The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores. Output
For each test case, you should output the smallest total reduced score, one line per test case.
Sample Input
Sample Output
|
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 1010
#define M 1000000007
using namespace std;
struct zz
{
int d;
int f;
}p[N];
bool cmp(zz a,zz b)
{
return a.f>b.f;
}
int a[N];
int main()
{
int t,n,m;
int i,j,k;
scanf("%d",&t);
while(t--)
{
memset(a,0,sizeof(a));
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&p[i].d);
for(i=0;i<n;i++)
scanf("%d",&p[i].f);
sort(p,p+n,cmp);
m=0;
for(i=0;i<n;i++)
{
k=0;
for(j=p[i].d;j>0;j--)
{
if(a[j]==0)
{
a[j]=1;
k=1;
break;
}
}
if(k==0)
m+=p[i].f;
}
printf("%d\n",m);
}
return 0;
}
HDU - 1709
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
Input
Output
Sample Input
3 1 2 4 3 9 2 1
Sample Output
0 2 4 5
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#define IN __int64
#define ull unsigned long long
#define ll long long
#define N 10010
#define M 1000000007
using namespace std;
int a[N],b[N],c[N],d[N];
int main()
{
int t,n,m;
int i,j,k;
while(scanf("%d",&n)!=EOF)
{
int s=0;
for(i=1;i<=n;i++)
scanf("%d",&c[i]),s+=c[i];
for(i=0;i<=s;i++)
{
a[i]=0;
b[i]=0;
}
for(i=0;i<=c[1];i+=c[1])
a[i]=1;
for(i=2;i<=n;i++)
{
for(j=0;j<=s;j++)
{
for(k=0;k+j<=s&&k<=c[i];k+=c[i])
{
if(k>=j)
b[k-j]+=a[j];
else
b[j-k]+=a[j];
b[j+k]+=a[j];
}
}
for(j=0;j<=s;j++)
{
a[j]=b[j];
b[j]=0;
}
}
k=0;
for(i=0;i<=s;i++)
{
if(a[i]==0)
d[k++]=i;
}
printf("%d\n",k);
if(k)
{
for(i=0;i<k-1;i++)
printf("%d ",d[i]);
printf("%d\n",d[k-1]);
}
}
return 0;
}
HDU - 2407
Description
An even number N of strands are stuck through a wall. On one side of the wall, a girl ties N/2 knots between disjoint pairs of strands. On the other side of the wall, the girl's groom-to-be also ties N/2 knots between disjoint pairs of strands. You are to find the probability that the knotted strands form one big loop (in which case the couple will be allowed to marry).
For example, suppose that N = 4 and you number the strands 1, 2, 3, 4. Also suppose that the girl has created the following pairs of strands by tying knots: {(1, 4), (2,3)}. Then the groom-to-be has two choices for tying the knots on his side: {(1,2), {3,4)} or {(1,3), (2,4)}. Input
The input file consists of one or more lines. Each line of the input file contains a positive even integer, less than or equal to 100. This integer represents the number of strands in the wall.
Output
For each line of input, the program will produce exactly one line of output: the probability that the knotted strands form one big loop, given the number of strands on the corresponding line of input. Print the probability to 5 decimal places.
Sample Input
Sample Output
|
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define ll long long
#define N 1010
#define M 1000000007
using namespace std;
double s[110];
int main()
{
int t,n,m;
int i,j,k;
s[2]=1;
for(i=4;i<=100;i+=2)
{
double ss=(i-2)*1.0/(i-1);
s[i]=s[i-2]*ss;
}
while(scanf("%d",&n)!=EOF)
{
printf("%.5lf\n",s[n]);
}
return 0;
}
POJ - 3268
Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse. Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different from her original route to the party since roads are one-way. Of all the cows, what is the longest amount of time a cow must spend walking to the party and back? Input
Line 1: Three space-separated integers, respectively:
N,
M, and
X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse. Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input 4 8 2 1 2 4 1 3 2 1 4 7 2 1 1 2 3 5 3 1 2 3 4 4 4 2 3 Sample Output 10 Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
|
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN__int64
#define N 1010
#define M 1000000007
using namespace std;
int map[N][N];
int a[N];
int vis[N],dis[N];//回去的最短距离
int vis1[N],dis1[N];//去的时候的最短距离
int n,m;
int djs(int y)
{
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
memset(vis1,0,sizeof(vis1));
memset(dis1,INF,sizeof(dis1));
dis[y]=0;dis1[y]=0;
int i,j,k,k1;
for(j=1;j<=n;j++)
{
k=-1;k1=-1;
for(i=1;i<=n;i++)
{
if(!vis[i]&&(k==-1||dis[i]<dis[k]))
k=i;
if(!vis1[i]&&(k1==-1||dis1[i]<dis1[k1]))
k1=i;
}
vis[k]=1;vis1[k1]=1;
for(i=1;i<=n;i++)
{
dis[i]=min(dis[i],dis[k]+map[k][i]);//回去
dis1[i]=min(dis1[i],dis1[k1]+map[i][k1]);//来
}
}
}
int main()
{
int i,j,k,x;
while(scanf("%d%d%d",&n,&m,&x)!=EOF)
{
int u,v,w;
memset(map,INF,sizeof(map));
memset(a,0,sizeof(a));
for(i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(map[u][v]>w)
map[u][v]=w;
}
djs(x);
int mm=0;
for(i=1;i<=n;i++)
{
if(i==x)
continue;
mm=max(mm,dis[i]+dis1[i]);
}
printf("%d\n",mm);
}
return 0;
}
POJ - 3181
Description
Farmer John goes to Dollar Days at The Cow Store and discovers an unlimited number of tools on sale. During his first visit, the tools are selling variously for $1, $2, and $3. Farmer John has exactly $5 to spend. He can buy 5 tools at $1 each or 1 tool at $3 and an additional 1 tool at $2. Of course, there are other combinations for a total of 5 different ways FJ can spend all his money on tools. Here they are:
1 @ US$3 + 1 @ US$2 1 @ US$3 + 2 @ US$1 1 @ US$2 + 3 @ US$1 2 @ US$2 + 1 @ US$1 5 @ US$1Write a program than will compute the number of ways FJ can spend N dollars (1 <= N <= 1000) at The Cow Store for tools on sale with a cost of $1..$K (1 <= K <= 100). Input
A single line with two space-separated integers: N and K.
Output
A single line with a single integer that is the number of unique ways FJ can spend his money.
Sample Input 5 3 Sample Output 5 |
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#define INF 1000000000000000000
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 1010
#define M 1000000007
using namespace std;
ll a[N],b[N];
int main()
{
int n,k;
while(scanf("%d%d",&n,&k)!=EOF)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
b[0]=1;
for(int i=1;i<=k;i++)
{
for(int j=i;j<=n;j++)
{
a[j]=a[j]+a[j-i]+(b[j]+b[j-i])/INF;
b[j]=(b[j]+b[j-i])%INF;
}
}
if(a[n])
printf("%lld",a[n]);
printf("%lld\n",b[n]);
}
return 0;
}