Skip to content

Instantly share code, notes, and snippets.

@taross-f
Created January 6, 2017 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taross-f/4f07a6b351598a7e37f22452313376a6 to your computer and use it in GitHub Desktop.
Save taross-f/4f07a6b351598a7e37f22452313376a6 to your computer and use it in GitHub Desktop.
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Net.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\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