Ural 1095 Nikifor 3 Number Theory

Source: Internet
Author: User

Question: give you a number, which contains numbers 1 2 3 4, so that you can rearrange the number so that the number can be divisible by 7.
Idea: After calculation, we found that 1234 of them are arranged in 24 full order, and 7 are the remainder, including, and 6. Therefore, you only need to calculate the result of 7 Remainder of the number pair except 1 2 3 4 4, and then let the following 1234 complete the remainder. Pay attention to a special case, such as 1001234. When 1234 is removed, the first 1 is removed. The consequence is that the number after 1234 is removed is 1. to prevent this situation, you can add 0 at last. I used to write large numbers in java. In fact, it can be done with C ++ ..
Code:
[Cpp] www.2cto.com
Import java. util .*;
Import java. math. BigInteger;
Public class Main {
Public static void main (String [] args ){
Cin = new partition (System. in );
Int numcase;
Numcase = cin. nextInt ();
For (int j = 1; j <= numcase; ++ j ){
BigInteger n;
Int flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0;
N = cin. nextBigInteger ();
String ss = n. toString ();
BigInteger x = new BigInteger ("0 ");
BigInteger x1 = new BigInteger ("1 ");
BigInteger x2 = new BigInteger ("2 ");
BigInteger x3 = new BigInteger ("3 ");
BigInteger x4 = new BigInteger ("4 ");
BigInteger x5 = new BigInteger ("5 ");
BigInteger x6 = new BigInteger ("6 ");
BigInteger sum = x;
Char ch [] = ss. toCharArray ();
Int cntzero = 0;
For (int I = 0; I <ss. length (); ++ I ){
If (ch [I] = '1' & flag1 = 0 ){
Flag1 = 1;
Continue;
}
Else if (ch [I] = '2' & flag2 = 0 ){
Flag2 = 1;
Continue;
}
Else if (ch [I] = '3' & flag3 = 0 ){
Flag3 = 1;
Continue;
}
Else if (ch [I] = '4' & flag4 = 0 ){
Flag4 = 1;
Continue;
}
Else if (ch [I] = '0 '){
Cntzero ++;
Continue;
}
Int y = ch [I]-'0 ';
BigInteger z = new BigInteger ("" + y );
BigInteger zz = new BigInteger ("10 ");
Sum = sum. multiply (zz );
Sum = sum. add (z );
}
BigInteger p = new BigInteger ("10000 ");
Sum = sum. multiply (p );
Ss = sum. toString ();
// System. out. println (ss );
BigInteger q = new BigInteger ("7 ");
BigInteger mm = sum. mod (q );
Mm = q. subtract (mm );
Ss = mm. toString ();
// System. out. println (ss );
BigInteger ans;
If (mm. compareTo (q) = 0 ){
Ans = new BigInteger ("3241 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x1) = 0 ){
Ans = new BigInteger ("1324 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x2) = 0 ){
Ans = new BigInteger ("1234 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x3) = 0 ){
Ans = new BigInteger ("2341 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x4) = 0 ){
Ans = new BigInteger ("1243 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x5) = 0 ){
Ans = new BigInteger ("1342 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
Else if (mm. compareTo (x6) = 0 ){
Ans = new BigInteger ("2134 ");
Sum = sum. add (ans );
Ss = sum. toString ();
System. out. print (ss );
}
For (int k = 0; k <cntzero; ++ k)
System. out. print (0 );
System. out. println ();
}
}
}

Author: wmn_wmn

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.