CJOJ 2428 打表

打表

Description

有一道比赛题目,输入两个整数x,y(1≤x,y≤n),输出某个函数f(x,y)。有位选手想打表(即事先计算出所有的f(x,y),写在源代码里),但是表太大了,原代码超过了比赛的限制,需要精简。

好在那道题目有一个性质,使得很容易根据f(x,y)算出f(x*k,y*k)(其中k是正整数),这样有一些f(x,y)就不需要存在表里了。输入n(n≤50000),你的任务是统计最简的表里有多少个元素。例如,n=2时有3个(1,1),(1,2),(2,1)。

Input

输入只有一行,一个整数n;

Output

输出也仅有一行,即表里元素的个数。

Sample Input

2

Sample Output

3

Source

欧拉函数

Solution

对于该函数的前一维x,与之配对的可选y(y < x)有phi[x]个,然后根据该函数的对称性随便搞一搞就出来了

Code

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#define L 50010
#define LL long long
using namespace std;

int n, phi[L];
LL ans;

int main(){
  freopen("CJOJ2428.in", "r", stdin);
  freopen("CJOJ2428.out", "w", stdout);
  scanf("%d", &n);
  phi[1] = 1;
  for (int i = 2; i <= n; ++i)
    if (!phi[i])
      for (int j = i; j <= n; j += i) {
	if (!phi[j]) phi[j] = j;
	phi[j] = phi[j] / i * (i - 1);
      }
  for (int i = 2; i <= n; ++i) ans += phi[i];
  printf("%lld\n", ans * 2 + 1);
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值