https://codeforces.com/problemset/problem/1244/C
8分钟做完AB,C卡了2小时
一看题发现我以前写过类似的巨爽
https://blog.csdn.net/liufengwei1/article/details/79598652
然后就开始WA了
因为我们x+y+z=n,且d>w,所以x要尽量大,x+y才会尽可能小
WA了以后那网上的那题的代码复制过来魔改,还是在WA卧槽
没想到是exgcd中间会爆long long,改成java就过了
import java.math.BigInteger;
import java.util.*;
public class Main
{
public static long nn,p,d,w,xx,yy;
public static BigInteger x,y,n;
public static BigInteger exgcd(BigInteger a,BigInteger b)
{
if(b.equals(BigInteger.ZERO))
{
x=BigInteger.ONE;
y=BigInteger.ZERO;
return a;
}
BigInteger gcd=exgcd(b,a.mod(b)),t=x;
x=y;y=t.subtract(a.divide(b).multiply(y));
return gcd;
}
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
nn=input.nextLong();p=input.nextLong();
d=input.nextLong();w=input.nextLong();
BigInteger n=BigInteger.valueOf(p);
BigInteger a=BigInteger.valueOf(d);
BigInteger b=BigInteger.valueOf(w);
BigInteger gcd=exgcd(a,b);
if(n.mod(gcd).equals(BigInteger.ZERO))
{
x=x.multiply(n.divide(gcd));
y=y.multiply(n.divide(gcd));
y=y.mod(a.divide(gcd));
if(y.compareTo(BigInteger.ZERO)<0)
y=y.add(a.divide(gcd));
x=n.subtract(y.multiply(b)).divide(a);
xx=x.longValue();yy=y.longValue();
if(xx>=0 &&yy>=0 &&xx+yy<=nn)
System.out.println(xx+" "+yy+" "+(nn-xx-yy));
else
System.out.println("-1");
}
else
{
System.out.println("-1");
}
}
}
#include<bits/stdc++.h>
#define maxl 300010
using namespace std;
long long nn,p,d,w,x,y,n;
inline void prework()
{
scanf("%lld%lld%lld%lld",&nn,&p,&w,&d);
}
long long exgcd(long long a,long long b)
{
if(b==0)
{
x=1;y=0;
return a;
}
long long gcd=exgcd(b,a%b),t=x;
x=y;y=t-(a/b)*y;
return gcd;
}
inline void mainwork()
{
n=p;
long long a=w,b=d,gcd=exgcd(a,b);
if(n%gcd!=0)
{
puts("-1");
return;
}
x=x*(n/gcd);y=y*(n/gcd);
y=y%(a/gcd);
if(y<0)
y+=a/gcd;
x=(n-y*b)/a;
if(x>=0 && y>=0 && x+y<=nn)
printf("%lld %lld %lld",x,y,nn-x-y);
else
puts("-1");
}
inline void print()
{
}
int main()
{
int t=1;
//scanf("%d",&t);
for(int i=1;i<=t;i++)
{
prework();
mainwork();
print();
}
return 0;
}