传送门

The Boss on Mars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2462    Accepted Submission(s): 760

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
 

题目大意:

给定 T 组数据。每组数据一个数 n ,然后让你求 <n且与 n 互素的数的四次方。详见例子。

解题思路:

就是就一个 S = a1^4 + a2^4 + ... + ak^4;

这个我们能够考虑容斥原理:也就是 1^4 + 2^4 + 3^4 + ... + n^4 - 不互素的数的四次方。

然后遇到了一个 非常严重的问题 就是 1^4 + 2^4 + 3^4 + ... + n^4怎么求;

首先 要推一下Sum =  1 + 2 + 3 + ... +n

(n+1)^2 - n^2 = 2*n+1;

n^2 - (n-1)^2 = 2*(n-1)+1;

...

3^2 - 2^2 = 2*2+1;

2^2 - 1^2 = 2*1+1;

将上述等式 左边加左边 右边加右边得到:

(n+1)^2 - 1 = 2*(1+2+3+...+n)+n*1;

又由于 我们所求 Sum =  1 + 2 + 3 + ... +n;

所以:2*Sum + n = (n+1)^2-1 =n^2+2*n+1-1 = n^2+2*n;

Sum = (n^2+n)/2。

所以 我们要求的四次方之和 就得用到 (n+1)^5 - n^5。(三次方的公式 首先得知道 就是通过刚才那个推出来的)

(x+1)^5 = x^5+5*x^4+10*x^3+10*x^2+5*x+1;

推完之后就是以下这个公式 还是挺费事儿的:

(1^4+2^4+……+n^4)=(n*(n+1)*(2n+1)*(3*n*n+3*n-1))/30;在这里 我们还要求一下 30 关于 MOD = 1e9+7的逆元,这个能够通过扩展欧几里得算法实现...

然后 就是 分析一下 代码的实现过程了:

第一步:我们首先进行素数筛 ;

第二步:我们就要进行素因子分解;

第三步:写一个高速幂 计算 X^4对 MOD取模

第四步:二进制枚举 。奇加偶减;

完事儿。

My Code:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; typedef long long LL;
const LL MOD = 1e9+7;
const LL MAXN = 1e6+5;
LL Scan()///输入外挂
{
LL res=0,ch,flag=0;
if((ch=getchar())=='-')
flag=1;
else if(ch>='0'&&ch<='9')
res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')
res=res*10+ch-'0';
return flag?-res:res;
} inline void Out(LL a)///输出外挂
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}
LL n;
/**
void Ex_gcd(int a, int b, int &x, int &y)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
int x1, y1;
Ex_gcd(b, a%b, x1, y1);
x = y1;
y = x1 - (a/b)*y1;
}**/
bool prime[MAXN];
int p[MAXN];
LL k;
void isprime()
{
memset(prime, false, sizeof(prime));
k = 0;
for(LL i=2; i<MAXN; i++)
{
if(!prime[i])
{
p[k++] = i;
for(LL j=i*i; j<MAXN; j+=i)
prime[j] = true;
}
}
}
int fac[MAXN/100], num[MAXN/100];
LL cnt;
void Dec(LL m)
{
cnt = 0;
memset(num, 0, sizeof(num));
for(LL i=0; i<k&&p[i]*p[i]<=m; i++)
{
if(m%p[i] == 0)
{
fac[cnt] = p[i];
while(m%p[i]==0)
{
m /= p[i];
num[cnt]++;
}
cnt++;
}
}
if(m > 1)
{
fac[cnt] = m;
num[cnt++] = 1;
}
/**cout<<cnt<<endl;
for(int i=0; i<cnt; i++)
cout<<fac[i]<<" ";*/
}
LL Get_ans(LL x)
{
LL ans = x%MOD;
ans = ans*(x+1)%MOD;
ans = ans*(x+x+1)%MOD;
ans = ans*((3*x*x+3*x-1)%MOD)%MOD;
ans = ans*233333335%MOD;///233333335是逆元
return ans;
}
LL quick_mod(LL a, LL b)
{
LL ans = 1;
while(b)
{
if(b & 1)
ans = ans*a%MOD;
b>>=1;
a = a*a%MOD;
}
return ans%MOD;
} int main()
{
isprime();
///Dec(6);
///int x, y;
///Ex_gcd(30,MOD, x, y)
int T;
T = Scan();
while(T--)
{
n = Scan();
Dec(n);
LL res = 0;
for(LL i=1; i<(1<<cnt); i++)
{
LL ans = 1, sum = 0;
for(LL j=0; j<cnt; j++)
{
if(i & (1<<j))
{
sum++;
ans = ans*fac[j]%MOD;
}
}
if(sum&1)
res = (res+Get_ans(n/ans)*quick_mod(ans,4)+MOD)%MOD;
else
res = (res-Get_ans(n/ans)*quick_mod(ans,4)+MOD)%MOD;
}
res = (Get_ans(n)-res+MOD)%MOD;
res=(res%MOD+MOD)%MOD;
printf("%lld\n",res);
}
return 0;
}

HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)的更多相关文章

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

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

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

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

  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 Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...

  5. hdu 4059 The Boss on Mars(纳入和排除)

    http://acm.hdu.edu.cn/showproblem.php?pid=4059 定义S = 1^4 + 2^4 + 3^4+.....+n^4.如今减去与n互质的数的4次方.问共降低了多 ...

  6. hdu 4059 The Boss on Mars 容斥

    题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...

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

  8. hdu4059The Boss on Mars 容斥原理

    //求1到n之间与n互质的数的四次方的和 //segma(n^4) = (6n^5+15n^4+10n^3-n)/30 //对于(a/b)%mod能够转化为(a*inv(b))%mod //inv(b ...

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

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

随机推荐

  1. spy++使用指南

    很多朋友都对窗口句柄比较迷糊,这篇短文就以spy++这个软件为主,介绍下窗体句柄和使用按键插件时,如果对这个句柄发送消息,即所谓的后台挂机.spy++这个软件来自VC++,装好VC后,就可以在工具中看 ...

  2. C#Winform将WebBowser控件替换为Chrome内核

    摘要 由于最近要做一个浏览器式的软件,其中有不少地方需要使用到jQuery和BootStrap,但是在C#中,默认的WebBrowser控件默认使用的是IE的core,而低版本的IE在JS加载上总是容 ...

  3. Pylons安装苦逼之路

    本文介绍一下我在安装pylons的过程中出现的一些错误和解决办法,当然这些都是不完全版. 1.在Serve1(服务器Python版本2.4.3)上面装环境的时候总是出现with_statement有关 ...

  4. Revit API修改链接文件房间边界

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] );//设置房间边界     ...

  5. After 2 years, I have finally solved my "Slow Hyper-V Guest Network Performance" issue. I am ecstatic.

    Edit - It should be known that I was initially researching this issue back in 2012 and the solution ...

  6. 破产姐妹第六季/全集2 Broke Girls迅雷下载

    英文全名2 Broke Girls,第6季(2016)CBS. 本季看点:<破产姐妹>第五季季终集达到了收视人数699万收视率1.7.在第五季终集,两姐妹发现Han(马修·摩伊 饰)的生活 ...

  7. 黑帆第四季/全集Black Sails迅雷下载

    本季看点:<黑帆>本剧是著名海盗故事<金银岛>(Treasure Island)的前传.Starz宣布<Black Sails>已提前获得第四季续订,第四季10集和 ...

  8. 《图解CSS3:核心技术与案例实战》

    <图解CSS3:核心技术与案例实战> 基本信息 作者: 大漠 丛书名: Web开发技术丛书 出版社:机械工业出版社 ISBN:9787111469209 上架时间:2014-7-2 出版日 ...

  9. 《“胡”说IC——菜鸟工程师完美进阶》

    <“胡”说IC——菜鸟工程师完美进阶> 基本信息 作者: 胡运旺 出版社:电子工业出版社 ISBN:9787121229107 上架时间:2014-5-15 出版日期:2014 年5月 开 ...

  10. MSSQL2008 全文索引的创建

    从MSSQL2008开始,全文索引推荐的创建方式已经与2005不同了.对于字符类型的数据库,可以直接创建. CREATE UNIQUE INDEX hr_job_idx ON hr_job_datab ...