您的位置:首页 > 其它

uva10820(简单数学题)

2015-06-12 19:19 274 查看
题目的大概意思是有两个变量x,y,然后x,y都是小于n的,有一个函数f(x,y),另外f(x*k,y*k) = f(x,y),问f(x,y)在x,y都不超过n的时候的取值个数,很明显的就是记下来的想x,y必定是互素的,当然有一个另外就是1,1。

求解,很明显的就是转化为欧拉phi函数问题,求出1-n中与n互素的个数,然后x,y可以互换位置,求得的结果要乘以2。

#include "stdio.h"
#include "string.h"
#include "math.h"
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
using namespace std;

#define MAXM 1
#define MAXN 1
#define max(a,b) a > b ? a : b
#define min(a,b) a < b ? a : b
#define Mem(a,b) memset(a,b,sizeof(a))
int Mod = 1000000007;
double pi = acos(-1.0);
double eps = 1e-6;

typedef struct{
int f,t,w,next;
}Edge;
Edge edge[MAXM];
int head[MAXN];
int kNum;

typedef long long LL;

void addEdge(int f, int t, int w)
{
edge[kNum].f = f;
edge[kNum].t = t;
edge[kNum].w = w;
edge[kNum].next = head[f];
head[f] = kNum ++;
}
LL phi[50005];
int N;

void func(){
Mem(phi, 0);
phi[1] = 1;
for(int i = 2; i <= 50000; i ++){
if( !phi[i] ){
//			phi[i] = i;
for(int j = i; j <= 50000; j += i){
if( !phi[j] ) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
}
for(int i = 2; i <= 50000; i ++){
phi[i] = 2 * phi[i] + phi[i-1];
}
}

int main()
{
//	freopen("d:\\test.txt", "r", stdin);
func();
while(cin>>N){
if( N < 1 )
break;
cout<<phi
<<endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: