結果

問題 No.1701 half price
ユーザー harurunharurun
提出日時 2021-02-22 02:26:43
言語 Java21
(openjdk 21)
結果
AC  
実行時間 596 ms / 3,000 ms
コード長 1,243 bytes
コンパイル時間 2,855 ms
コンパイル使用メモリ 75,452 KB
実行使用メモリ 45,060 KB
最終ジャッジ日時 2023-09-30 09:22:44
合計ジャッジ時間 7,474 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 103 ms
39,340 KB
testcase_01 AC 105 ms
39,580 KB
testcase_02 AC 231 ms
44,952 KB
testcase_03 AC 111 ms
39,608 KB
testcase_04 AC 251 ms
45,060 KB
testcase_05 AC 124 ms
40,096 KB
testcase_06 AC 596 ms
44,504 KB
testcase_07 AC 539 ms
44,472 KB
testcase_08 AC 99 ms
39,472 KB
testcase_09 AC 104 ms
39,384 KB
testcase_10 AC 106 ms
39,248 KB
testcase_11 AC 101 ms
39,408 KB
testcase_12 AC 100 ms
39,132 KB
testcase_13 AC 100 ms
39,256 KB
testcase_14 AC 105 ms
39,136 KB
testcase_15 AC 103 ms
39,504 KB
testcase_16 AC 104 ms
39,324 KB
testcase_17 AC 103 ms
39,232 KB
testcase_18 AC 102 ms
39,332 KB
testcase_19 AC 100 ms
38,972 KB
testcase_20 AC 105 ms
39,336 KB
testcase_21 AC 102 ms
39,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashSet;
import java.lang.Math;
import java.lang.String;

class Main {
  public static void main(String[] args) {
    Scanner scan=new Scanner(System.in);
    int N,W,m,n,cnt=0,a;
    N=Integer.parseInt(scan.next());
    W=Integer.parseInt(scan.next());
  	ArrayList<Integer> A=new ArrayList<Integer>();
    for(int i=0;i<N;i++){
      a=Integer.parseInt(scan.next());
      if(a==0)cnt++;
      else A.add(a);
    }
  	if(W==0){
  	  System.out.println((int)Math.pow(2,cnt)-1);
  	  return;
  	}
    n=N-cnt;
    m=(int)Math.pow(3,n);
  	HashSet<Integer> ans=new HashSet<Integer>();
    for(int i=0;i<m;i++){
      long s=0;
      int b=0,x=1;
      String u=change(i,3);
      for(int j=0;j<u.length();j++){
      	if(u.charAt(j)=='1'){
      	  s+=A.get(j);
      	  b+=x;
      	}
      	else if(u.charAt(j)=='2'){
      	  s+=A.get(j)/2;
      	  b+=x;
      	}
      	x*=2;
      }
      if(s==W)ans.add(b);
    }
    System.out.println(ans.size()*(int)Math.pow(2,cnt));
  }
  private static String change(int n,int k){
    StringBuilder sb=new StringBuilder();
    while(n!=0){
      sb.append(String.valueOf(n%k));
      n/=k;
    }
    return sb.toString();
  }
}
0