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. 基于visual Studio2013解决C语言竞赛题之1013字符串查找

         题目 解决代码及点评 /* 功能:编写函数IND,让它判断一个字符串是否为另一个字符串的子串的功能,若是则返回第一次出现的起始位置,否则返回0 时间:13:55 2013 ...

  2. hdu1003 最大连续子序和

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  3. W英语: 紧急, 非紧急

    take your time 慢慢来 It is not urgent. Take it easy please. 不急,慢慢来.

  4. Linux 安装ibus极点五笔输入法备忘录

    Linux 安装 ibus 五笔输入法备忘录 useful?: https://github.com/definite/ibus-table-chinese 一. yum install ibus* ...

  5. 一些实用的mysql语句(不断积累更新)

    1.数据表里仅仅有生日字段,想计算出其年龄的mysql语句: SELECT *,DATE_FORMAT(FROM_DAYS(TO_DAYS(NOW( ))-TO_DAYS(生日字段)),'%Y')+0 ...

  6. Matlab图像处理系列2———空间域平滑滤波器

    注:本系列来自于图像处理课程实验,用Matlab实现最主要的图像处理算法 本文章是Matlab图像处理系列的第二篇文章.介绍了空间域图像处理最主要的概念----模版和滤波器,给出了均值滤波起和中值滤波 ...

  7. python3语法小记(二)列表 和 元组

    列表(list): 列表就像一个线性容器,但是比C++的 lis t扩展多得多 列表里的元素可以是相同类型,也可以包含各种类型,比如列表里嵌套另一个列表 >>> L1 = [1,2, ...

  8. C++学习之路—多态性与虚函数(二)纯虚函数与抽象类

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 1    纯虚函数 在前面的博客中已经提到:有时 ...

  9. Python 自动化测试报告HTMLTestRunner

    HTMLTestRunner 是Python的一个测试报告 参考网址: http://tungwaiyip.info/software/HTMLTestRunner.html https://pypi ...

  10. Android开发之查看应用包名package和入口activity名称的方法

    使用android自动化测试工具monkeyrunner启动应用时,需要填写被测程序的包名和启动的Activity,以下有两种查看应用包名package和入口activity名称的方法: 方法一:使用 ...