LightOJ1336——Sigma Function

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的因数和是偶数的有多少个,完全蒙逼,题解看了好半天才看懂
1. 首先我们先求出和是奇数的个数,然后用n减去
2. 因数和的式子是f(x)= (1+p1+p1^2+p1^3+…+p1^a1)(1+p2+p2^2+…+p2^a2)…*(1+pn+pn^2+…+pn^an); 当有一个因子p=2时,无论系数多少,(1+2^1+2^2+…+2^a1) 都是奇数
3. 然后如果p不是2,那么这个对应的系数必须是一个偶数,也就是后面pn+pn^2+…+pn^an 要是偶数项,这样加起来才是奇数
4. 所以满足这两个条件,就之后平方数和两倍的平方数了,为什么呢,因为比如这个数n没有2这个因子,那么就只有3啊5啊这些,而且系数都是偶数,比如2,所以这个数就是3^2*5^2,这不就是一个平方数吗,而如果这个数还有2这个因子,无论2有多少次,都可以转为平方数或者平方数乘2
5. 所以,找出1-n中满足条件的数的个数即可

代码:

#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(void){
    int t;
    ll n;
    scanf("%d",&t);
    int c=1;
    while(t--){
        ll ans=0;
        scanf("%lld",&n);
        //先求出因数和为奇数的个数
        for(ll i=1;i*i<=n;i++){
            ans++;
            if(2*i*i<=n){
                ans++;
            }
        }
        printf("Case %d: %lld\n",c++,n-ans);
    }
    return 0;
}
全部评论

相关推荐

头像
05-16 12:47
已编辑
中国地质大学(武汉) Java
你出生在农村,与其它农村小孩子无异小学时你对成绩没有概念,只感觉上课不听课也是无聊,只知道不写完作业会被老师罚站一到考试,自己成绩总是名列靠前,即使偶尔落后,你也从不在意中学时你觉得课本的东西很简单,随便学学就会了,并没有大量刷题你总是想不通,那些所谓的数学物理中难题,明明是在送分,为什么你的同学总是想不出解题方法高中时这三年你过的不容易,晚睡早起,给了自己很多压力.但是你也发现自己是有些小聪明的,你感觉班里有些同学很刻苦,但成绩比你差远了。那些数学题和物理题的陷阱,同学一遍遍踩坑,但是你总能发现并避开它们.“为了父母的期盼,为了恩师的厚望,为了天赐的智慧,为了青春的理想......”“天行健...
创作助手_刘北:其实,这种已经是神童级别的了,不费吹灰之力就能拿到自己想要的东西,就像机器按照程序走了一遍,就像我小时候看爱情公寓,觉得他们都很惨,几个人只能挤在一个房间里合租,但是好在他们有一群非常好的朋友,随着时间的推移,慢慢长大了,在看爱情公寓的时候,觉得他们都很厉害,博士、留学生、***、电台公子,数学天才,任何一个都是我可望而不可即的,更别说可以在异地认识一群更好的朋友了,所以呢,人还是要自给自足,满足当下,不要攀比,意气风发的且有理想的18岁少年永远都存在,只不过随着时间的推移他被你包裹在了洋葱的最深处。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务