Created
April 26, 2016 08:00
-
-
Save PreSoichiSumi/9cf65c6802563736ad18e93a9f1cefe6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigDecimal; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Scanner; | |
import java.util.Set; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc=new Scanner(System.in); | |
int N=sc.nextInt(); | |
int a=sc.nextInt(); | |
String str=sc.next(); | |
BigDecimal k =new BigDecimal("+"+str); | |
List<Integer> dictionally=new ArrayList<Integer>(); | |
int memo[]=new int[100001]; | |
Set<Integer> visited=new HashSet<>(); | |
for(int i=0;i<N;i++) | |
dictionally.add(sc.nextInt()); | |
// BigDecimal count=new BigDecimal(0); | |
while(!k.equals(BigDecimal.ZERO)){ | |
k=k.subtract(BigDecimal.ONE); | |
if(visited.add(a)){ | |
for (Integer integer : visited) { | |
memo[integer.intValue()-1]++; | |
} | |
a=dictionally.get(a-1); | |
}else{ | |
BigDecimal memoVal=new BigDecimal(memo[a-1]); | |
if(k.compareTo(memoVal)>=0){//剰余が出るなら | |
k=k.remainder(new BigDecimal(memo[a-1])); | |
} | |
a=dictionally.get(a-1); | |
break; | |
} | |
} | |
while(!k.equals(BigDecimal.ZERO)){ | |
a=dictionally.get(a-1); | |
k=k.subtract(BigDecimal.ONE); | |
} | |
System.out.println(a); | |
sc.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment