LightOJ - 1336 Sigma Function (n以内约数和为奇数(偶数)的数的个数)
2017-07-06 09:20
323 查看
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很大不能直接暴力的
关于sigma(n)的式子有个想法是这样的:sigma(n)=(1+q1+...+q1^e1)*(1+q2+...+q2^e2)*....*(1+qn+...+qn^en)
很显然当q[i]不为2的时候,当e[i]为奇数时括号内的和为偶数,e[i]为偶数时括号内的和为奇数,
当q[i]==2时,括号内的和必为奇数;
这个题到这一步我都想到了,但接下来的我就GG了....
这个题直接求偶数的个数的话就是直接求存在q[i]不为2且e[i]为奇数的数的个数,但这个我想了很久觉得好像没什么办法可以求出来(可能是因为我很菜吧)
那么可以反着来:求sigma为奇数的个数,然后减一减
当sigma为奇数的时候,必有:q[i]不为2的项的e[i]全部为偶数
于是sigma(s)为奇数的时候,s可以以两种方法得到:s=a^2,s=2*b^2;
(不管a原来的sigma是怎么样的,a^2一定是“所有的e[i]都为偶数,使得任意括号内的和都为奇数,从而a^2为奇数”)
(b也与a一样,但是a的2的次数一定也是偶数,然而sigma为奇数的数中,2的次数是可奇可偶的,所以有乘以2)
然后s还要满足s<=n,
于是a的个数,b的个数分别为sqrt(n),sqrt(n/2),所以sigma为奇数的数的个数为sqrt(n)+sqrt(n/2)....然后减一减
(呜哇~做过一遍的题还不会做,感觉好伤)
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=2e5+3;
LL n;
int main()
{
int T;
scanf("%d",&T);
int cas=0;
while(T--)
{
scanf("%lld",&n);
printf("Case %d: %lld\n",++cas,n-(LL)sqrt(n)-(LL)sqrt(n/2));
}
return 0;
}
大概就是这样,如有错误,还望指正
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很大不能直接暴力的
关于sigma(n)的式子有个想法是这样的:sigma(n)=(1+q1+...+q1^e1)*(1+q2+...+q2^e2)*....*(1+qn+...+qn^en)
很显然当q[i]不为2的时候,当e[i]为奇数时括号内的和为偶数,e[i]为偶数时括号内的和为奇数,
当q[i]==2时,括号内的和必为奇数;
这个题到这一步我都想到了,但接下来的我就GG了....
这个题直接求偶数的个数的话就是直接求存在q[i]不为2且e[i]为奇数的数的个数,但这个我想了很久觉得好像没什么办法可以求出来(可能是因为我很菜吧)
那么可以反着来:求sigma为奇数的个数,然后减一减
当sigma为奇数的时候,必有:q[i]不为2的项的e[i]全部为偶数
于是sigma(s)为奇数的时候,s可以以两种方法得到:s=a^2,s=2*b^2;
(不管a原来的sigma是怎么样的,a^2一定是“所有的e[i]都为偶数,使得任意括号内的和都为奇数,从而a^2为奇数”)
(b也与a一样,但是a的2的次数一定也是偶数,然而sigma为奇数的数中,2的次数是可奇可偶的,所以有乘以2)
然后s还要满足s<=n,
于是a的个数,b的个数分别为sqrt(n),sqrt(n/2),所以sigma为奇数的数的个数为sqrt(n)+sqrt(n/2)....然后减一减
(呜哇~做过一遍的题还不会做,感觉好伤)
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=2e5+3;
LL n;
int main()
{
int T;
scanf("%d",&T);
int cas=0;
while(T--)
{
scanf("%lld",&n);
printf("Case %d: %lld\n",++cas,n-(LL)sqrt(n)-(LL)sqrt(n/2));
}
return 0;
}
大概就是这样,如有错误,还望指正
相关文章推荐
- LightOJ1336 Sigma Function(约数和为奇数的个数)
- LightOJ1336 Sigma Function(约数和为偶数的个数)
- LightOJ1336 Sigma Function(约数和为奇数的个数)
- LightOJ 1336 Sigma Function(求1~n的因数和为偶数的个数)
- LightOJ 1336 Sigma Function(1--n中所有因子和为偶数的数字个数)
- 问题六十五: 计算正整数n以内(包括n)的奇数之和及偶数之和。
- 分别计算100以内的奇数之和和偶数之和
- Python3基础 dict 推导式 生成10以内+奇数的值为True 偶数为False的字典
- LightOJ1336 Sigma Function(约数和为奇数的个数)
- C - Sigma Function(LightOJ 1336)
- lightoj 1336 Sigma Function
- 学习脚本1:计算100以内奇数和和偶数和 (笔记)
- 求100以内的奇数和偶数的和
- 将100个100以内随机整数存入数组,将奇数存入oddNum[]中,将偶数存入Even[]中,并将oddNum[]和Even[]打印
- LightOJ-1336 Sigma Function 唯一分解定理 巧妙使用sqrt()等算数目
- 100以内自然数和_奇数和_偶数和
- LightOJ 1336- Sigma Function
- D - Sigma Function 1~n内有多少个约数和为偶数
- 【数论 && 公式推导】LightOJ - 1336 Sigma Function
- LightOJ 1336 Sigma Function