http://lightoj.com/volume_showproblem.php?problem=1336

Sigma Function

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 (找规律 + 唯一分解定理)的更多相关文章

  1. LightOJ - 1336 - Sigma Function(质数分解)

    链接: https://vjudge.net/problem/LightOJ-1336 题意: Sigma function is an interesting function in Number ...

  2. LightOJ 1245 数学题,找规律

    1.LightOJ 1245   Harmonic Number (II)   数学题 2.总结:看了题解,很严谨,但又确实恶心的题 题意:求n/1+n/2+....+n/n,n<=2^31. ...

  3. 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+ ...

  4. LightOJ 1369 Answering Queries(找规律)

    题目链接:https://vjudge.net/contest/28079#problem/P 题目大意:给你数组A[]以及如下所示的函数f: long long f( int A[], int n  ...

  5. LightOJ 1336 - Sigma Function

    原题链接 基础数论中很经典的一道题 题意 给出了σ(n)的计算公式,让你找出整数1-n中有多少对应σ(n)的值是偶数. 思路 观察σ(n)的公式发现,每一个乘项都是 (piei+1 - 1) / (p ...

  6. LightOJ 1336 Sigma Function(数论 整数拆分推论)

    --->题意:给一个函数的定义,F(n)代表n的所有约数之和,并且给出了整数拆分公式以及F(n)的计算方法,对于一个给出的N让我们求1 - N之间有多少个数满足F(x)为偶数的情况,输出这个数. ...

  7. LightOJ 1410 Consistent Verdicts(找规律)

    题目链接:https://vjudge.net/contest/28079#problem/Q 题目大意:题目描述很长很吓人,大概的意思就是有n个坐标代表n个人的位置,每个人听力都是一样的,每人发出一 ...

  8. LightOJ - 1336 Sigma Function(约数和+整数拆分)

    题干中给出函数公式: 其中pi为n的每个素因数,ei为其个数.设该函数为F(x),其意义为x的约数之和.问在1-n中有多少x,令F(x)为偶数. 分析:设f(p)为(p^(e+1)-1)/(p-1). ...

  9. BZOJ1432: [ZJOI2009]Function(找规律)

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1523  Solved: 1128[Submit][Status][Discuss] Descriptio ...

随机推荐

  1. Machine Learning for hackers读书笔记(八)PCA:构建股票市场指数

    library('ggplot2') prices <- read.csv('G:\\dataguru\\ML_for_Hackers\\ML_for_Hackers-master\\08-PC ...

  2. 【笨嘴拙舌WINDOWS】剪切板

    Windows剪贴板是一种比较简单同时也是开销比较小的IPC(InterProcess Communication,进程间通讯)机制.Windows系统支持剪贴板IPC的基本机制是由系统预留的一块全局 ...

  3. CSS之弧形阴影

    简述 网页上经常会出现一些弧形的阴影效果,看起来很漂亮,下面我们来讲述下如何用CSS来实现一个弧形阴影. 简述 阴影 效果 注释 标题 效果 源码 合并 效果 源码 阴影 效果 首先实现一个简单的阴影 ...

  4. ecshop 首页调用指定类产品

    方法一.已测试成功 1.在/includes/lib_goods.php最底部增加以下代码 function index_get_cat_id_goods_best_list($cat_id = '' ...

  5. js浮点数运算需要注意的问题

    最近在js运算浮点数时发现了一个问题.问题是这样的:js函数中处理两个浮点数的相加,为了防止出现0.1+0.2=0.30000000000000004的问题,两个数都先乘以10000后再相加,得到结果 ...

  6. Arduino运行时突然[卡死在某一行/立即重启/串口输出乱码/程序执行不正常]的可能原因

    1.这一行是分配内存,而内存不够了(Arduino uno只有2k) 2.内存本身已经只剩一点点了,于是就有莫名其妙的问题 3.没有调用Wire.begin().xx.setup()之类的操作!

  7. DevExpress控件GridView挂下拉控件无法对上值

    下拉控件使用RepositoryItemLookUpEdit,加入如下事件进行处理. repositoryItemLookUpEdit1.CustomDisplayText += new DevExp ...

  8. Brush、Color、String相互转换

    using System.Windows.Media; 1.String转换成Color Color color = (Color)ColorConverter.ConvertFromString(s ...

  9. [搜片神器]直接从DHT网络下载BT种子的方法

    DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr DHT系 ...

  10. Android 动画深入解析

    http://blog.csdn.net/rain_butterfly/article/details/39642613