LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)
http://lightoj.com/volume_showproblem.php?problem=1336
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代码:
- #include<stdio.h>
- #include<math.h>
- #include<string.h>
- #include<stdlib.h>
- #include<algorithm>
- using namespace std;
- typedef long long ll;
- const int N = 1e6 + ;
- int main()
- {
- int t, p = ;
- ll n;
- scanf("%d", &t);
- while(t--)
- {
- p++;
- ll x, y;
- scanf("%lld", &n);
- x = (ll)sqrt(n);//计算n中x^2的个数
- y = (ll)sqrt(1.0 * n / );//计算n中2*x^2的个数
- printf("Case %d: %lld\n", p, n - x - y);
- }
- return ;
- }
TLE代码(可以用来打表找规律):
- #include<stdio.h>
- #include<math.h>
- #include<string.h>
- #include<stdlib.h>
- #include<algorithm>
- using namespace std;
- typedef long long ll;
- const int N = 1e6 + ;
- int prime[N], k;
- bool Isprime[N];
- void Prime()
- {
- k = ;
- memset(Isprime, true, sizeof(Isprime));
- Isprime[] = false;
- for(int i = ; i < N ; i++)
- {
- if(Isprime[i])
- {
- prime[k++] = i;
- for(int j = ; j * i < N ; j++)
- Isprime[j * i] = false;
- }
- }
- }
- ll solve(ll n)
- {
- ll x, y, a;
- ll num = ;
- for(ll i = ; i < k && prime[i] * prime[i] <= n ; i++)
- {
- x = ;
- if(n % prime[i] == )
- {
- while(n % prime[i] == )
- {
- x++;
- n /= prime[i];
- }
- x += ;
- a = ;
- for(ll j = ; j <= x ; j++)
- a *= prime[i];
- y = a - ;
- num *= y /(prime[i] - );
- }
- }
- if(n > )
- num *= ((n * n - ) / (n - ));
- return num;
- }
- int main()
- {
- Prime();
- int t, x = ;
- ll n;
- scanf("%d", &t);
- while(t--)
- {
- x++;
- scanf("%lld", &n);
- ll num = ;
- for(ll i = ; i <= n ; i++)
- {
- ll m = solve(i);
- if(m % != )
- {
- printf("%lld %lld\n", i, m);
- num++;
- }
- }
- printf("Case %d: %lld\n", x, n - - num);
- }
- return ;
- }
LightOJ 13361336 - Sigma Function (找规律 + 唯一分解定理)的更多相关文章
- LightOJ - 1336 - Sigma Function(质数分解)
链接: https://vjudge.net/problem/LightOJ-1336 题意: Sigma function is an interesting function in Number ...
- LightOJ 1245 数学题,找规律
1.LightOJ 1245 Harmonic Number (II) 数学题 2.总结:看了题解,很严谨,但又确实恶心的题 题意:求n/1+n/2+....+n/n,n<=2^31. ...
- 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+ ...
- LightOJ 1369 Answering Queries(找规律)
题目链接:https://vjudge.net/contest/28079#problem/P 题目大意:给你数组A[]以及如下所示的函数f: long long f( int A[], int n ...
- LightOJ 1336 - Sigma Function
原题链接 基础数论中很经典的一道题 题意 给出了σ(n)的计算公式,让你找出整数1-n中有多少对应σ(n)的值是偶数. 思路 观察σ(n)的公式发现,每一个乘项都是 (piei+1 - 1) / (p ...
- LightOJ 1336 Sigma Function(数论 整数拆分推论)
--->题意:给一个函数的定义,F(n)代表n的所有约数之和,并且给出了整数拆分公式以及F(n)的计算方法,对于一个给出的N让我们求1 - N之间有多少个数满足F(x)为偶数的情况,输出这个数. ...
- LightOJ 1410 Consistent Verdicts(找规律)
题目链接:https://vjudge.net/contest/28079#problem/Q 题目大意:题目描述很长很吓人,大概的意思就是有n个坐标代表n个人的位置,每个人听力都是一样的,每人发出一 ...
- LightOJ - 1336 Sigma Function(约数和+整数拆分)
题干中给出函数公式: 其中pi为n的每个素因数,ei为其个数.设该函数为F(x),其意义为x的约数之和.问在1-n中有多少x,令F(x)为偶数. 分析:设f(p)为(p^(e+1)-1)/(p-1). ...
- BZOJ1432: [ZJOI2009]Function(找规律)
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1523 Solved: 1128[Submit][Status][Discuss] Descriptio ...
随机推荐
- laravel重要概念和知识点
Service Provider: 一个laravel service provider就是一个注册IoC container binding的类.实际上,laravel本身就自包含了一堆管理核心框架 ...
- 漫游Kafka设计篇之主从同步
Kafka允许topic的分区拥有若干副本,这个数量是可以配置的,你可以为每个topci配置副本的数量.Kafka会自动在每个个副本上备份数据,所以当一个节点down掉时数据依然是可用的. Kafka ...
- java前端选择
在做web开发的时候难免遇到一个问题,那就是,选择什么样的框架.下面把前端的框架简单的列一下. 1.flex Apache基金会今天发布了Flex 4.8版本,这是Adobe将Flex捐献给Apach ...
- Strlen()与sizeof()
在学习C语言时以及面试中,经常会见到strlen()与sizeof()这一对容易混淆的概念,搞清楚这两个概念,往往考察了编程人员对语言的基本掌握能力. 首先大家先明确两个概念是: 1.strlen() ...
- hibernate注解(转)
一.实体Bean 每个持久化POJO类都是一个实体Bean, 通过在类的定义中使用 @Entity 注解来进行声明. 声明实体Bean @Entity public class Flight impl ...
- LeetCode Longest Common Prefix 最长公共前缀
题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...
- 学习opengl(起步)
库可以在这里下载 第一个程序: #ifndef GLUT_DISABLE_ATEXIT_HACK #define GLUT_DISABLE_ATEXIT_HACK #endif #include &l ...
- LINUX下的tty,console与串口分析
1.LINUX下TTY.CONSOLE.串口之间是怎样的层次关系?具体的函数接口是怎样的?串口是如何被调用的? 2.printk函数是把信息发送到控制台上吧?如何让PRINTK把信息通过串口送出?或者 ...
- Levenshtein Distance (编辑距离) 算法详解
编辑距离即从一个字符串变换到另一个字符串所需要的最少变化操作步骤(以字符为单位,如son到sun,s不用变,将o->s,n不用变,故操作步骤为1). 为了得到编辑距离,我们画一张二维表来理解,以 ...
- [再寄小读者之数学篇](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 ...