http://lightoj.com/volume_showproblem.php?problem=1336

Sigma Function

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

Sigma function is an interesting function in Number Theory. It is denoted by the Greek letter Sigma (σ). This function actually denotes the sum of all divisors of a number. For example σ(24) = 1+2+3+4+6+8+12+24=60. Sigma of small numbers is easy to find but for large numbers it is very difficult to find in a straight forward way. But mathematicians have discovered a formula to find sigma. If the prime power decomposition of an integer is

Then we can write,

For some n the value of σ(n) is odd and for others it is even. Given a value n, you will have to find how many integers from 1 to n have even value of σ.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 1012).

Output

For each case, print the case number and the result.

Sample Input

4

3

10

100

1000

Sample Output

Case 1: 1

Case 2: 5

Case 3: 83

Case 4: 947

题目大意:给你一个数n,让你求从1到n中因子和为偶数的数共有多少个,可以用唯一分定理的公式

来找(我这样写过,不过超时)

那么我们可以通过超时的代码将100中不满足的数打出来,如下:

2 3

1 1
4 7
8 15
9 13
16 31
18 39
25 31
32 63
36 91
49 57
50 93
64 127
72 195
81 121
98 171
100 217

发现100中,因子和为奇数的有:

1 2 4 6 9 16 18 25 32 36 49 50 64 72 81 98 100

有没有发现,这些数字有一个规律,他们是 x^2, 2*x, 2*x^2, 只要100中的数满足这三个中的一个,那么,这个数就是不满足的,

总数-不满足的个数 = 满足的个数

我们还可以发现:当x为偶数时2*x和x^2会有重复的部分

当x为奇数时2*x和2*x^2会有重复的部分

那么我们可以将2*x省去,我们求求出x^2的个数和2*x^2的个数,然后用总数减去它们的个数即可

AC代码:

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<algorithm>
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9. const int N = 1e6 + ;
  10.  
  11. int main()
  12. {
  13. int t, p = ;
  14. ll n;
  15. scanf("%d", &t);
  16. while(t--)
  17. {
  18. p++;
  19. ll x, y;
  20. scanf("%lld", &n);
  21. x = (ll)sqrt(n);//计算n中x^2的个数
  22. y = (ll)sqrt(1.0 * n / );//计算n中2*x^2的个数
  23. printf("Case %d: %lld\n", p, n - x - y);
  24. }
  25. return ;
  26. }

TLE代码(可以用来打表找规律):

  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<string.h>
  4. #include<stdlib.h>
  5. #include<algorithm>
  6.  
  7. using namespace std;
  8. typedef long long ll;
  9. const int N = 1e6 + ;
  10.  
  11. int prime[N], k;
  12. bool Isprime[N];
  13.  
  14. void Prime()
  15. {
  16. k = ;
  17. memset(Isprime, true, sizeof(Isprime));
  18. Isprime[] = false;
  19. for(int i = ; i < N ; i++)
  20. {
  21. if(Isprime[i])
  22. {
  23. prime[k++] = i;
  24. for(int j = ; j * i < N ; j++)
  25. Isprime[j * i] = false;
  26. }
  27. }
  28. }
  29.  
  30. ll solve(ll n)
  31. {
  32. ll x, y, a;
  33. ll num = ;
  34. for(ll i = ; i < k && prime[i] * prime[i] <= n ; i++)
  35. {
  36. x = ;
  37. if(n % prime[i] == )
  38. {
  39. while(n % prime[i] == )
  40. {
  41. x++;
  42. n /= prime[i];
  43. }
  44. x += ;
  45. a = ;
  46. for(ll j = ; j <= x ; j++)
  47. a *= prime[i];
  48. y = a - ;
  49. num *= y /(prime[i] - );
  50. }
  51. }
  52. if(n > )
  53. num *= ((n * n - ) / (n - ));
  54. return num;
  55. }
  56.  
  57. int main()
  58. {
  59. Prime();
  60. int t, x = ;
  61. ll n;
  62. scanf("%d", &t);
  63. while(t--)
  64. {
  65. x++;
  66. scanf("%lld", &n);
  67. ll num = ;
  68. for(ll i = ; i <= n ; i++)
  69. {
  70. ll m = solve(i);
  71. if(m % != )
  72. {
  73. printf("%lld %lld\n", i, m);
  74. num++;
  75. }
  76. }
  77. printf("Case %d: %lld\n", x, n - - num);
  78. }
  79. return ;
  80. }
 

LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)的更多相关文章

  1. LightOJ - 1336 - Sigma Function(质数分解)

    链接: https://vjudge.net/problem/LightOJ-1336 题意: Sigma function is an interesting function in Number ...

  2. LightOJ 1245 数学题,找规律

    1.LightOJ 1245   Harmonic Number (II)   数学题 2.总结:看了题解,很严谨,但又确实恶心的题 题意:求n/1+n/2+....+n/n,n<=2^31. ...

  3. LightOJ 1336 Sigma Function 算数基本定理

    题目大意:f(n)为n的因子和,给出 n 求 1~n 中f(n)为偶数的个数. 题目思路:算数基本定理: n=p1^e1*p2^e1 …… pn^en (p为素数): f(n)=(1+p1+p1^2+ ...

  4. LightOJ 1369 Answering Queries(找规律)

    题目链接:https://vjudge.net/contest/28079#problem/P 题目大意:给你数组A[]以及如下所示的函数f: long long f( int A[], int n  ...

  5. LightOJ 1336 - Sigma Function

    原题链接 基础数论中很经典的一道题 题意 给出了σ(n)的计算公式,让你找出整数1-n中有多少对应σ(n)的值是偶数. 思路 观察σ(n)的公式发现,每一个乘项都是 (piei+1 - 1) / (p ...

  6. LightOJ 1336 Sigma Function(数论 整数拆分推论)

    --->题意:给一个函数的定义,F(n)代表n的所有约数之和,并且给出了整数拆分公式以及F(n)的计算方法,对于一个给出的N让我们求1 - N之间有多少个数满足F(x)为偶数的情况,输出这个数. ...

  7. LightOJ 1410 Consistent Verdicts(找规律)

    题目链接:https://vjudge.net/contest/28079#problem/Q 题目大意:题目描述很长很吓人,大概的意思就是有n个坐标代表n个人的位置,每个人听力都是一样的,每人发出一 ...

  8. LightOJ - 1336 Sigma Function(约数和+整数拆分)

    题干中给出函数公式: 其中pi为n的每个素因数,ei为其个数.设该函数为F(x),其意义为x的约数之和.问在1-n中有多少x,令F(x)为偶数. 分析:设f(p)为(p^(e+1)-1)/(p-1). ...

  9. BZOJ1432: [ZJOI2009]Function(找规律)

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1523  Solved: 1128[Submit][Status][Discuss] Descriptio ...

随机推荐

  1. laravel重要概念和知识点

    Service Provider: 一个laravel service provider就是一个注册IoC container binding的类.实际上,laravel本身就自包含了一堆管理核心框架 ...

  2. 漫游Kafka设计篇之主从同步

    Kafka允许topic的分区拥有若干副本,这个数量是可以配置的,你可以为每个topci配置副本的数量.Kafka会自动在每个个副本上备份数据,所以当一个节点down掉时数据依然是可用的. Kafka ...

  3. java前端选择

    在做web开发的时候难免遇到一个问题,那就是,选择什么样的框架.下面把前端的框架简单的列一下. 1.flex Apache基金会今天发布了Flex 4.8版本,这是Adobe将Flex捐献给Apach ...

  4. Strlen()与sizeof()

    在学习C语言时以及面试中,经常会见到strlen()与sizeof()这一对容易混淆的概念,搞清楚这两个概念,往往考察了编程人员对语言的基本掌握能力. 首先大家先明确两个概念是: 1.strlen() ...

  5. hibernate注解(转)

    一.实体Bean 每个持久化POJO类都是一个实体Bean, 通过在类的定义中使用 @Entity 注解来进行声明. 声明实体Bean @Entity public class Flight impl ...

  6. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  7. 学习opengl(起步)

    库可以在这里下载 第一个程序: #ifndef GLUT_DISABLE_ATEXIT_HACK #define GLUT_DISABLE_ATEXIT_HACK #endif #include &l ...

  8. LINUX下的tty,console与串口分析

    1.LINUX下TTY.CONSOLE.串口之间是怎样的层次关系?具体的函数接口是怎样的?串口是如何被调用的? 2.printk函数是把信息发送到控制台上吧?如何让PRINTK把信息通过串口送出?或者 ...

  9. Levenshtein Distance (编辑距离) 算法详解

    编辑距离即从一个字符串变换到另一个字符串所需要的最少变化操作步骤(以字符为单位,如son到sun,s不用变,将o->s,n不用变,故操作步骤为1). 为了得到编辑距离,我们画一张二维表来理解,以 ...

  10. [再寄小读者之数学篇](2014-12-04 $\left(1+\frac{1}{x}\right)^x>\frac{2ex}{2x+1},\forall\ x>0.$)

    试证: $$\bex \left(1+\frac{1}{x}\right)^x>\frac{2ex}{2x+1},\forall\ x>0. \eex$$ 证明 (from Hanssch ...