Programming Challenge -PKU-
http://w.atwiki.jp/azounoman/
Programming Challenge -PKU-ja2006-05-14T01:01:00+09:0011475360602800 Joseph's Problem
https://w.atwiki.jp/azounoman/pages/99.html
**2800 Joseph's Problem
**解答例
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
long n = sc.nextInt();
long k = sc.nextInt();
long r = 0;
/*
for(int i=1;i<=n;i++) r += k%i;
System.out.println(r);
*/
long n0 = (long)Math.sqrt(n);
for(int i=1;i<=n0;i++) r += k%i;
// n0 == n
if(n0==n){
System.out.println(r);
return;
}
// n0 != n
long c1, c2, r1, r2, q;
// k = c1 * q + r1
// k = c2 * q + r2
c1 = n0 + 1;
q = k / c1;
while(true){
if(q==0) c2 = n;
else c2 = Math.min(k/q, n);
r1 = k - c1 * q;
r2 = k - c2 * q;
r += (r1 + r2) * (c2 - c1 + 1) / 2;
if(c2= 2006-05-14T01:01:00+09:0011475360602782 Bin Packing
https://w.atwiki.jp/azounoman/pages/98.html
**2782 Bin Packing
**解答例
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int b = sc.nextInt();
int[] ls = new int[n];
for(int i=0;i<n;i++) ls[i] = sc.nextInt();
Arrays.sort(ls);
int inf = 0;
int sup = n-1;
int cnt = 0;
while(true){
cnt++;
int x = ls[sup];
sup--;
if(inf>sup) break;
int y = ls[inf];
if(x+y<=b) inf++;
if(inf>sup) break;
}
System.out.println(cnt);
}
} 2006-05-14T00:59:46+09:0011475359862769 Reduced ID Numbers
https://w.atwiki.jp/azounoman/pages/97.html
**2769 Reduced ID Numbers
**解答例
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
int g = sc.nextInt();
int a[] = new int[g];
for(int j=0;j<g;j++){
a[j] = sc.nextInt();
}
Arrays.sort(a);
boolean[] ans = new boolean[1000000];
Arrays.fill(ans, true);
ans[0] = false;
if(g==1) System.out.println(1);
else{
for(int k=0;k<g;k++){
for(int l = k+1;l<g;l++){
int x = a[l] - a[k];
if(ans[x]==false) continue;
for(int m=1;m*m<=x;m++){
if(x%m==0){
ans[ 2006-05-14T00:58:23+09:0011475359032739 Sum of Consecutive Prime Numbers
https://w.atwiki.jp/azounoman/pages/96.html
**2739 Sum of Consecutive Prime Numbers
**解答例
import java.util.*;
public class Main {
static final int SIZE = 2000;
static int[] primetable;
public static void main(String[] args) {
//make prime number table
makePrimeTable();
//scanner
Scanner sc = new Scanner(System.in);
//solve
int x;
while((x = sc.nextInt())!=0){
int ans = 0;
for(int i=0;i<SIZE;i++){
int sum = 0;
for(int j=i;j<SIZE;j++){
sum += primetable[j];
if(x==sum){
ans++;
break;
}
if(x<sum) break;
}
}
System.out.println(ans);
}
}
private static void makePrimeTable(){
primetable = new int[SIZE];
int i = 0;
int x = 2 2006-05-14T00:56:26+09:0011475357862688 Cleaning Robot
https://w.atwiki.jp/azounoman/pages/95.html
**2688 Cleaning Robot
**解答例
import java.util.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(true){
int w = sc.nextInt();
int h = sc.nextInt();
if(w==0&&h==0) break;
char[][] floor = new char[h][w];
for(int i=0;i<h;i++){
String s = sc.next();
for(int j=0;j<w;j++){
floor[i][j] = s.charAt(j);
}
}
Solver sol = new Solver(w, h, floor);
System.out.println(sol.solve());
}
}
}
class Solver{
int w;
int h;
char[][] floor;
int size; // 1 + num of dirty tile
Point loc[];
int[][] dist;
int max;
public Solver(int w, int h, char[][] floo 2006-05-14T00:54:36+09:0011475356762686 Traveling by Stagecoach
https://w.atwiki.jp/azounoman/pages/94.html
**2686 Traveling by Stagecoach
**解答例
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
int n = sc.nextInt();
int m = sc.nextInt();
int p = sc.nextInt();
int a = sc.nextInt()-1;
int b = sc.nextInt()-1;
if(n==0 && m==0 && p==0) break;
int[] t = new int[n];
for(int i=0;i<n;i++) t[i] = sc.nextInt();
double[][] path = new double[m][m];
for(int i=0;i<m;i++) Arrays.fill(path[i], Double.MAX_VALUE);
for(int i=0;i<p;i++){
int x = sc.nextInt()-1;
int y = sc.nextInt()-1;
double z = sc.nextDouble();
path[x][y] = z;
path[y][x] = z;
}
Solver sol = new Solver(n, m, a, b, t, path);
double ans = 2006-05-14T00:52:05+09:0011475355252685 Numeral System
https://w.atwiki.jp/azounoman/pages/93.html
**2685 Numeral System
**解答例
import java.util.*;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
String s1 = sc.next();
String s2 = sc.next();
print_mcxi(mcxi_to_int(s1) + mcxi_to_int(s2));
}
}
private static int mcxi_to_int(String s){
int ret = 0;
int index = 0;
int prefix;
char unit;
while(index<s.length()){
char c = s.charAt(index);
if('2'<=c&&c<='9'){
prefix = c - '0';
index++;
c = s.charAt(index);
}
else{
prefix = 1;
}
unit = c;
switch(unit){
case 'm': 2006-05-14T00:50:08+09:0011475354082684 Polygonal Line Search
https://w.atwiki.jp/azounoman/pages/92.html
**2684 Polygonal Line Search
**解答例
import java.util.*;
public class Main {
private static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
int n;
while((n=sc.nextInt())>0){
int m0 = sc.nextInt();
PolyLine tmp = new PolyLine(m0, readPoints(m0));
PolyLine rtmp = tmp.reverse();
int[] tmpcode = tmp.code();
int[] rtmpcode = rtmp.code();
for(int i=1;i<=n;i++){
int m = sc.nextInt();
PolyLine pl = new PolyLine(m, readPoints(m));
if(m!=m0) continue;
int[] plcode = pl.code();
if(PolyLine.codeEquals(tmpcode, plcode) ||
PolyLine.codeEquals(rtmpcode, plcode)){
System.out.println(i);
}
}
System.out.println("+++++");
}
}
private stati 2006-05-14T00:47:58+09:0011475352782683 Ohgas' Fortune
https://w.atwiki.jp/azounoman/pages/91.html
**2683 Ohgas' Fortune
**解答例
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
for(int i=0;i<m;i++){
int f = sc.nextInt();
int y = sc.nextInt();
int n = sc.nextInt();
int max = Integer.MIN_VALUE;
for(int j=0;j<n;j++){
int mode = sc.nextInt();
double r = sc.nextDouble();
int c = sc.nextInt();
if(mode==0) max = Math.max(max, single(f, y, r, c));
else max = Math.max(max, compound(f, y, r, c));
}
System.out.println(max);
}
}
private static int single(int a, int year, double ratio, int charge){
int interest = 0;
for(int i=0;i<year;i++){
interest += a * ratio;
a - 2006-05-14T00:46:20+09:0011475351802640 Playground
https://w.atwiki.jp/azounoman/pages/90.html
**2640 Playground
**解答例
import java.util.*;
public class Main {
static final double EPSILON = 1.0E-6;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(true){
int k = sc.nextInt();
if(k==0) break;
double a[] = new double[k];
for(int i=0;i<k;i++) a[i] = sc.nextDouble();
Arrays.sort(a);
if(k==1){
System.out.println("NO");
continue;
}
boolean end = false;
for(int i=1;i<k;i++){
double sum = 0;
for(int j=0;j<i;j++) sum += a[j];
if(sum - a[i] > -EPSILON){
System.out.println("YES");
end = true;
break;
}
if(end) break;
2006-05-14T00:43:11+09:001147534991