UVALIVE 7362(欧拉函数)

来源:互联网 发布:网络维修工具包 编辑:程序博客网 时间:2024/05/17 04:19

题目链接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5384

题解:

欧拉函数记录所有数据范围内的值,然后加一起就可以了。

AC代码:

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;int t, number, n;long long int ans;int euler(int x){    int i, res=x;    for (i = 2; i < (int)sqrt(x * 1.0) + 1; i++)        if(x%i==0)        {            res = res / i * (i - 1);            while (x % i == 0)                x /= i; // 保证i一定是素数        }    if (x > 1)        res = res / x * (x - 1);    return res;}int hasaki[10001];int main(){    cin >> t;    for(int i = 1; i <= 10000; i++)    {        hasaki[i] = euler(i);    }    while(t--)    {        scanf("%d%d", &number, &n);        ans = 0;        for(int i = 1; i <= n; i++)            ans += hasaki[i];        cout << number << " " <<ans + 1<<endl;    }    return 0;}
0 0