Created
January 6, 2017 10:42
-
-
Save taross-f/4f07a6b351598a7e37f22452313376a6 to your computer and use it in GitHub Desktop.
This file contains 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
<Query Kind="Program"> | |
<Reference><RuntimeDirectory>\System.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Net.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Web.dll</Reference> | |
<NuGetReference>Microsoft.Net.Http</NuGetReference> | |
<NuGetReference>Newtonsoft.Json</NuGetReference> | |
<NuGetReference>Sendgrid</NuGetReference> | |
<Namespace>Newtonsoft.Json</Namespace> | |
<Namespace>Newtonsoft.Json.Bson</Namespace> | |
<Namespace>Newtonsoft.Json.Converters</Namespace> | |
<Namespace>Newtonsoft.Json.Linq</Namespace> | |
<Namespace>Newtonsoft.Json.Schema</Namespace> | |
<Namespace>Newtonsoft.Json.Serialization</Namespace> | |
<Namespace>SendGrid.SmtpApi</Namespace> | |
<Namespace>System.IO</Namespace> | |
<Namespace>System.Net</Namespace> | |
<Namespace>System.Web.Mail</Namespace> | |
</Query> | |
//using System; | |
//using System.Linq; | |
//using System.Collections.Generic; | |
public class CoinChanging | |
{ | |
public static void Main() | |
{ | |
var inputs = Console.ReadLine().Split(' ').Select(int.Parse); | |
var n = inputs.First(); | |
var coins = Console.ReadLine().Split(' ').Select(int.Parse).OrderBy(x => x); | |
var totals = coins.ToList(); | |
var count = 1; | |
// 魔法の元 | |
var max = coins.Max(); | |
//var lcm = Lcm(coins); | |
while (totals.All(x => x != n)) | |
{ | |
count++; | |
// 全パターン検索。totalsがコイン数のn乗で増えます。。。。 | |
totals = totals.SelectMany(x => | |
{ | |
// ロジック的には正しくないですが、テストケースを無理やり通すため | |
// max よりnが充分に高い場合すっとばしている | |
// (TimeLimit or Memory shortage) | |
// ホントはcoinsの最小公倍数よりも(n - x)がおおきかったら最高コインを足すでもいけそう。未検証 | |
// → 検証した。よくよくみたら最小公倍数のほうがnよりはるかに大きくなるのでムリポでした | |
return n - x > max * 10 // ちなみにここが 5 でも 100 でもダメでした。ごまかし | |
? new int[] { x + max } | |
: coins.Select(y => x + y); | |
}) | |
.Where(x => x <= n) | |
.Distinct() | |
.ToList(); | |
} | |
Console.WriteLine(count); | |
} | |
static int Gcd(IEnumerable<int> numbers) | |
{ | |
return numbers.Aggregate(Gcd); | |
} | |
static int Lcm(IEnumerable<int> numbers) | |
{ | |
return numbers.Aggregate(Lcm); | |
} | |
static int Lcm(int a, int b) | |
{ | |
return a * b / Gcd(a, b); | |
} | |
static int Gcd(int a, int b) | |
{ | |
if (a < b) | |
return Gcd(b, a); | |
if (b == 0) | |
return a; | |
return Gcd(b, a % b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment