結果

問題 No.362 門松ナンバー
ユーザー threepipes_sthreepipes_s
提出日時 2016-04-18 01:49:19
言語 Java21
(openjdk 21)
結果
AC  
実行時間 59 ms / 3,000 ms
コード長 5,273 bytes
コンパイル時間 2,622 ms
コンパイル使用メモリ 87,748 KB
実行使用メモリ 50,932 KB
最終ジャッジ日時 2024-04-15 03:15:32
合計ジャッジ時間 4,443 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,412 KB
testcase_01 AC 53 ms
50,060 KB
testcase_02 AC 52 ms
50,084 KB
testcase_03 AC 53 ms
50,048 KB
testcase_04 AC 51 ms
50,512 KB
testcase_05 AC 55 ms
50,380 KB
testcase_06 AC 56 ms
50,400 KB
testcase_07 AC 53 ms
50,388 KB
testcase_08 AC 54 ms
50,056 KB
testcase_09 AC 55 ms
50,484 KB
testcase_10 AC 56 ms
50,476 KB
testcase_11 AC 56 ms
50,556 KB
testcase_12 AC 58 ms
50,932 KB
testcase_13 AC 59 ms
50,560 KB
testcase_14 AC 58 ms
50,552 KB
testcase_15 AC 57 ms
50,444 KB
testcase_16 AC 58 ms
50,596 KB
testcase_17 AC 56 ms
50,456 KB
testcase_18 AC 59 ms
50,264 KB
testcase_19 AC 55 ms
50,220 KB
testcase_20 AC 57 ms
50,632 KB
testcase_21 AC 51 ms
50,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.BitSet;
import java.util.HashMap;

public class Main {
	public static void main(String[] args) throws NumberFormatException,
	IOException {Main solve = new Main();solve.solve();}
	void dump(int[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");System.out.println();}
	void dump(int[]a,int n){for(int i=0;i<a.length;i++)System.out.printf("%"+n+"d",a[i]);System.out.println();}
	void dump(long[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");System.out.println();}
	void dump(char[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]);System.out.println();}
	long pow(long a,int n){long r=1;while(n>0){if((n&1)==1)r*=a;a*=a;n>>=1;}return r;}
	String itob(int a,int l){return String.format("%"+l+"s",Integer.toBinaryString(a)).replace(' ','0');}
	int binarySearchSmallerMax(int[]a,int v) // get maximum index which a[idx]<=v
	{int l=0,r=a.length-1,s=0;while(l<=r){int m=(l+r)/2;if(a[m]>v)r=m-1;else{l=m+1;s=m;}}return a[s]>v?-1:s;}
	void solve() throws NumberFormatException, IOException{
		final ContestScanner in = new ContestScanner();
		Writer out = new Writer();
		dp = new long[15][10][10];
		dig = new long[15][10];
		for(int i=0; i<10; i++){
			// 先頭の桁が0のときもカウントしていることに注意
			for(int j=0; j<10; j++){
				if(j==i) continue;
				for(int k=0; k<10; k++){
					if(k==j || k==i || !isK(i, j, k)) continue;
					dp[3][i][j]++;
				}
			}
		}
		for(int i=4; i<=14; i++){
			// i桁目について
			for(int x=0; x<10; x++){
				for(int y=0; y<10; y++){
					if(x==y) continue;
					for(int z=0; z<10; z++){
						if(!isK(x, y, z)) continue;
						dp[i][x][y] += dp[i-1][y][z];
					}
				}
			}
		}
		long sum = 0;
		for(int i=3; i<15; i++){
			dig[i][0] = sum;
			for(int j=1; j<10; j++){
				dig[i][j] += sum;
				for(int k=0; k<10; k++){
					dig[i][j] += dp[i][j][k];
				}
				sum += dig[i][j]-sum;
			}
		}
		int T = in.nextInt();
		while(T-->0){
			long k = in.nextLong();
			long left = 0;
			long right = 37294859064823L;
			long ans = 0;
			while(left<=right){
				long mid = (left+right)/2;
				if(getUnder(mid)<k){
					left = mid+1;
				}else{
					ans = mid;
					right = mid-1;
				}
			}
			System.out.println(ans);
		}
	}
	
	int[] sk = new int[20];
	boolean isKN(long value){
		if(value<100) return false;
		int len = 0;
		while(value>0){
			sk[len++] = (int)(value%10);
			value/=10;
		}
		for(int i=0; i<=len-3; i++){
			if(!isK(sk[i], sk[i+1], sk[i+2])) return false;
		}
		return true;
	}
	
	long[][][] dp;
	long[][] dig;
	int[] s = new int[20];
	long getUnder(long value){
		long oldV = value;
		if(value<100) return 0;
		int len = 0;
		while(value>0){
			s[len++] = (int)(value%10);
			value/=10;
		}
		long sum = 0;
		for(int i=len-1; i>=2; i--){
			int d = s[i-1];
			for(int j=0; j<d; j++){
				if(i==len-1 || isK(s[i+1], s[i], j))
					sum += dp[i+1][s[i]][j];
			}
			if(i<len-1 && !isK(s[i+1], s[i], d)) break;
		}
		for(int i=0; i<=s[0]; i++){
			if(isKN(oldV/10*10+i)) sum++;
		}
		return sum+dig[len][s[len-1]-1];
	}
	
	boolean isKPlus(int a0, int a1, int a2){
		return a0<a2 && (a1>a0 && a1>a2 || a1<a0 && a1<a2);
	}
	boolean isK(int a0, int a1, int a2){
		return a0!=a2 && (a1>a0 && a1>a2 || a1<a0 && a1<a2);
	}
}


class MultiSet<T> extends HashMap<T, Integer>{
	@Override
	public Integer get(Object key){return containsKey(key)?super.get(key):0;}
	public void add(T key,int v){put(key,get(key)+v);}
	public void add(T key){put(key,get(key)+1);}
	public void sub(T key)
	{final int v=get(key);if(v==1)remove(key);else put(key,v-1);}
}
class Timer{
	long time;
	public void set(){time=System.currentTimeMillis();}
	public long stop(){return time=System.currentTimeMillis()-time;}
	public void print()
	{System.out.println("Time: "+(System.currentTimeMillis()-time)+"ms");}
	@Override public String toString(){return"Time: "+time+"ms";}
}
class Writer extends PrintWriter{
	public Writer(String filename)throws IOException
	{super(new BufferedWriter(new FileWriter(filename)));}
	public Writer()throws IOException{super(System.out);}
}
class ContestScanner {
	private InputStreamReader in;private int c=-2;
	public ContestScanner()throws IOException 
	{in=new InputStreamReader(System.in);}
	public ContestScanner(String filename)throws IOException
	{in=new InputStreamReader(new FileInputStream(filename));}
	public String nextToken()throws IOException {
		StringBuilder sb=new StringBuilder();
		while((c=in.read())!=-1&&Character.isWhitespace(c));
		while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();}
		return sb.toString();
	}
	public String readLine()throws IOException{
		StringBuilder sb=new StringBuilder();if(c==-2)c=in.read();
		while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();}
		return sb.toString();
	}
	public long nextLong()throws IOException,NumberFormatException
	{return Long.parseLong(nextToken());}
	public int nextInt()throws NumberFormatException,IOException
	{return(int)nextLong();}
	public double nextDouble()throws NumberFormatException,IOException 
	{return Double.parseDouble(nextToken());}
}
0