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 ...
随机推荐
- ubuntu网络、包管理、工作内容小结
中国地图 1.配置IP cat /etc/network/interfaces auto lo iface lo inet loopback # The loopback network interf ...
- [Bzoj4942][Noi2017]整数(线段树)
4942: [Noi2017]整数 Time Limit: 50 Sec Memory Limit: 512 MBSubmit: 363 Solved: 237[Submit][Status][D ...
- BZOJ题目(持续更新)
bzoj1009:kmp想法+递推+矩阵快速幂.很好的想法,考虑用长串去kmp匹配短串,dp[i][j]表示匹配指针分别指在i.j位置时候,前i位母字符串一共有多少种可能性,那么dp[i][j]=Σd ...
- 这道js面试题号称99%的人会做错
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 如何使用ssh远程编辑定时任务crontab?
linxu定时任务使用crontab,编辑crontab可以直接编辑:crontab -e:也可以直接读取文件 crontab file.这两种操作都不需要特殊权限sudo.区别在于,crontab ...
- Exchange 2013 Database Move to New Partition
建议不要删除默认数据库,可以通过修改默认数据库名称.路径等实现您的需求. 客戶:The HK Anti-Cancer Society. 要求:遷移數據庫(01)到新分區,實際是遷移成為數據庫(05) ...
- [C++设计模式] proxy 代理模式
代理模式:为其它对象提供一种代理以控制对这个对象的訪问. Proxy: 保存一个引用使得代理能够訪问实体.若RealSubject和Subject的接口同样,Proxy会引用Subject,就相当于在 ...
- scheme语言编写执行
scheme是lisp的一种 编辑器能够用emacs.网上有非常多教导怎样编写的 (begin (display "hello") (newline)) 编写完以.scm保存,这里 ...
- 自己写的通过ADO操作mysql数据库
#include <iostream> #include <windows.h> #include <atlstr.h> #import "c:\Prog ...
- Objective C block背后的黑魔法
前言 block在Objective C开发中应用非常广泛,我们知道block会捕获外部对象,也知道使用block要防止循环引用. "知其然而不知其所以然"是一件非常痛苦的事情,那 ...