結果

問題 No.2324 Two Countries within UEC
ユーザー kusagame12kusagame12
提出日時 2023-11-05 21:01:05
言語 Java
(openjdk 23)
結果
MLE  
実行時間 -
コード長 1,987 bytes
コンパイル時間 2,803 ms
コンパイル使用メモリ 79,840 KB
実行使用メモリ 656,628 KB
最終ジャッジ日時 2024-09-25 22:42:11
合計ジャッジ時間 11,849 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 2
other MLE * 1 -- * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        int p = sc.nextInt();
        int q = sc.nextInt();
        
        List<Town> towns = new ArrayList<>();
        for (int i = 1; i <= n ; i++){ 
            Town town = new Town(i);
            for (int j = 1; j <= m ; j++){ 
                town.addConnectTown(j , (i * j) % p);
            }
            towns.add(town);
        }
        
        int[] ans = new int[q];
        for (int i = 0; i < q ; i++){ 
            int x = sc.nextInt();
            int f = sc.nextInt();
            
            int num = mySearch(towns , x);
            //System.out.println(num);
            Map<Integer , List<Integer>> closenessMap = towns.get(mySearch(towns , x)).closenessMap;
            
            if(closenessMap.containsKey(f)){
                ans[i] = closenessMap.get(f).size();
            }
            
        }
        
        for(int i = 0 ; i < ans.length ; i++){
            System.out.println(ans[i]);
        }
        
    }
    
    private static int mySearch(List<Town> list, int target) {
    	for(int i = 0; i < list.size(); i++) {
    		if (list.get(i).num == target) {
    		    return i;
    		}
    	}
    	return -1;
    }
}

class Town{
    
    int num;
    Map<Integer , List<Integer>> closenessMap = new HashMap<>();
    
    public Town(int num){
        this.num = num;
    }
    
    public void addConnectTown(int connectTown , int closeness){
        if(!closenessMap.containsKey(closeness)){
            List<Integer> list = new ArrayList<>();
            list.add(connectTown);
            closenessMap.put(closeness , list);
        }else{
            List<Integer> list = closenessMap.get(closeness);
            list.add(connectTown);
            closenessMap.put(closeness , list);
        }
    }
    
}
0