The Boss on Mars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1335    Accepted Submission(s): 401

Problem Description
On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger boss.

Due to no moons around Mars, the employees can only get the salaries per-year. There are n employees in ACM, and it’s time for them to get salaries from their boss. All employees are numbered from 1 to n. With the unknown reasons, if the employee’s work number is k, he can get k^4 Mars dollars this year. So the employees working for the ACM are very rich.

Because the number of employees is so large that the boss of ACM must distribute too much money, he wants to fire the people whose work number is co-prime with n next year. Now the boss wants to know how much he will save after the dismissal.

 
Input
The first line contains an integer T indicating the number of test cases. (1 ≤ T ≤ 1000) Each test case, there is only one integer n, indicating the number of employees in ACM. (1 ≤ n ≤ 10^8)

 
Output
For each test case, output an integer indicating the money the boss can save. Because the answer is so large, please module the answer with 1,000,000,007.

 
Sample Input
2
4
5
 
Sample Output
82
354

Hint

Case1: sum=1+3*3*3*3=82
Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354

 
Author
ZHANG, Chao
 
Source
 
Recommend
lcy
这题,我们比赛都写的差不多了,就是在算的时候由于n的平方,爆掉了,不知道哪里错了,一直卡到了最后!也没发现,唉,可惜了!
首先我们可以得到公式1^4+2^4+3^4+.....+m^4为m*(1+m)*(1+2*m)(3*m*m+3*m-1);然后,用个容斥定理就解决问题了!
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
using namespace std;
#define mod 1000000007
__int64 vec[33];
int ans;
__int64 prime[10500],pans;
__int64 re;
__int64 n;
int init()
{
int i;
memset(prime,0,sizeof(prime));
for(i=2;i<=10000;i++)
{
if(!prime[i])
for(int j=i+i;j<10050;j=j+i)
{
prime[j]=1;
}
}
pans=0;
for(i=2;i<=10000;i++)
{
if(!prime[i])
prime[pans++]=i;
}
return 1;
}
__int64 ff(__int64 k)
{
int i,j;
__int64 m=(__int64)(n/k),a[5],b[]={2,3,5};
a[0]=m;a[3]=(1+m),a[1]=(1+2*m),a[2]=(3*m*m+3*m-1);
for(j=0;j<=2;j++)
{
for(i=0;i<4;i++)
{
if(a[i]%b[j]==0)
{
a[i]/=b[j];
break;
}
}
}
return a[2]%mod*a[0]%mod*a[1]%mod*a[3]%mod*k%mod*k%mod*k%mod*k%mod;
}
void dfs(int now,int len,__int64 s )
{
int i;
if(len>=ans)
return;
if(now>=ans)
return;
if(len&1)
{
re+=ff(s);
re%=mod;
if(re<0)
re+=mod;
}
else
{
re-=ff(s);
re%=mod;
if(re<0)
re+=mod;
}
for(i=now+1;i<ans;i++)
{
dfs(i,len+1,s*vec[i]);
}
}
bool isp(__int64 ii,__int64 pri)
{
if(pri==1)
return false;
__int64 m=sqrt((double)pri);
for(__int64 i=ii+1;i<pans&&prime[i]<=m;i++)
{
if(pri%prime[i]==0)
return false;
}
return true;
}
int main()
{
init();
int tcase,i;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%I64d",&n);
ans=0;
__int64 m=(__int64)sqrt((double)n)+1;
__int64 tempn=n;
bool flag=true;
if(isp(-1,n))
flag=false;
else
flag=true;__int64 ii=prime[0];
for(__int64 i1=0;flag&&ii<=tempn&&i1<pans;i1++)
{
ii=prime[i1];
if(tempn%ii==0)
{
vec[ans++]=ii;
while(tempn%ii==0)
{
tempn/=ii;
}
if(isp(i1,tempn))
flag=false;
}
}
if(!flag)
vec[ans++]=tempn;
re=ff(1);
for(i=0;i<ans;i++)
dfs(i,0,vec[i]);
printf("%I64d\n",re);
}
return 0;
}

hdu4059 The Boss on Mars的更多相关文章

  1. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  2. hdu4059 The Boss on Mars 容斥原理

    On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger bo ...

  3. HDU 4059 The Boss on Mars 容斥原理

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)

    传送门 The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  5. 数论 + 容斥 - HDU 4059 The Boss on Mars

    The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...

  6. The Boss on Mars

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 4059 The Boss on Mars(容斥原理)

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 4059 The Boss on Mars

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  9. zoj 3547 The Boss on Mars

    需要用到概率论的容斥定理以及计算1 ^ 4 + 2 ^ 4 + ……+ n ^ 4的计算公式1^4+2^4+……+n^4=n(n+1)(2n+1)(3n^2+3n-1)/30 #pragma comm ...

随机推荐

  1. Oracle 当前时间加减

     当我们用 select sysdate+number from dual ;我们得到的是,当前的时间加上number天后的时间.从这里我们也可以看出,使用这种方式进行时间计算的时候,计算的单位是天, ...

  2. ubuntu无法解析主机错误与解决的方法

    今天在用命令行进行操作的时候,出现了无法解析主机的错误.google了一下,原来是hosts文件的问题.更改过来即可了 进入终端,输入 sudo gedit /etc/hosts.输入password ...

  3. UVA 10574 - Counting Rectangles(枚举+计数)

    10574 - Counting Rectangles 题目链接 题意:给定一些点,求可以成几个边平行于坐标轴的矩形 思路:先把点按x排序,再按y排序.然后用O(n^2)的方法找出每条垂直x轴的边,保 ...

  4. Spring MVC程序

    Spring MVC程序(IDEA开发环境)   回顾Java平台上Web开发历程来看,从Servlet出现开始,到JSP繁盛一时,然后是Servlet+JSP时代,最后演化为现在Web开发框架盛行的 ...

  5. arm-linux-gcc下载与安装

    在RHEL 5平台上安装配置arm-linux-gcc  2011-02-23 19:35:40|  分类: 嵌入式开发环境 |  标签: |字号大中小 订阅 . 在linux平台上安装好的基础上,开 ...

  6. Get Cordova Ready for Grunt and CoffeeScript

    Cordova, Grunt and Coffee You may reference to below if you deside to work with coffee instead of Ja ...

  7. SVM(三)—Kernels(核函数)

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 内容整理中...

  8. Python 学习入门(21)—— 线程

    本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础以及Python两个线程标准库的完整介绍及使用示例. 1. 线程基础 1.1. 线程状态 线程有5种状态,状态转换的过程如下图 ...

  9. PHP之操作数据库

    数据库,顾名思义,是一个存放数据的容器.然后在使用过程中对数据库里面的数据增删改查,具体是怎么实现的呢? 这儿不得不提一下一个神奇的东西:SQL语句:结构化查询语言(Structured Query ...

  10. Android SDK Manager安装报错

    安装出错了,上网百度一大堆说法,又说版本兼容问题的,有说http换https的,我这个菜鸟真搞不明白,自己捣腾一下午总算弄上去了! 问题: 安装完Eclipse和ADT却无法更新SDK. 启动Andr ...