LightOJ1336 Sigma Function —— 质因子分解、约数和为偶数
题目链接:https://vjudge.net/problem/LightOJ-1336
| Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 |
Output for Sample Input |
|
4 3 10 100 1000 |
Case 1: 1 Case 2: 5 Case 3: 83 Case 4: 947 |
题意:
求1到n(n<=1e12)内,有多少个数的约数和为偶数。
题解:
1.将一个数n进行质因子分解,得到 pi 和 ai,其中pi为第i个质因子,ai为第i个质因子的个数,
那么这个数的约数和:f(n) = (1+2^1+2^2……2^a1)*(1+3^1+3^2……3^a2)……*(1+pi^1+pi^2……pi^ai)*……
解释:从每一个括号中挑选一个数出来相乘,就得到一个约数。在根据组合数的思想,总的就得到所有约数的和。
2.可知:偶数可以是偶数乘以偶数,也可以是奇数乘以偶数;而奇数只能是奇数乘以奇数。所以,统计奇数要比统计偶数方便,所以总体思想就是用总的个数减去约数和为奇数的个数。
3.那么,要使 f(n) 为奇数,必须满足每个括号中的数之和为奇数。可知,当pi = 2时,括号里的数必定为奇数。因为2的正数次方均为偶数,再加上一个1,就为奇数。所以:
3.1 当n不含有2这个质因子时:每个括号内ai必须为偶数,当ai为偶数就说明了括号内有 ai+1个奇数相加,和为奇数。因此,当每个质因子的个数ai均为偶数时,n可以表示为 n = x^2,即表明当n为一个平方数时,f(n)为奇数。
3.2 当n含有2这个质因子时:可知对于2来说,无论它的个数为多少,对应括号里的和都为奇数,那么只要同时满足其他括号里的数之和都为奇数,即满足3.1的要求,那么f(n)为奇数。此时 n = 2*x^2。(注:当n = 2*2*2*x^2时, n = 2*(2*x)^2,即同样满足 2*x^2的通项公式)
3.3 综上,当 n = x^2 或者 n = 2*x^2时, f(n)为奇数。所以在n之内,有sqrt(n) + sqrt(n/2) 个数的约数和为奇数。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 5e5+; int main()
{
int T, kase = ;
scanf("%d", &T);
while(T--)
{
LL n;
scanf("%lld",&n);
LL ans = n - (LL)sqrt(n) - (LL)sqrt(n/);
printf("Case %d: %lld\n", ++kase,ans);
}
}
LightOJ1336 Sigma Function —— 质因子分解、约数和为偶数的更多相关文章
- LightOJ1336 Sigma Function(约数和为偶数的个数)
Sigma Function Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit ...
- LightOJ-1336 Sigma Function 唯一分解定理 巧妙使用sqrt()等算数目
题目链接:https://cn.vjudge.net/problem/LightOJ-1336 题意 给出一个区间[1, n],求区间内所有数中因数之和为偶数的数目 思路 第二次写这个题 首先想到唯一 ...
- LightOJ1336 Sigma Function
题意 求和运算是一种有趣的操作,它来源于古希腊字母σ,现在我们来求一个数字的所有因子之和.例如σ(24)=1+2+3+4+6+8+12+24=60.对于小的数字求和是非常的简单,但是对于大数字求和就比 ...
- 【LightOJ1336】Sigma Function(数论)
[LightOJ1336]Sigma Function(数论) 题面 Vjudge 求和运算是一种有趣的操作,它来源于古希腊字母σ,现在我们来求一个数字的所有因子之和.例如σ(24)=1+2+3+4+ ...
- D - Sigma Function 1~n内有多少个约数和为偶数
/** 题目:D - Sigma Function 链接:https://vjudge.net/contest/154246#problem/D 题意:求1~n内约数和为偶数的数的个数. 思路:一个数 ...
- Sigma Function (平方数与平方数*2的约数和是奇数)
Sigma Function https://vjudge.net/contest/288520#problem/D Sigma function is an interesting function ...
- Sigma Function LightOJ - 1336 (约数和为奇数)
题意: 求1-n中约数和为偶数的数的个数 记住一个定理:...平方数 及其 平方数的2倍 的约数和为奇数 then....减啦 证明: ....我jiao着人家写的很详细,so 看看人家写的吧! 转 ...
- LightOJ - 1336 - Sigma Function(质数分解)
链接: https://vjudge.net/problem/LightOJ-1336 题意: Sigma function is an interesting function in Number ...
- Sigma Function (LightOJ - 1336)【简单数论】【算术基本定理】【思维】
Sigma Function (LightOJ - 1336)[简单数论][算术基本定理][思维] 标签: 入门讲座题解 数论 题目描述 Sigma function is an interestin ...
随机推荐
- 第3章 Spring Boot 入门指南
Part II. 入门指南 如果你刚刚开始使用Spring Boot,这是你的一部分内容! 在这里我们将会回答一些基本的“what?”, “how?” 和 “why?”的问题. 在这里你会找到一个详细 ...
- java中正则表达式要进行转义的字符。
/** * 转义正则特殊字符 ($()*+.[]?\^{},|) * * @param keyword * @return */public static String escapeExprSpeci ...
- Ext grid中单元格编辑,editor为combobox时用法
{ header: 'TO_PTN_NM', dataIndex: "TO_PTN_NM", sortable: true, renderer: function (v, m, r ...
- SVG动画实践篇-音量变化效果
git 地址:https://github.com/rainnaZR/svg-animations/tree/master/src/pages/step2/volumn 说明 这个动画的效果就是多个线 ...
- Openlayers3 编辑要素
参考文章 Openlayers之编辑要素 MAPZONE GIS SDK接入Openlayers3之五——图形编辑工具 [学习笔记之Openlayers3]要素保存篇(第四篇) openlayers实 ...
- 【spring data jpa】启动报错:nested exception is java.util.NoSuchElementException
spring boot项目中 使用spring data jpa 启动报错: org.springframework.beans.factory.UnsatisfiedDependencyExcept ...
- 【matlab】:matlab中不断的出现计算过程怎么办
这个问题是会常常性出的.就是matlab中不断的出现计算. 关于这个问题,我们须要考虑的是自己是不是写错了,通常会出现以下两种可能的错误 1,关于计算的函数没有写分号 :这样的是致命问题,假设函数不写 ...
- IntelliJ IDEA 10.5.1 引用外部Jar包
具体步骤: File -> Project Structure (ctrl + shift + alt + s ) -> Module -> Dependencies -> A ...
- 直接返回list不封装的结果集
直接返回list不封装的结果集,在Jsp访问方式: 1.封装成map访问 2.用jstl: <c:forEach var="images" items="${lis ...
- ios文件系统文件目录操作
对于一个运行在iPhone得app,它只能访问自己根目录下得一些文件(所谓sandbox). 一个app发布到iPhone上后,目录结构如下: 1.其中获取 app root 可以用 NSHomeDi ...