hdu3579 (linear congruence equation set)

Source: Internet
Author: User

Hello Kiki

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2734 Accepted Submission (s): 1010


Problem Descriptionone Day I is shopping in the supermarket. There is a cashier counting coins seriously when a little kid running and singing "in front of the bridge over a group of ducks, come quickly to count, 24678". And then the cashier put the counted coins back morosely and count again ...
Hello Kiki is such a lovely girl so she loves doing counting in a different the. For example, when she was counting X coins, she count them N times. Each time she divide the coins to several same sized groups and write down the group size Mi and the number of the Remai Ning coins Ai on her note.
One day Kiki's father found she note and he wanted to know what much coins Kiki was counting.

Inputthe first line is T indicating the number of test cases.
Contains n on the first line, Mi (1 <= i <= N) on the second line, and corresponding Ai (1 <= i <= N) On the third line.
All numbers in the input and output is integers.
1 <= T <=, 1 <= N <= 6, 1 <= Mi <=, 0 <= Ai < mi

Outputfor Each case output the least positive integer X which Kiki is counting in the sample output format. If there is no solution then output-1.

Sample Input2214 575 56519 54 40 24 8011 2 36 20 76

Sample outputcase 1:341case 2:5,996

Authordigiter (Special Thanks Echo)

Source acm-icpc multi-university Training Contest (--host by Bjtu)
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<stdlib.h>using namespacestd;/*for X=r0 (mod M0) x=r1 (mod m1) ... x=rn (mod mn) input arrays M and array R, returns the 0,[m0,m1 of [mn]-1,..., x0] in the range that satisfies the above equation. All solutions for X are: X0+z*[m0,m1,... mn] (z is an integer)*/Long LongCAL_AXB (Long LongALong LongBLong LongMoD) {    //anti-Multiplication overflow    Long Longsum=0;  while(b) {if(b&1) sum= (sum+a)%MoD; b>>=1; A= (a+a)%MoD; }    returnsum;}//ax + by = gcd (A, b)//incoming fixed value A, B. Put back D=GCD (A, B), X, yvoidEXTENDGCD (Long LongALong LongBLong Long&d,Long Long&x,Long Long&y) {    if(b==0) {d=a;x=1; y=0;return;} EXTENDGCD (B,a%b,d,y,x); Y-= x* (A/b);}Long LongMulti_modx (Long LongM[],Long LongR[],intNLong Long&M) {    Long Longm0,r0; M0=m[0]; r0=r[0];  for(intI=1; i<n;i++)    {        Long Longm1=m[i],r1=R[i]; Long LongK0,k1; Long Longtmpd;        EXTENDGCD (M0,M1,TMPD,K0,K1); if((R1-R0)%tmpd!=0)return-1; K0*= (R1-R0)/tmpd; M1*= m0/tmpd; R0= (CAL_AXB (k0,m0,m1) +r0)%M1; M0=M1; } M=M0; return(R0%M0+M0)%M0;}intMain () {intT; CIN>>T; inttt=1;  while(t--)    {        intN; CIN>>N; Long Longa[Ten],b[Ten];  for(intI=0; i<n;i++) Cin>>A[i];  for(intI=0; i<n;i++) Cin>>C[i]; Long LongM; Long Longans=Multi_modx (a,b,n,m); printf ("Case %d:", tt++); if(ans==0) ans+=l; cout<<ans<<Endl; }    return 0;}

hdu3579 (linear congruence equation set)

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.