Legal Luiz!
Traduza para Java (use BigInteger) e poste no UVa sua solução para ver se está ok.
Não vou copiar sua solução, afinal, vc acabou fazendo praticamente tudo.
[]'s
Legal Luiz!
Traduza para Java (use BigInteger) e poste no UVa sua solução para ver se está ok.
Não vou copiar sua solução, afinal, vc acabou fazendo praticamente tudo.
[]'s
Nem, vai lá. O problema era seu. Pode fazer em java, se quiser, e colocar lá a resposta.
Detalhe, acho que não precisa usar biginteger se vc fizer por log.
tem um problema que eu to buscando resposta até hoje e não consegui asolução. se quiser me ajudar, se quiser fazer e me mostrar a solução:
problema:
http://projecteuler.net/problem=361
[]'s
Não funcionou não.
Você testou as entradas do problema? Foram geradas as saídas corretas?
<script>
function somatoria(x)
{
//r=s+1
x-=2
t=-5+x;
s=t+1
r=t+2
q=t+3
p=t+4
o=t+5
document.write( (x+2)+" = "+ (
(o*(o+1)*(o+2)*(o+3)*(o+4)/120)*120 +
(p*(p+1)*(p+2)*(p+3)*(p+4)*(p+5)/720)*2480 +
(q*(q+1)*(q+2)*(q+3)*(q+4)*(q+5)*(q+6)/5040)*15280 +
(r*(r+1)*(r+2)*(r+3)*(r+4)*(r+5)*(r+6)*(r+7)/40320)*38720 +
(s*(s+1)*(s+2)*(s+3)*(s+4)*(s+5)*(s+6)*(s+7)*(s+8)/362880)*42800 +
(t*(t+1)*(t+2)*(t+3)*(t+4)*(t+5)*(t+6)*(t+7)*(t+8)*(t+9)/3628800)*17120
)%10007 )
document.write( "<br>" )
document.write( (t*(t+1)*(t+2)*(t+3)*(t+4)*(t+5)*(t+6)*(t+7)*(t+8)*(t+9)/3628800)*17120 ) //<================ possivel truncamento
}
somatoria(1001)
</script>
Não fiz para todos os inputs.
Acho que está ocorrendo um truncamento dos inteiros.
Vou fazer aqui usando log. pra me certificar.
Esse probleminha tá foda.
Eu simplifiquei para log, mas ainda estou tendo o mesmo erro. tenho que estudar isso com maior cuidado.
Se encontrar alguma solução eu posto aqui mais tarde.
Alguem já respondeu esse problema? Não existe possibilidade desse 1001 estar errado?
aqui minha simplificação:
<script>
/*
in out
12 2199
20 803
1001 2390
0
*/
function somatoria(x)
{
var fatoriais =[Math.log(120),Math.log(720),Math.log(5040),Math.log(40320),Math.log(362880),Math.log(3628800)]
var chaves =[Math.log(120),Math.log(2480),Math.log(15280),Math.log(38720),Math.log(42800),Math.log(17120) ]
var arraySums = new Array();
x-=2
t=-5+x;
var prime = Math.log(10007);
arraySums.push( Math.log(t+9)+Math.log(t+8)+Math.log(t+7)+Math.log(t+6)+Math.log(t+5) )
arraySums.push( Math.log(t+4)+arraySums[0] )
arraySums.push( Math.log(t+3)+arraySums[1] )
arraySums.push( Math.log(t+2)+arraySums[2] )
arraySums.push( Math.log(t+1)+arraySums[3] )
arraySums.push( Math.log(t)+arraySums[4] )
var a=arraySums[0]-fatoriais[0]+chaves[0]
a=(Math.exp(a-prime) - Math.floor(Math.exp(a-prime)))*10007
if(a==0)
a=arraySums[0]-fatoriais[0]+chaves[0]
var b=arraySums[1]-fatoriais[1]+chaves[1]
b=(Math.exp(b-prime) - Math.floor(Math.exp(b-prime)))*10007
if(b==0)
b=arraySums[1]-fatoriais[1]+chaves[1]
var c=arraySums[2]-fatoriais[2]+chaves[2]
c=(Math.exp(c-prime) - Math.floor(Math.exp(c-prime)))*10007
if(c==0)
c=arraySums[2]-fatoriais[2]+chaves[2]
var d=arraySums[3]-fatoriais[3]+chaves[3]
d=(Math.exp(d-prime) - Math.floor(Math.exp(d-prime)))*10007
if(d==0)
d=arraySums[3]-fatoriais[3]+chaves[3]
var e=arraySums[4]-fatoriais[4]+chaves[4]
e=(Math.exp(e-prime) - Math.floor(Math.exp(e-prime)))*10007
if(e==0)
e=arraySums[4]-fatoriais[4]+chaves[4]
var f=arraySums[5]-fatoriais[5]+chaves[5]
f=(Math.exp(f-prime) - Math.floor(Math.exp(f-prime)))*10007
if(f==0)
f=arraySums[5]-fatoriais[5]+chaves[5]
document.write( (x+2)+" = "+ Math.round(a +b +c +d +e +f )%10007 )
document.write( "<br>" )
//document.write( (t*(t+1)*(t+2)*(t+3)*(t+4)*(t+5)*(t+6)*(t+7)*(t+8)*(t+9)/3628800)*17120 )
}
for(var i=1; i<=1001; i++)
{
somatoria(i)
}
</script>
Oi Luiz,
No UVa, só 6 pessoas conseguiram responder.
[]'s
Acho que descobri porque não tá dando cert, mas não tenho como testar.
Eu não to com o Java instalado aqui e por isso não tenho como utilizar o BigInteger.
Eu acho que os numeros encontrados são tão grandes que o log não está pegando casas decimais suficientes.
Acho que seria uma boa se vc tentasse fazer ai utilizando biginteger mesmo, como vc estava tentando antes.
No bigInteger existe como pegar o logaritimo?
Se tiver acho que tem como testar. Acha que pode testar ai e me dizer se funcionou para o 1001?
Se não for isso tenho que pedir arrego.
Oi Luiz, valeu pelo empenho!
Estou mexendo com algumas coisas agora e na hora q tiver um tempinho eu vou ver o que faço!
Valeu 
[]'s
Como eu lhe falei o Logaritmo tá sendo truncado e por isso o erro.
Tive que fazer usando BigInteger:
biginteger.js do site http://silentmatt.com/biginteger/
Anexei o arquivo “biginteger.js”
<script type="text/javascript" src="biginteger.js"></script>
<script>
/*
in out
12 2199
20 803
1001 2390
0
*/
var fatoriais = [
BigInteger.parse(["120"],10)
,BigInteger.parse(["720"],10)
,BigInteger.parse(["5040"],10)
,BigInteger.parse(["40320"],10)
,BigInteger.parse(["362880"],10)
,BigInteger.parse(["3628800"],10)
]
var chaves = [
BigInteger.parse(["120"],10)
,BigInteger.parse(["2480"],10)
,BigInteger.parse(["15280"],10)
,BigInteger.parse(["38720"],10)
,BigInteger.parse(["42800"],10)
,BigInteger.parse(["17120"],10)
]
function somatoria(x)
{
x-=2
t=-5+x;
var a0 =BigInteger.parse([""+(t+9)],10)
.multiply(BigInteger.parse([""+(t+8)],10))
.multiply(BigInteger.parse([""+(t+7)],10))
.multiply(BigInteger.parse([""+(t+6)],10))
.multiply(BigInteger.parse([""+(t+5)],10))
var a1 = new BigInteger()
a1=a1.add(a0.multiply(BigInteger.parse([""+(t+4)],10)))
var a2 = new BigInteger()
a2=a2.add(a1.multiply(BigInteger.parse([""+(t+3)],10)))
var a3 = new BigInteger()
a3=a3.add(a2.multiply(BigInteger.parse([""+(t+2)],10)))
var a4 = new BigInteger()
a4=a4.add(a3.multiply(BigInteger.parse([""+(t+1)],10)))
var a5 = new BigInteger()
a5=a5.add(a4.multiply(BigInteger.parse([""+t],10)))
var a=a0.quotient(fatoriais[0]).multiply(chaves[0])
var b=a1.quotient(fatoriais[1]).multiply(chaves[1])
var c=a2.quotient(fatoriais[2]).multiply(chaves[2])
var d=a3.quotient(fatoriais[3]).multiply(chaves[3])
var e=a4.quotient(fatoriais[4]).multiply(chaves[4])
var f=a5.quotient(fatoriais[5]).multiply(chaves[5])
document.write( (x+2)+" = "+ (a.add(b).add(c).add(d).add(e).add(f).remainder(10007).toString(10)) );
document.write( "<br>" )
}
somatoria(1001)
</script>
vc tem razão. Os ciclos aparecem com apenas 2 repetições de numeros quando pegamos o resto de um numero primo.
Se o numero não for primo, poderão existir muitos zeros e muitos numeros repetidos antes de um novo ciclo se iniciar.
Melhorias do codigo.
biblioteca que estou usando:
http://www-cs-students.stanford.edu/~tjw/jsbn/
<script type="text/javascript" src="jsbn.js"></script>
<script type="text/javascript" src="jsbn2.js"/></script>
<script>
var fatorial0 = new BigInteger(""+120,10)
var fatorial1 = new BigInteger(""+720,10)
var fatorial2 = new BigInteger(""+5040,10)
var fatorial3 = new BigInteger(""+40320,10)
var fatorial4 = new BigInteger(""+362880,10)
var fatorial5 = new BigInteger(""+3628800,10)
var chave0 = new BigInteger(""+120,10)
var chave1 = new BigInteger(""+2480,10)
var chave2 = new BigInteger(""+15280,10)
var chave3 = new BigInteger(""+38720,10)
var chave4 = new BigInteger(""+42800,10)
var chave5 = new BigInteger(""+17120,10)
function somatoria(x)
{
t=-7+x;
t%=10007
var a0 =new BigInteger((t+9)+"",10)
.multiply(new BigInteger((t+8)+"",10))
.multiply(new BigInteger((t+7)+"",10))
.multiply(new BigInteger((t+6)+"",10))
.multiply(new BigInteger((t+5)+"",10))
var a1 = a0.multiply(new BigInteger((t+4)+"",10))
var a2 = a1.multiply(new BigInteger((t+3)+"",10))
var a3 = a2.multiply(new BigInteger((t+2)+"",10))
var a4 = a3.multiply(new BigInteger((t+1)+"",10))
var a5 = a4.multiply(new BigInteger(t+"",10))
var b0 = a0.divide(fatorial0).multiply(chave0)
var b1 = a1.divide(fatorial1).multiply(chave1)
var b2 = a2.divide(fatorial2).multiply(chave2)
var b3 = a3.divide(fatorial3).multiply(chave3)
var b4 = a4.divide(fatorial4).multiply(chave4)
var b5 = a5.divide(fatorial5).multiply(chave5)
var sums = b0.add(b1).add(b2).add(b3).add(b4).add(b5);
document.write(x+"-"+ sums.remainder(new BigInteger("10007",10)).toString(10));
document.write( "<br>" )
}
//ciclos para exemplo de demonstracao. Qualquer n.
var n=12;
somatoria(n+10007*0)
somatoria(n+10007*1)
somatoria(n+10007*2)
somatoria(n+10007*3)
somatoria(n+10007*4)
somatoria(n+10007*32)
</script>