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 ...
随机推荐
- R语言缺失值信息处理
mean(!is.na(mat))可以计算数据完整度(没有缺失值的) mean(!is.na(mat))>0.9,90%完整可以使用 # 缺失值的位置研究as.vector(attributes ...
- R语言AMORE包实现BP神经网络——German数据集
library(AMORE)data<-read.table('G:\\dataguru\\ML\\ML09\\基于BP网络的个人信贷信用评估\\基于BP网络的个人信贷信用评估\\german. ...
- 51nod1354 选数字
01背包tle. 解题报告(by System Message) 类似于背包的DP,以乘积为状态.先把等选数字里面不是K约数的去掉.然后找出K的约数,进行离散化.然后dp[i][j]表示前i个数字乘积 ...
- cakephp之查询
find public find( string $type 'first' , array $query array() ) Queries the datasource and returns a ...
- UVA 11865 Stream My Contest 组网 (朱刘算法,有向生成树,树形图)
题意: 给n个点编号为0~n-1,0号点为根,给m条边(含自环,重边),每条边有个代价,也有带宽.给定c,问代价不超过c,树形图的最小带宽的最大值能达到多少? 思路: 点数才60,而带宽范围也不大,可 ...
- HDU 5353 Average 糖果分配(模拟,图)
题意:有n个人坐在圆桌上,每个人带着糖果若干,每次只能给旁边的人1科糖果,而且坐相邻的两个人最多只能给一次(要么你给我,要么我给你),问是否能将糖果平均分了. 思路: 明显每个人最多只能多于平均值2个 ...
- 关于UNION ALL与 UNION 用法和区别
(转自:http://www.cnblogs.com/EricaMIN1987_IT/archive/2011/01/20/1940188.html) UNION指令的目的是将两个SQL语句的结果合并 ...
- 关于"user.dir"的认识
最近阅读了一些tomcat源码,看到tomcat在读取jar包外配置文件,是将“user.dir”的路径作为home path文件即 通过System.getProperty("user.d ...
- 细数Android开源项目中那些频繁使用的并发库中的类
这篇blog旨在帮助大家 梳理一下前面分析的那些开源代码中喜欢使用的一些类,这对我们真正理解这些项目是有极大好处的,以后遇到类似问题 我们就可以自己模仿他们也写 出类似的代码. 1.ExecutorS ...
- C#利用最新版的WPS实现导入导出
微软的EXCEl操作相信大家也知道,不方便,安装包太大,而且表格的数据量也只有6000多(是6000多还是60000多我就忘记了),在导出导入大量数据的就没办法,而wsp表格则实现了百万数据的容量,而 ...