Project Euler 48-Self powers

Self powers

Problem 48

The series, 11 + 22 + 33 + ... + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + ... + 10001000.

 

Answer:
9110846700
Completed on Fri, 5 Apr 2013, 12:33

Go to the thread for problem 48 in the forum.

 

ans=0
i=1
F=10**10
for i in range(1,1001):
p=1
for j in range(i): p=p*i%F
ans=(ans+p)%F
print i
print ans

Project Euler 3-Largest prime factor

Largest prime factor

Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

 

Answer:
6857
Completed on Fri, 5 Apr 2013, 12:14

Go to the thread for problem 3 in the forum.

 

import math
n=600851475143
i=2
print math.sqrt(n)
while i< =math.sqrt(n): if (n%i==0): while (n%i==0): n=n/i print i; i=i+1 print n

Project Euler 2-Even Fibonacci numbers

Even Fibonacci numbers

Problem 2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Answer:
4613732
Completed on Fri, 5 Apr 2013, 06:37

Go to the thread for problem 2 in the forum.

 Even(是偶数的意思……(─.─|||

f=[1,1,1]
i=2
ans=0
while f[i]+f[i-1]<4000000:
i=i+1
f.append(f[i-1]+f[i-2])
if (f[i]%2==0): ans=ans+f[i]
print sum(f)-1,f,ans

UVA 11538(Chess Queen-矩阵对角线长度)

Problem A
Chess Queen
Input:
Standard Input

Output: Standard Output

 

王后互相攻击的前提是在一行,一列,或一对角线。:

 

在 (NxM) 的棋盘上 2 王后互相攻击,求方案数.

 

Input

 

输入数据不超过 5000 行 ,每行为M and N (0< M, N£106) ,数据以0 0结尾.

 

Output

 

每行一个整数表示方案数,保证它在u64范围内.

 

Sample Input                              Output for Sample Input

2 2

100 223

2300 1000

0 0

12

10907100

11514134000                                                                        

 

Problemsetter: Shahriar Manzoor

Special Thanks to: Mohammad Mahmudur Rahman

首先,一个矩形的长宽若为m,n(m>=n)

那么它一个方向的对角线应为1..(n-1)各2条,n有(m-n+1)条

知道这个的化,本题就转化为,在一列一行或一对角线任取2点,有几种取法。

#include<cstdio>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
#define MAXN (1000000+10)
unsigned long long n,m;
int main()
{
	while (cin>>n>>m&&n&&m)
	{
		if (n>m) swap(n,m);
		cout<<n*m*(n+m-2)+2*n*(n-1)*(3*m-n-1)/3<<endl;
	}
	return 0;
}