Created
March 19, 2015 12:08
-
-
Save paralleltree/7d65912554209f5a842e to your computer and use it in GitHub Desktop.
学内プロコン(15/03/19)
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using System.Net; | |
class Client | |
{ | |
readonly string Token = "unique_token"; | |
readonly bool IsReadFromConsole; | |
private WebClient Web { get; set; } | |
private StringReader Stream { get; set; } | |
public Client() | |
{ | |
this.IsReadFromConsole = true; | |
} | |
public Client(string problemUri) | |
{ | |
this.Web = new WebClient(); | |
// initial download | |
Web.DownloadData("http://foo.com/barbar.png"); | |
this.Stream = new StringReader(Web.DownloadString(problemUri)); // break at here. then continue. | |
this.IsReadFromConsole = false; | |
} | |
public string ReadLine() | |
{ | |
if (IsReadFromConsole) return Console.ReadLine(); | |
else return Stream.ReadLine(); | |
} | |
public void Send(string dest, string name, string value) | |
{ | |
var values = new System.Collections.Specialized.NameValueCollection(); | |
values.Add("token", Token); | |
values.Add(name, value); | |
Web.UploadValues(dest, values); | |
} | |
} |
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
class Program | |
{ | |
// ref: https://github.com/OCTPC/miniprocon/blob/master/miniprocon001/regulation.md | |
static void Main(string[] args) | |
{ | |
var client = new Client("http://localhost/problem"); // 入出力 | |
string[] str = client.ReadLine().Split(' '); | |
long x = long.Parse(str[0]); | |
long y = long.Parse(str[1]); | |
// 絶対値 | |
long xx = x < 0 ? -x : x; | |
long yy = y < 0 ? -y : y; | |
var primes = Primes(xx > yy ? xx : yy).ToList(); | |
string ans = ""; | |
// x | |
if (x < 0) | |
{ | |
ans += string.Join("\n", Calc(x, primes).Select(p => (p < 0 ? "R" : "L") + (p < 0 ? -p : p))); | |
} | |
else | |
{ | |
ans += string.Join("\n", Calc(x, primes).Select(p => (p < 0 ? "L" : "R") + (p < 0 ? -p : p))); | |
} | |
ans += "\n"; | |
// y | |
if (y < 0) | |
{ | |
ans += string.Join("\n", Calc(y, primes).Select(p => (p < 0 ? "U" : "D") + (p < 0 ? -p : p))); | |
} | |
else | |
{ | |
ans += string.Join("\n", Calc(y, primes).Select(p => (p < 0 ? "D" : "U") + (p < 0 ? -p : p))); | |
} | |
client.Send("http://localhost/post", "answer", ans); | |
Console.WriteLine(ans); | |
} | |
// 1方向の解の計算 | |
static IEnumerable<long> Calc(long dest, List<long> primes) | |
{ | |
// 絶対値 | |
dest = dest < 0 ? -dest : dest; | |
// 一発で決まるとき | |
if (primes.Contains(dest)) | |
{ | |
yield return dest; | |
yield break; | |
} | |
// あきらめて前方から詰めていく | |
long remaining = dest; | |
while (remaining > 1) | |
{ | |
// 貪欲に埋める | |
long value = primes.TakeWhile(p => p <= remaining).Last(); | |
remaining -= value; | |
yield return value; | |
} | |
if (remaining == 0) | |
{ | |
// ぴったり。めでたしめでたし。 | |
yield break; | |
} | |
else | |
{ | |
while (remaining != 0) | |
{ | |
// 1ずつ進めるんじゃ | |
yield return 3; | |
yield return -2; | |
remaining -= 1; | |
} | |
} | |
} | |
// エラトステネスの篩 | |
static IEnumerable<long> Primes(long max) | |
{ | |
if (max < 2) yield break; | |
long sqrt = (long)Math.Sqrt(max); | |
var arr = new bool[max + 1]; // not prime? | |
for (long i = 2; i <= sqrt; i++) | |
{ | |
if (arr[i]) continue; | |
for (long j = i + i; j < arr.Length; j += i) | |
arr[j] = true; | |
} | |
for (long i = 2; i < arr.Length; i++) | |
if (!arr[i]) yield return i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment