Problem Description
Professor Zhang has a number sequence . However, the sequence is not complete and some elements are missing. Fortunately, Professor Zhang remembers some properties of the sequence:
- For every , .
- The sequence is non-increasing, i.e. .
- The sum of all elements in the sequence is not zero.
Professor Zhang wants to know the maximum value of among all the possible sequences.
Input
There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:
The first contains two integers and – the length of the sequence and the number of known elements.
In the next lines, each contains two integers and , indicating that .
Output
For each test case, output the answer as an irreducible fraction “/”, where , are integers, .
Sample Input
2
2 0
3 1
3 1
Sample Output
1/1
200/201
题意很简单,前两个数要尽可能大,后面的数要尽可能小,序列又是非递增的,最大值为200
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <map>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 1010
#define mod 1000000007
using namespace std;
long long gcd(long long a,long long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int a[MAXN];
int main()
{
int t,n,m,x,y;
scanf("%d",&t);
while(t--)
{
memset(a,-1,sizeof(a));
scanf("%d%d",&n,&m);
if(m==0)
{
printf("1/1\n");
continue;
}
for(int i=0; i<m; ++i)
{
scanf("%d%d",&x,&y);
a[x]=y;
}
int ans1=0,ans2=0;
if(a[1]==-1&&a[2]==-1)
ans1=200;
if(a[1]!=-1&&a[2]==-1)
ans1=2*a[1];
if(a[1]==-1&&a[2]!=-1)
ans1=100+a[2];
if(a[1]!=-1&&a[2]!=-1)
ans1=a[1]+a[2];
int p=2;
for(int i=3; i<=n; ++i)
{
if(a[i]!=-1)
{
ans2+=(i-p)*a[i];
p=i;
}
}
ans2+=ans1;
if(ans1==0&&ans2==0)
ans2++;
int c=gcd(ans1,ans2);
ans1/=c,ans2/=c;
printf("%d/%d\n",ans1,ans2);
}
return 0;
}