Sigma Function Input

本文介绍了一种通过素因子分解来快速找出1至n中因子和为偶数的整数个数的方法。利用数学规律避免了直接计算的复杂度,通过去重计算,有效地解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

题目扩展:


题意:

1—n中,有多少个数的因子和是偶数。

题解:

打表找规律。

素因子分解打表计算前n项和判断奇数偶数可以发现如下规律:

只要是2^x,x^2,2*x^2...只有这种数的因子和是奇数。所以,我们直接去重即可。
但是这些直接去重我们会发现减去的这些值有重复的,所以我们要判断下。

i(代表x||a):0 1 2 3 4 5 6 7 8 9 ......

2^x: 1 2 4 8 16 32 64 128......

a^2:0 1 4 9 16 25 36 49 64 ......

2*a^2:0 2 8 18 32 50 72 ......

我们可以发现2^x里面有的数,x^22*x^2里面都有。

加下划线的字一一对应,加粗的字一一对应。

2^xx^2, x为偶数时二者出现重复。
②2^x
2*x^2,当x为奇数时,二者出现重复。

所以不需要考虑2^x的个数,直接用n减去x^22*x^2的个数就是我们要的结果。

易知:x^2的个数=sqrt(n)2*x^2的个数=sqrt(n/2)

那么为什么会是这样呢?给出推导过程:

n=p1^e1*p2^e2...,f(n)=(p1^(e1+1)-1)/(p1-1))*(p2^(e2+1)-1)/(p2-1))....
(p1^(e1+1)-1/(p1-1))=p1^0+p1^1......+p1^e1;
要使得f(n)为奇数,则(p1^(e1+1)-1)/(p1-1)(pn^(en+1)-1)/(pn-1)都要为奇数;

因为奇数*奇数=奇数,奇数*偶数=偶数;

1)p=2时,2^(e+1)-1,一定为奇数;
2)
p!=2时,则p为奇数(因为p是素因子),则当e为偶数时(p^(e+1)-1)/(p-1)为奇数。Sn=(p^(e+1)-1)/(p-1),相当于首项为1,公比为p的数列求和,p为奇素数,当e为偶数时,为奇数项奇数求和,结果必为奇数。

代码如下:

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
int main()
{
    int t,cc=1;
    cin>>t;
    while(t--)
    {
        ll n;
        cin>>n;
        printf("Case %d: %lld\n",cc++,n-(int)sqrt(n)-(int)sqrt(n/2));
    }

}

转自:https://www.cnblogs.com/Ritchie/p/5299970.html

### Matlab Function 的语法与示例 Matlab Function 提供了一种灵活的方式来集成复杂的算法逻辑到 Simulink 模型中。它允许用户编写自定义 MATLAB 函数并将其嵌入到模型中,从而充分利用 MATLAB 的强大功能及其丰富的工具箱资源[^1]。 #### 基本语法结构 以下是 Matlab Function 的基本语法框架: ```matlab function [output1, output2, ...] = functionName(input1, input2, ...) % 这是一个注释部分,用于描述函数的功能和参数说明 % 函数体 output1 = someOperation(input1); output2 = anotherOperation(input2); end ``` 在此基础上,可以进一步扩展以支持更复杂的应用场景。 --- #### 示例:图像处理中的滤波器应用 假设我们需要在一个图像处理系统中实现高斯模糊效果,则可以通过如下方式构建 Matlab Function 来完成此任务: ```matlab function blurredImage = applyGaussianBlur(inputImage, sigma) % applyGaussianBlur 对输入图像施加高斯模糊 % % 输入: % inputImage - 待处理的灰度或彩色图像 (矩阵形式) % sigma - 高斯核的标准差 % % 输出: % blurredImage - 经过高斯模糊后的图像 % 调用 image processing toolbox 中的 imgaussfilt 函数 blurredImage = imgaussfilt(inputImage, sigma); end ``` 上述代码展示了如何通过 `imgaussfilt` 函数轻松实现图像的高斯模糊操作,并且该函数可以直接被嵌套于 Simulink 环境下的 Matlab Function 模块之中。 --- #### 示例:傅里叶逆变换计算 另一个常见的应用场景涉及信号分析领域内的频域转换问题。下面给出一段关于傅里叶逆变换的具体实例: ```matlab function timeSignal = inverseFourierTransform(freqSignal, tVar) % inverseFourierTransform 计算给定频率响应的时间序列数据 % % 输入: % freqSignal - 表达式的符号表示或者数值向量 % tVar - 时间变量名,默认为 't' % % 输出: % timeSignal - 返回对应时间域上的解析表达式或者是离散样本集 % 使用 ifourier 执行反傅立叶变换 timeSignal = ifourier(freqSignal, tVar); end ``` 这里运用到了内置命令 `ifourier()` 完成从频谱回到原始时域信号的过程[^2]。 --- #### 注意事项 当创建新的 Matlab Functions 时需要注意以下几点: - **输入/输出端口数量匹配**:确保实际使用的 I/O 参数数目同声明一致; - **兼容性验证**:测试不同类型的输入值是否都能正常运行; - **性能优化考量**:对于实时仿真环境而言尤其重要,应尽量减少不必要的循环迭代次数以及内存占用率高等情况的发生;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值