HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2494 Accepted Submission(s): 775
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.
2
4
5
82
354这道题目是求小于n和n互质的数的四次方的和利用容斥原理,可以用总和减去不和n互质的和也可以直接求互质的数的和这里还涉及到了sum{1^4,2^4,3^4....n^4}求和公式n*(n+1)*(2*n+1)*((3*n*n+3*n-1)/30求和公式里有/ 那么在用同余定理的时候就要用逆元b/c(mod m)=b(mod m)* c^(m-2)(mod m)证明略#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h> using namespace std;
typedef long long int LL;
#define MAX 1000000
LL prime[MAX+5];
LL sprime[MAX+5];
bool check[MAX+5];
const LL mod=1e9+7;
LL INF;
LL cnt;
LL quick(LL x,LL a)
{
LL sum=1;
for(x;x>0;x>>=1)
{
if(x&1)
sum=(sum*a)%mod;
a=(a*a)%mod;
}
return sum;
}
LL sum1(LL n)
{
INF=quick(mod-2,30);
return n*(n+1)%mod*(2*n+1)%mod*((3*n*n%mod+3*n-1)%mod)%mod*INF%mod;
}
LL sum2(LL n)
{
return (((n*n)%mod*n)%mod*n)%mod;
}
void eular()
{
memset(check,false,sizeof(check));
int tot=0;
for(LL i=2;i<MAX+5;i++)
{
if(!check[i])
prime[tot++]=i;
for(int j=0;j<tot;j++)
{
if(i*prime[j]>MAX+5) break;
check[i*prime[j]]=true;
if(i%prime[j]==0) break;
}
}
}
void Divide(LL n)
{
cnt=0;
LL t=(LL)sqrt(n*1.0);
for(LL i=0;prime[i]<=t;i++)
{
if(n%prime[i]==0)
{
sprime[cnt++]=prime[i];
while(n%prime[i]==0)
n/=prime[i];
}
}
if(n>1)
sprime[cnt++]=n;
}
int main()
{
eular();
int t;
scanf("%d",&t);
while(t--)
{
LL n;
scanf("%lld",&n);
Divide(n); LL ans=0;
LL res=sum1(n);
for(int i=1;i<((LL)(1<<(cnt)));i++)
{
int num=0;
LL tmp=1;
for(int j=0;j<cnt;j++)
{
if(i&(1<<j))
{
num++;
tmp*=sprime[j];
}
}
if(num&1)
ans=(ans+(sum1(n/tmp)*sum2(tmp))%mod)%mod;
else
ans=((ans-(sum1(n/tmp)*sum2(tmp))%mod)%mod+mod)%mod;
}
printf("%lld\n",(res-ans+mod)%mod);
}
return 0;
}
HDU 4059 The Boss on Mars(容斥原理)的更多相关文章
- HDU 4059 The Boss on Mars 容斥原理
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)
传送门 The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 4059 The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 数论 + 容斥 - HDU 4059 The Boss on Mars
The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...
- 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次方.问共降低了多 ...
- hdu 4059 The Boss on Mars 容斥
题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...
- 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 ...
- hdu 4059 数论+高次方求和+容斥原理
http://acm.hdu.edu.cn/showproblem.php? pid=4059 现场赛中通过率挺高的一道题 可是容斥原理不怎么会.. 參考了http://blog.csdn.net/a ...
- 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 ...
随机推荐
- laravel的模板嵌套循环可以这样调试
@foreach($first as $value) {{ dump($value) }} @endforeach
- offsetof宏的实现
1.c语言的结构体中,因为字节对齐的问题,导致成员地址并不能根据类型的大小进行计算.例如: struct test { char ch; int a; } printf("test的大小=% ...
- linux下创建用户(转)
转自 http://www.cnblogs.com/ylan2009/articles/2321177.html Note: 1, Linux Shell 按Tab键不能补全 发现使用新增的用户登陆的 ...
- Nginx日志过滤 使用ngx_log_if不记录特定日志
ngx_log_if是Nginx的一个第三方模块.它在Github上的描述是这样介绍的:ngx_log_if是一个独立的模块,允许您控制不要写的访问日志,类似于Apache的"CustomL ...
- layui点击table表格的每一格时显示相应的内容
$(document).on('click','.layui-table-cell',function(){ // $("p").css({"background-col ...
- mybatis expected at least 1 bean which qualifies as autowire candidate for this dependency
错误原因:没有引入相应mapper接口,导致spring没有找到依赖 解决方法一:使用注解的方法: 首先在spring配置文件中添加 <bean class="org.mybatis. ...
- 常用的几个linux命令
linux 命令众多,特别是每个命令后面的option更是很多,如果不经常使用,就容易忘记.下面是一些常用的命令和参数.其他不常用的,可以用help去现查现用. 1. 最常用的命令列表 下面列出几个在 ...
- Xampp + Zend Studio + xDebug 环境搭建 (Mac,Windows都适用)
这几天折腾了一下PHP开发环境的搭建,现总结一下安装步骤: 1. 安装 Zend Studio,然后破解. 2. 安装 Xampp 3. 配置 Xampp 3.1 配置 Apache服务端 ...
- HLJU 1042 Fight (种类并查集)
1042: Fight Time Limit: 1 Sec Memory Limit: 128 MB Submit: 26 Solved: 8 [Submit][Status][pid=1042& ...
- 在linux下安装配置rabbitMQ详细教程
在linux下安装配置rabbitMQ详细教程 2017年12月20日 17:34:47 阅读数:7539 安装Erlang 由于RabbitMQ依赖Erlang, 所以需要先安装Erlang. Er ...