这数论好多“套路”,一个一个的让我不知所措,很迷!!!
发题之前先了解了解欧拉函数,就是1-n中与n互质的数的个数(gcd(i,n)==1)
两种求解欧拉函数的代码见连接内。
具体到题目之中。
Given the value of N, you will have to find the value of G. The definition of G is given below:
|
Here GCD(i,j) means the greatest common divisor of integer i and integer j.
For those who have trouble understanding summation notation, the meaning of G is given in the following code:
G=0; for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } /*Here gcd() is a function that finds the greatest common divisor of the two input numbers*/ |
Input
The input file contains at most 100 lines of inputs. Each line contains an integer N (1<N<4000001). The meaning of N is given in the problem statement. Input is terminated by a line containing a single zero.
Output
For each line of input produce one line of output. This line contains the value of G for the corresponding N. The value of G will fit in a 64-bit signed integer.
Sample Input Output for Sample Input
10 100 200000 0
| 67 13015 143295493160
|
题意:题目中代码的意思
思路:开始直接按照题目中写,事大了,200000跑不出来,O(n^2log(n)),硬来肯定不行
以10为例,与之互素的有φ(10)=4个(1,3,7,9),与之gcd=2的有φ(10/2)=4个(2,4,6,8),与之gcd=5的有φ(10/5)=1个(5)
10的目标值就是5*φ(2)+2*φ(5)+φ(10)=5+8+4=17
有了这个基础,代码就好理解了,线性求解欧拉函数
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll long long
#define N 4000001
ll p[N];
ll ans[N];
void solo()
{
for(int i=2;i<N;i++)
p[i]=i;
for(int i=2;i<N;i++)
{
if (p[i]==i)
for(int j=i;j<N;j+=i)
p[j]=p[j]/i*(i-1);
for(int j=i;j<N;j+=i)
ans[j]+=j/i*p[i];
}
for (int i=3;i<N;i++)
ans[i]+=ans[i-1];
}
int main()
{
solo();
int n;
while (scanf("%d",&n)!=EOF)
{
if(n==0)break;
printf("%lld\n",ans[n]);
}
return 0;
}
Description
Computer generated and assisted proofs and verification occupy a small niche in the realm of Computer Science. The first proof of the four-color problem was completed with the assistance of a computer program and current efforts in verification have succeeded in verifying the translation of high-level code down to the chip level.
This problem deals with computing quantities relating to part of Fermat's Last Theorem: that there are no integer solutions of a^n + b^n = c^n for n > 2.
Given a positive integer N, you are to write a program that computes two quantities regarding the solution of x^2 + y^2 = z^2, where x, y, and z are constrained to be positive integers less than or equal to N. You are to compute the number of triples (x,y,z) such that x < y < z, and they are relatively prime, i.e., have no common divisor larger than 1. You are also to compute the number of values 0 < p <= N such that p is not part of any triple (not just relatively prime triples).
Input
The input consists of a sequence of positive integers, one per line. Each integer in the input file will be less than or equal to 1,000,000. Input is terminated by end-of-file
Output
For each integer N in the input file print two integers separated by a space. The first integer is the number of relatively prime triples (such that each component of the triple is <=N). The second number is the number of positive integers <=N that are not part of any triple whose components are all <=N. There should be one output line for each input line.
Sample Input
10
25
100
Sample Output
1 4
4 9
16 27
题意:求出<n的本原勾股定理abc,两两互质,在输出与这些数(倍数)无关的数的个数
思路:公式推导
最终结果:a=st b=(s*s-t*t)/2 c=(s*s+t*t)/2
简写过程,首先a,b一奇一偶,所以c是奇数 a^2=(c-b)(c+b)
只需要证名证明gcd(c-b,c+b)=1也就是他们互质,利用尚士子可得s^2=c+b t^2=c-b
假设gcd(c-b,c+b)=d,所以k*d=c-b+c+b=2*c,k是随意假设的,同理k2*d=c+b-c+b=2*b,而且k3*d=(c-b)(c+b)=a^2
因为bc互质,所以d是1或者2,又因为存在k3,所以d是奇数1
下面就是解方程了
import java.util.Scanner;
public class Main {
//public static
public static long gcd(long a,long b)
{
if(b==0)return a;
return gcd(b,a%b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner cin=new Scanner(System.in);
//for(int i=1;i<10;i++)
//System.out.println(gcd(18,27));
while(cin.hasNext())
{
long n=cin.nextLong();
//System.out.println(n);
int []vis=new int[1000010];
if(n==0)
break;
long ans=0;
long a,b,c;
for(long i=1;;i+=2)
{
//System.out.println(i);
if((i*i+(i+2)*(i+2))/2>n)
break;
for(long j=i+2;;j+=2)
{
if(gcd(i,j)==1)
{
c=(i*i+j*j)/2;
b=(j*j-i*i)/2;
a=i*j;
if(c>n)
break;
ans++;
for(long k=1;k*c<=n;k++)
{
vis[(int) (a*k)]=1;
vis[(int) (b*k)]=1;
vis[(int) (c*k)]=1;
}
}
}
}
int num=0;
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
num++;
}
System.out.println(ans+" "+num);
}
}
}