A.Pens and Pencils
传送门
签到题
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
int t;
double a,b,c,d,k;
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d,&k);
int x=ceil(a/c);
int y=k-x;
if(y>=ceil(b/d))
{
printf("%d %d\n",x,y);
}
else
{
printf("-1\n");
}
}
return 0;
}
B.Rooms and Staircases
传送门
分别从左往右,和从右往左找第一个‘1’,最佳走法应该是走到‘1’后走到下一层,然后往回走,如图中的 1-2-3-4-4-3-2-1 。取(l-1,n-r) 的最小值,在用 n 减,即是走一层的最长路径.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
using namespace std;
int n,t;
char str[1005];
int main()
{
scanf("%d",&t);
while(T--)
{
scanf("%d%s",&n,str+1);
int l=-1,r=-1;
for(int i=1;i<=n;i++)
{
if(str[i]=='1')
{
l=i;
break;
}
}
for(int i=n;i>=1;i--)
{
if(str[i]=='1')
{
r=i;
break;
}
}
if(l==-1 && r==-1)
printf("%d\n",n);
else
printf("%d\n",2*(n-min(l-1,n-r)));
}
return 0;
}
C.The Football Season
传送门
扩展欧几里得定理,求一元二次方程,只要判断两个最小正整数x0,y0的两种情况下可不可以实现,如果这两种都不能符合,那么其他的也一定无法实现。
最初要判断方程是否有解,中途注意取模,否则会爆。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
ll n,p,w,d;
ll gcd(ll a,ll b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
ll r=exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-(a/b)*y;
return r;
}
int main()
{
scanf("%lld%lld%lld%lld",&n,&p,&w,&d);
ll x,y;
ll ans=exgcd(w,d,x,y);
if(p%ans!=0)
{
printf("-1\n");
return 0;
}
else
{
ll s=d/ans;
x=x%s*((p/ans)%s);
//x=x*p/ans; 这样处理会爆
ll x0=(x%s+s)%s;
ll y0=(p-w*x0)/d;
if(x0+y0<=n&&x0>=0&&y0>=0)
{
printf("%lld %lld %lld\n",x0,y0,n-x0-y0);
return 0;
}
ll ss=w/ans;
y0=(y0%ss+ss)%ss;
x0=(p-d*y0)/w;
if(x0+y0<=n&&x0>=0&&y0>=0)
{
printf("%lld %lld %lld\n",x0,y0,n-x0-y0);
return 0;
}
printf("-1\n");
}
return 0;
}