codeforces 1244 C The Football Season

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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章