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 ...
随机推荐
- Spring Boot + Elastic stack 记录日志
原文链接:https://piotrminkowski.wordpress.com/2019/05/07/logging-with-spring-boot-and-elastic-stack/ 作者: ...
- 第4章 CentOS软件安装
一.安装JDK 1.1 卸载旧版JDK 首先,在你的服务器上运行一下更新. yum update 然后,在您的系统上搜索,任何版本的已安装的JDK组件. rpm -qa | grep -E '^ope ...
- PyTorch学习笔记之Variable_and_function_cat
application 1 from torch.autograd import Variable import torch b = Variable(torch.FloatTensor([64, 1 ...
- poj 1651 Multiplication Puzzle【区间DP】
题目链接:http://poj.org/problem? id=1651 题意:初使ans=0,每次消去一个值,位置在pos(pos!=1 && pos !=n) 同一时候ans+=a ...
- Java如何Attachment源码
该文章教你如何在Eclipse中Attachment源码,学到了不少东西. http://jingyan.baidu.com/article/1709ad80b107f64635c4f040.html ...
- 使用fuser命令kill一个终端(特殊文件)的方法
/********************************************************************* * Author : Samson * Date ...
- Jememeter和Loadrunner测试MySQL性能
From:http://blog.csdn.net/testingstar/article/details/60579454 MySQL数据库性能测试的方法 前置条件: 安装系统:windows 7 ...
- 【每日Scrum】第二天(4.23) TD学生助手Sprint2站立会议
站立会议 组员 昨天 今天 困难 签到 刘铸辉 (组长) 昨天觉得整个界面不适合后期功能扩展,所以进行了整体整改 今天主要看了多事件处理的内容然后改了下界面, 遇到的困难就是正在寻找用户交互性比较好的 ...
- python訪问redis
python訪问redis 1 Linux上安装redis a) 下载 $ wget http://download.redis.io/releases/redis-3.0.5.tar.gz b) 编 ...
- open-source Julius speech-recognition engine
http://julius.osdn.jp/en_index.php?q=index-en.html Open-Source Large Vocabulary CSR Engine Julius ht ...