锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
緇撹: 鏁版牴f(n)涓巒妯?鍚屼綑錛屼笖f(n)鐨勮寖鍥村睘浜嶽1,9].
璇佹槑錛氬鏋渘 = am*10m + am-1*10m-1 +...+ a0*100 , 浠(n) = am+am-1+...+a0.
n -> [n0=g(n)] -> [n1=g(n0)] -> [n2=g(n1)] -> ... -> f(n)
涓棿鐨勬瘡涓幆鑺傞兘鏄ā9鍚屼綑鐨勶紝浼犻掍笅鍘伙紝鎵浠鍜宖(n)涔熸槸妯?鍚屼綑鐨?br>
2import java.io.FileNotFoundException;
3import java.util.Scanner;
4
5
6/**//*
7 * To change this template, choose Tools | Templates
8 * and open the template in the editor.
9 */
10/** *//**
11 *
12 * @author wangzhihao
13 */
14class Seq
{
15
16 int[] A;
17
18 Seq(int[] a)
{
19 A = a;
20 }
21 int DigitSum()
{
22 int res = 0, term = 1;
23 for (int i = 0; i < A.length; i++)
{
24 term = term * ( A[i] % 9) % 9;
25 res = ( res + term ) % 9;
26 }
27 return res == 0 ? 9 : res;
28 }
29}
30
31public class Solution
{
32
33 /** *//**
34 * @param args the command line arguments
35 */
36 public static void main(String[] args) throws FileNotFoundException
{
37 Scanner sc = new Scanner(System.in);
38 int testCase = sc.nextInt();
39 for (int cas = 1; cas <= testCase; cas++)
{
40 int n = sc.nextInt();
41 int[] a = new int[n];
42 for (int i = 0; i < n; i++)
{
43 a[i] = sc.nextInt();
44 }
45 Seq seq = new Seq(a);
46 System.out.println(seq.DigitSum());
47 }
48 }
49}
50
]]>
accept code
2 import java.util.Scanner;
3 import java.util.*;
4 import java.math.BigInteger;
5
6 class rook{
7 int n, k;
8
9 long [][]C;
10
11 rook(int _n, int _k){
12 n = _n;
13 k = _k;
14 }
15
16 long fac(int k){
17 if(k==0)
18 return 1;
19 else
20 return k * fac(k-1);
21 }
22
23 long count(){
24
25 C = new long[n+1][n+1];
26
27 for(int i = 0; i <= n; i++){
28 C[i][0] = C[i][i] = 1;
29 for(int j = 1; j < i; j++)
30 C[i][j] = C[i-1][j-1] + C[i-1][j];
31 }
32
33 if(n < k){
34 return 0;
35 }
36 else{
37 long kk = C[n][k];
38 return kk * kk * fac( k );
39 }
40 }
41 }
42
43 public class Solution{
44 public static void main(String[] args)throws Exception{
45 Scanner sc = new Scanner(System.in);
46
47 rook r = new rook( sc.nextInt(), sc.nextInt() );
48
49 System.out.println( r.count() );
50 }
51 }
]]>
棣栧厛絎竴涓瀵熸槸鐧借壊鏍煎瓙鍜岄粦鑹叉牸瀛愭槸鐙珛鐨勶紝鍙互鍒嗗紑鑰冭檻錛屾敞鎰忎簩鑰呬笉涓瀹氬縐般傜劧鍚庢棆杞鐩橈紝寰楀埌涓涓彵褰㈢殑鏈熺浖錛屾敞鎰忓埌琛屽垪欏哄簭鏄棤鍏崇殑錛屾墍浠ュ彲浠ヤ氦鎹㈣鍒楋紝浠庡皬鍒板ぇ寮濮媎p銆?br>
闄勫綍涓錛歛ccept浠g爜錛坉p錛?br>
2
3 import java.util.Scanner;
4 import java.util.*;
5 import java.math.BigInteger;
6
7 class biShop{
8 int n, k;
9 biShop(int _n, int _k){
10 n = _n;
11 k = _k;
12 }
13
14 BigInteger [] countOneSide(Vector<Integer> upper){
15 int [] upp = new int[upper.size()];
16 for(int i = 0; i < upp.length; i++){
17 upp[i] = upper.get(i);
18 }
19 Arrays.sort(upp);
20
21 BigInteger [] pre, now;
22 now = new BigInteger[1];
23 now[0] = BigInteger.ONE;
24
25 for(int i = 0; i < upp.length; i++){
26 pre = now;
27 now = new BigInteger[ upp[i] + 1 ];
28 for(int j = 0; j < now.length; j++){
29 now[j] = BigInteger.ZERO;
30 if( j < pre.length )
31 now[j] = now[j].add( pre[j] );
32 if( 0 < j && j <= pre.length)
33 now[j] = now[j]. add( pre[j-1].multiply(BigInteger.valueOf(upp[i] - j + 1)) );
34 }
35 }
36 return now;
37 }
38
39 BigInteger count(){
40 Vector<Integer> A = new Vector<Integer>();
41 Vector<Integer> B = new Vector<Integer>();
42
43 A.add(n);
44 for(int i = n-1, j = 0; i > 0; i--, j++){
45 if( (j & 1) == 0){
46 B.add(i);
47 B.add(i);
48 }else{
49 A.add(i);
50 A.add(i);
51 }
52 }
53
54 BigInteger [] a = countOneSide(A);
55 BigInteger [] b = countOneSide(B);
56 BigInteger res = BigInteger.ZERO;
57
58 for(int i = 0; i <= k; i++){
59 if(i >= a.length || k - i >= b.length)continue;
60 res = res.add( a[i] .multiply( b[k-i]) );
61 }
62 return res;
63 }
64 }
65
66 public class Solution{
67 public static void main(String[] args)throws Exception{
68 Scanner sc = new Scanner(System.in);
69
70 biShop b = new biShop( sc.nextInt(), sc.nextInt() );
71
72 System.out.println( b.count() );
73 }
74 }
]]>
寮濮嬪啓浜嗕竴涓泦鍚圖P鐨勪唬鐮併傜粨鏋淭LE錛屾劅瑙夋瘮杈冨鎬紝綆楀鏉傚害鏄?7*17*(2^17)=3鍗冨涓囥備笉榪囪繃浜?00+鐨勪漢錛屽仛鍒拌繖閲屽氨鎰熻鑷繁鍋氬鏉備簡銆?br>
鍚庢潵鎵嶅彂鐜扮洿鎺ユ悳鍙互璺戝埌52ms錛屼笉榪囧埌鐜板湪榪樻槸涓嶄細浼拌鎼滅儲鐨勬晥鐜囥傚彧鏄劅瑙夋悳绱㈠緢寮哄ぇ銆?br>
闄勫綍 涓錛歍LE浠g爜錛堥泦鍚圖P錛?br>
2
3 class Permutation{
4 int []digits;
5 int [][]visit;
6 int size;
7
8 void read(){
9 Scanner sc = new Scanner(System.in);
10 String str = sc.next();
11 digits = new int[str.length()];
12 for(int i = 0; i < str.length(); i++)
13 digits[i] = str.charAt(i) - '0';
14
15 size = 1 << digits.length;
16 visit = new int[size][17];
17 for(int i = 0; i < size; i++)
18 for(int j = 0; j < 17; j++){
19 visit[i][j] = -1;
20 }
21 }
22
23 boolean find(int a, int b){
24
25 if(a==0)return b==0;
26
27 if(visit[a][b] == 0)return false;
28 if(visit[a][b] == 1)return true;
29
30 for(int i = 0; i < digits.length; i++){
31 if( ( (1<<i) & a ) == 0)continue;
32
33 int na = a ^ (1<<i);
34 int nb = (17 + b - digits[i]) * 12 % 17;
35 if(na == 0 && digits[i] == 0) continue;
36
37 if( find( na, nb) ){
38 visit[a][b] = 1;
39 return true;
40 }
41 }
42
43 visit[a][b] = 0;
44 return false;
45 }
46
47 void print(int a, int b){
48 if( a == 0)return;
49 for(int i = 0; i < digits.length; i++){
50 if( ( (1<<i) & a ) == 0)continue;
51
52 int na = a ^ (1<<i);
53 int nb = (17 + b - digits[i]) * 12 % 17;
54 if(na == 0 && digits[i] == 0) continue;
55
56 if( find( na, nb) ){
57 print(na, nb);
58 System.out.print(digits[i]);
59 break;
60 }
61 }
62 }
63
64 void work(){
65 if( find(size-1, 0) )
66 {
67 print(size-1, 0);
68 System.out.println();
69 }
70 else
71 System.out.println(-1);
72 }
73 }
74
75 public class Solution{
76 public static void main(String[] args)throws Exception{
77 Permutation P = new Permutation();
78 P.read();
79 P.work();
80 }
81 }
闄勫綍浜岋細Accept浠g爜錛坉fs錛?br>
2 import java.util.Arrays;
3 class Permutation{
4 int []digits;
5 int []ans;
6 static int size = 10;
7
8 void read(){
9 Scanner sc = new Scanner(System.in);
10 String str = sc.next();
11
12 ans = new int[ str.length() ];
13 digits = new int[size];
14 Arrays.fill(digits, 0);
15
16 for(int i = 0; i < str.length(); i++)
17 digits[ str.charAt(i) - '0' ] ++ ;
18 }
19
20 boolean dfs(int len, int res){
21 if(len == ans.length)return res == 0;
22
23 for(int i = 0; i < size; i++){
24 if(i==0 && len == 0)continue;
25 if(digits[i] > 0){
26 digits[i]--;
27
28 ans[len] = i;
29 if(dfs(len+1, (res*10 + i) % 17) )return true;
30
31 digits[i]++;
32 }
33 }
34 return false;
35 }
36
37 void work(){
38 if( dfs(0, 0) ){
39 for(int i = 0; i < ans.length; i++)
40 System.out.print(ans[i]);
41 System.out.println();
42 }
43 else
44 System.out.println(-1);
45 }
46 }
47
48 public class Solution{
49 public static void main(String[] args)throws Exception{
50 Permutation P = new Permutation();
51 P.read();
52 P.work();
53 }
54 }
]]>