Last active
August 29, 2015 14:05
-
-
Save kuuso/1070b52acab7c423c5d4 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
using System; | |
using System.IO; | |
using System.Collections; | |
using System.Collections.Generic; | |
class TEST{ | |
static void Main(){ | |
String inputFile="./data.txt"; | |
StreamReader sr=new StreamReader(inputFile); | |
//Stream stdin=Console.OpenStandardInput(); | |
//StreamReader SR=new StreamReader(stdin); | |
String s=""; | |
List<String> L=new List<String>(); | |
while(!String.IsNullOrEmpty(s=sr.ReadLine())){ | |
var ss=s.Split(' '); | |
Sol mySol =new Sol(ss); | |
bool chk=mySol.Solve(); | |
if(!chk)L.Add(ss[0]); | |
} | |
Console.WriteLine(""); | |
Console.WriteLine("Answer--result does not match with expectation -Total {0}",L.Count); | |
for(int i=0;i<L.Count;i++){ | |
Console.Write(i==0?"{0}":",{0}",L[i]); | |
} | |
Console.WriteLine(""); | |
} | |
} | |
class Sol{ | |
public bool Solve(){ | |
//周期periodicを計算して、各部分列の公差をlong[periodic]で返す。 | |
long[] diffs=FindPeriodic(); | |
//FromからToの和を求める | |
long sum=CalSum(diffs); | |
return sum==expect; | |
} | |
long[] FindPeriodic(){ | |
int p=1; | |
while(2*p<data.Length){ | |
//周期pで等差数列をなしているかをチェックする。 | |
bool found=true; | |
for(int j=0;j<p;j++){ | |
for(int i=j;i+2*p<data.Length;i+=p){ | |
if(data[i]+data[i+2*p]!=data[i+p]*2){ | |
found=false; | |
} | |
} | |
} | |
if(found)break; | |
p++; | |
} | |
long[] ret=new long[p]; | |
for(int i=0;i<p;i++){ | |
ret[i]=data[i+p]-data[i]; | |
} | |
return ret; | |
} | |
long CalSum(long[] diffs){ | |
int mod=diffs.Length; | |
long sum=0; | |
for(int i=0;i<mod;i++){ | |
long head=from; | |
while(head%mod!=i)head++; | |
head/=mod; | |
long tail=to; | |
while(tail%mod!=i)tail--; | |
tail/=mod; | |
long num=tail-head+1; | |
sum+=(data[i]+head*diffs[i] + data[i]+tail*diffs[i])*num/2; | |
} | |
return sum; | |
} | |
String idx; | |
long[] data; | |
long from; | |
long to; | |
long expect; | |
public Sol(String[] ss_){ | |
//コンストラクタ:入力データはフィールドで保持。 | |
//添え字は 0-originに修正 | |
idx=ss_[0]; | |
data=Array.ConvertAll(ss_[1].Split(','),e=>long.Parse(e)); | |
from=long.Parse(ss_[2])-1; | |
to=long.Parse(ss_[3])-1; | |
expect=long.Parse(ss_[4]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment