Fermat’s Chirstmas Theorem
Time Limit: 1000MS Memory limit: 65536K
题目描述
In a letter dated December 25, 1640; the great mathematician Pierre de Fermat wrote to Marin Mersenne that he just proved that an odd prime p is expressible as p = a2 + b2 if and only if p is expressible as p = 4c + 1. As usual, Fermat didn’t include the proof, and as far as we know, never
wrote it down. It wasn’t until 100 years later that no one other than Euler proved this theorem.
To illustrate, each of the following primes can be expressed as the sum of two squares:
5 = 2
2 + 1
2
13 = 3
2 + 2
2
17 = 4
2 + 1
2
41 = 5
2 + 4
2
Whereas the primes 11, 19, 23, and 31 cannot be expressed as a sum of two squares. Write a program to count the number of primes that can be expressed as sum of squares within a given interval.
输入
Your program will be tested on one or more test cases. Each test case is specified on a separate input line that specifies two integers L, U where L ≤ U < 1, 000, 000
The last line of the input file includes a dummy test case with both L = U = −1.
输出
L U x y
where L and U are as specified in the input. x is the total number of primes within the interval [L, U ] (inclusive,) and y is the total number of primes (also within [L, U ]) that can be expressed as a sum of squares.
示例输入
10 20 11 19 100 1000 -1 -1
示例输出
10 20 4 2 11 19 4 2 100 1000 143 69 素数打表,求给定范围内的素数的个数,以及能够表示成两个数的平方和的数的个数。关键在于判断2啊,纠结啊……
/*
遇到素数需要打表时,先估算素数的个数:
num = n / lnx;
num为大概数字,越大误差越小(只是估计,用于估算素数表数组大小)
这个打表法效率貌似很高,网上说几乎达到了线性时间(不知道是真是假=。=)
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<ctime>
#include<algorithm>
using namespace std;
int n,m;
bool visit[1100001];
int prime[1100001];
int num=0;
void init_prim()
{
memset(visit, true, sizeof(visit));
for (int i = 2; i <= 1000000; ++i)
{
if (visit[i] == true)
{
num++;
prime[num] = i;
}
for (int j = 1; ((j <= num) && (i * prime[j] <=1000000)); ++j)
{
visit[i * prime[j]] = false;
if (i % prime[j] == 0) break; //点睛之笔
}
}
}
int main()
{
memset(prime, 0, sizeof(prime));
init_prim();
while(scanf("%d%d",&m,&n))
{
if(m==-1&&n==-1)break;
int count = 0,sum=0;
if(m<=2&&n>=2)
sum++;
for(int i = 0; i <=num; ++i)
if(prime[i]&&prime[i]>=m&&prime[i]<=n)
{
count++;
if((prime[i]-1)%4==0)
sum++;
}
printf("%d %d %d %d\n",m,n,count,sum);
}
return 0;
}
Hide That Number
Time Limit: 1000MS Memory limit: 65536K
题目描述
According to Wikipedia, Cryptography is “the practice and study of hiding information” and this is exactly what Alex is looking for. Ever since he was a kid, Alex was paranoid over someone laying their hands on his phone book. He decided that he must write the numbers in some secret way that only he can decipher. At first he tried quite complex algorithms, but that slowed him down when he needed to dial a number fast. He finally came up with the following algorithm: Rather than writing down the number itself, Alex would shift the number one place to the left (as if multiplying it by 10,) then adding the shifted number to the original. For example, if the phone number was 123, Alex would add 1230 to it, resulting in 1353. To make what he writes looks as a regular phone number, Alex truncates the result (from the left,) so that it has as many digits as the original phone number. In this example, Alex writes 353 instead of 123 in his phone book.Alex needs a program to print the original phone number given what is written in his phone book. Alex, who by the way is a good friend of Johnny, isn’t that good in arithmetic. It is quite possible that the numbers are messed up. The program should print "IMPOSSIBLE" (without the quotes) if the original number cannot be computed.
输入
Your program will be tested on one or more test cases. Each case is specified on a separate line and is made of a single positive number having less than 1, 000, 000 digits. The last line of the input file is made of a single zero.
输出
For each test case, output the result on a single line using the following format:k._resultWhere k is the test case number (starting at 1) and _ is a single space.
示例输入
353 9988 123456 0
示例输出
1. 123 2. IMPOSSIBLE 3. 738496
数字减法。
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; char str[1000005]; char st[1000005]; int main() { int cas=1; while(scanf("%s",str)) { if(strcmp(str,"0")==0)break; int len=strlen(str); int j=0; st[0]=str[len-1]; for(int i=len-2; i>=0; i--) { int flag=0; if(str[i]>=st[j]) st[j+1]=((str[i]-'0')-(st[j]-'0'))+'0'; else { st[j+1]=((str[i]-'0')+10-(st[j]-'0'))+'0'; flag=1; } if(flag) str[i-1]=((str[i-1]-'0')-1)+'0'; j++; } cout<<cas++<<". "; if(st[len-1]=='0') cout<<"IMPOSSIBLE"<<endl; else { for(int i=len-1; i>=0; i--) cout<<st[i]; cout<<endl; } } return 0; }