Skip to content

Instantly share code, notes, and snippets.

@MeilCli
Created October 19, 2014 13:43
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 MeilCli/88bf12640af55a703d24 to your computer and use it in GitHub Desktop.
Save MeilCli/88bf12640af55a703d24 to your computer and use it in GitHub Desktop.
ゴリ押し
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AjinoriNoTyousen {
public class Program {
//Q. 正の整数であって、その正の約数のうち4で割った余りが2でないようなものの総和が1000であるものをすべて求めよ.
public static void Main(string[] args) {
Func<int,int> sum = x => {
int s = 0;
for(int i = 1;i <= x;i = i + 4) {
s = s + (x % i == 0 ? i : 0);
s = s + ((i + 2) <= x && x % (i + 2) == 0 ? (i + 2) : 0);
s = s + ((i + 3) <= x && x % (i + 3) == 0 ? (i + 3) : 0);
}
return s;
};
Func<int,int[]> split = x => {
var list = new List<int>();
for(int i = 1;i <= x;i++) {
if(i % 2 == 0 && i % 4 != 00) {
continue;
}
if(x % i != 0) {
continue;
}
list.Add(i);
}
return list.ToArray();
};
//検索範囲は[条件に合う]1+(約数)+n=1000のnまで
//ここでは簡易的に1+1000>1000である1000に設定
for(int n = 1;n <= 1000;n++) {
int sn = sum(n);
if(sn == 1000) {
Console.Out.Write(n);
Console.Out.Write(" : ");
Console.Out.WriteLine(string.Join("+",split(n)));
}
}
}
}
}
448 : 1+4+7+8+16+28+32+56+64+112+224+448
796 : 1+4+199+796
続行するには何かキーを押してください . . .
@MeilCli
Copy link
Author

MeilCli commented Oct 19, 2014

100000までの探索でも同じ解

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment