[Miscellaneous Questions] Csuoj1274balls and Boxes

Source: Internet
Author: User

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1274

Test instructions: Chinese problem test instructions not much to repeat

It is worth noting that N<m does not have to consider the situation of n==m (M is the number of boxes, n is the number of boxes selected each time, do not get reversed!) )

The problem is that the congruence equation

Each time you select N boxes to place the ball, that means you add n balls each time.

The number of balls in the last m box is equal, that is, the total number of final state balls is a multiple of M

So it's easy to get the congruence equation: sum+x*n=y*m (sum is the sum of the balls for present state)

where x is required to select X times box to place the ball

Y for final each box has a Y ball

Next, as long as the Juchenchu equation

If there is no solution, then no matter how the operation can not achieve this goal

There are several conditions to be satisfied if there is a solution:

(The first thing you need to record: the box with the most initial state has MAXN balls, with the fewest Minn balls)

Y>=maxn that is, the number of balls in each box must be greater than or equal to the maximum number of balls in the initial state of the box.

X>=y-minn is the choice of the box to play the ball must be greater than or equal to the final state of the number of balls in each box and the initial state of the ball in the box the minimum number of differences

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <climits>5#include <cctype>6#include <cmath>7#include <string>8#include <sstream>9#include <iostream>Ten#include <algorithm> One#include <iomanip> A using namespacestd; -#include <queue> -#include <stack> the#include <vector> -#include <deque> -#include <Set> -#include <map> +typedefLong LongLL; -typedefLong DoubleLD; + Const DoublePi=acos (-1.0); A Const Doubleeps=1e-9; at #defineINF 0x3f3f3f - #defineLson L, M, rt<<1 - #defineRson m+1, R, rt<<1|1 -typedef pair<int,int>PI; -typedef pair<int, PI >PP; - #ifdef _WIN32 in #defineLLD "%i64d" - #else to #defineLLD "%lld" + #endif - //#pragma COMMENT (linker, "/stack:1024000000,1024000000") the //ll Quick (ll A, ll b) {ll ans=1;while (b) {if (b & 1) ans*=a;a=a*a;b>>=1;} return ans;} * //inline int Read () {char ch= "; int ans=0;while (ch< ' 0 ' | | ch> ' 9 ') Ch=getchar (); while (ch<= ' 9 ' && Ch >= ' 0 ') {ans=ans*10+ch-' 0 '; Ch=getchar ();} return ans;} $InlinevoidPrint (LL x) {printf (LLD, x);p UTS ("");}Panax Notoginseng //inline void Read (LL &ret) {char c;int sgn; LL bit=0.1;if (C=getchar (), c==eof) return, while (c!= '-' &&c!= '. && (c< ' 0 ' | | C> ' 9 ')) C=getchar (), sgn= (c== '-'), -1:1;ret= (c== '-')? 0: (c ' 0 '); while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') ret=ret*10+ (c ' 0 '); if (c== "| | c== ' \ n ') {ret*=sgn; return;} while (C=getchar (), c>= ' 0 ' &&c<= ' 9 ') ret+= (c ' 0 ') *bit,bit/=10;ret*=sgn;} -  the voidEX_GCD (ll A, ll B, LL &d, LL &x, LL &y) + { A     if(b) the     { +EX_GCD (b, a%B, D, X, y); -LL tmp=x; $x=y; $Y=tmp-(A/b) *y; -     } -     Else the     { -x=1, y=0, d=A;Wuyi         return ; the     } - } Wu intMain () - { About     intT; $scanf"%d", &t); -      while(t--) -     { -         intN, M; Ascanf"%d%d", &n, &m); +         intsum=0, maxn=0, minn=Int_max; the          for(intI=0; i<n;i++) -         { $             intA; thescanf"%d", &a); thesum+=A; themaxn=Max (MAXN, a); theminn=min (Minn, a); -         } in LL D, x, y; the EX_GCD (n, M, d, X, y); the         if(sum%d) Aboutprintf"-1\n"); the         Else the         { thex*=sum/D; +y*=sum/D; -LL ans1= (maxn-x) *d/m; theLL ans2= (X+y-minn) *d/(nm);Bayi             if((x+m/d*ans1) &LT;MAXN) ans1++; the             if((N/d*ans2-y) < ((X+M/D*ANS2)-minn)) ans2++; thePrint (N/d*max (ans1, Ans2)-y); -         } -     } the     return 0; the}
Csuoj 1274

At last... The above is the result of pure yy ... Concrete proof or directly look at the official matter well, I can not prove it anyway (the http://acm.csu.edu.cn/csuacm/2013/04/)

[Miscellaneous Questions] Csuoj1274balls and Boxes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.