热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

hdu4059TheBossonMars(差分+容斥原理)

题意:求小于n(1≤n≤10^8)的数中,与n互质的数的四次方和。知识点:差分:一阶差分:设则

题意:

求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和。

 

知识点:

    差分:

         一阶差分: 设  image

                         则  image  为一阶差分。

         二阶差分:imageimage

                                       image

                                       image

           n阶差分:  image   且可推出    image

        

         性质: 1. image

 

                       2.image

 

    差分序列:

         给你一列数 a[i][1],a[i][2],a[i][3],a[i][4],a[i][5]……

         那么a[i][j]=a[i-1][j+1]-a[i-1][j], 即后一行是上一行相邻两项的差(第一行除外)。

         如果给你一个多项式, 比如 f(x)=(x+1)*(x+2)*……*(x+p),即多项式最高项指数为p。

         则得到的差分序列有如下性质:

         1. f(0),f(1)…f(p)组成多项式的第一行,后面的差分序列可以由上一行推出。第p+1行以后差分序列的值都为0。

         2.我们这里要用的差分序列是其第0行对角线的数。 我们设他们为c0、c1、c2、……cp;   则:

            第n项的值:f(n)=c0*C(n,0)+c1*C(n,1)+c2*C(n,2)+……+cp*C(n,p);

            前n项的值:Sum(n)=c0*C(n+1,1)+c1*C(n+1,2)+c2*C(n+1,3)+……+cp*C(n+1,p+1);

            把求前n项和组合公式给化简出来Sum(n)=(n^5)/5+(n^4)/2+(n^3)/3-n/30

                                                                       =(n*(n+1)*(2n+1)*(3*n*n+3*n-1))/30

                  参考文献

 

题解:反面考虑,容斥原理,sum(n)=1^4+2^4+…n^4=(n*(n+1)*(2n+1)*(3*n*n+3*n-1))/30,减掉与n不互质的数4次方,将n质因子分解后减掉一个因子的倍数的4次方结果,加上两个因子乘积的倍数的4次方结果,减去……以此类推。

其中还涉及逆元,因为MOD为素数,用费马小定理求。

 

#include
#include
#include
using namespace std;
typedef long long LL;
const LL MOD=1e9+7;
const LL NN=1e8+5;
const int N=1e4+5;
const LL ni=233333335; //30 mod MOD 的逆
LL n,syz[15],ans;
int ycnt;

LL mutisum(LL n)
{
LL ans1=1;
//long long 范围<18,446,744,073,709,551,616 约10^20 30*MOD> LL范围,此法不可,实力被坑
//LL mod=30*MOD;
//ans1=(((((n*(n+1))%mod)*(2*n+1))%mod)*((3*n*n+3*n-1)%mod))%mod;
ans1=(((((((n*(n+1))%MOD)*(2*n+1))%MOD)*((3*n*n+3*n-1)%MOD))%MOD)*ni)%MOD;
//ans1/=30;
return ans1%MOD;
}

int prime[N];
bool vis[N];
int pcnt;
void is_prime()
{
pcnt=0;
memset(vis,0,sizeof(vis));
for(int i=2;i {
if(!vis[i])
{
prime[pcnt++]=i;
for(int j=i+i;j vis[j]=1;
}
}
}

void fenjie(LL n1)
{
ycnt=0;
for(int i=0;i {
if(n1%prime[i]==0)
syz[ycnt++]=prime[i];
while(n1%prime[i]==0)
n1/=prime[i];
}
if(n1>1)
syz[ycnt++]=n1;
}

void dfs(int c,int cur,int j,LL ans1) //dfs(c,1,0,1);
{
if(cur==c+1)
{
LL nn=(n-1)/ans1,as1=ans1%MOD;
if(c&1)
ans-=(((((((mutisum(nn)*as1)%MOD)*as1)%MOD)*as1)%MOD)*as1)%MOD;
else
ans+=(((((((mutisum(nn)*as1)%MOD)*as1)%MOD)*as1)%MOD)*as1)%MOD;
ans%=MOD;
return;
}
for(;j {
dfs(c,cur+1,j+1,ans1*syz[j]);
}
}

void test()
{
for(int i=0;i cout< cout<
}

int main()
{
int t;
scanf("%d",&t);
is_prime();
while(t--)
{
scanf("%lld",&n);
if(n==1)
{
printf("1\n");
continue;
}
fenjie(n);
ans=mutisum(n-1);
for(int c=1;c<=ycnt;c++)
dfs(c,1,0,1);
if(ans<0)
ans=(ans+MOD)%MOD;
printf("%lld\n",ans);
// test();
}
}




The Boss on MarsTime Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64uSubmit Status Practice HDU 4059

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

245  

Sample Output

82354

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 

推荐阅读
  • [USACO 2006 November Gold] 玉米地Corn Fields
    题目描述  FarmerJohn新买了一块长方形的牧场,这块牧场被划分成M行N列(1<M<12;1<N<12),每一格都是一块正方形的土地。FJ打 ... [详细]
  • 题目链接:杭电多校7-VirtualJudgevjudge上题目显示的有问题,我下面附上官方题目:样例输入:32201 ... [详细]
  • 题目描述输入整型数组和排序标识,对其元素按照升序或降序进行排序(一组测试用例可能会有多组数据)本题有多组输入,请使用whil ... [详细]
  • PIMPL 是 C++ 中的一个编程技巧,意思为指向实现的指针。具体操作是把类的实现细节放到一个单独的类中,并用一个指针进行访问 ... [详细]
  • 开发笔记:sql盲注之报错注入(附自动化脚本)
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了sql盲注之报错注入(附自动化脚本)相关的知识,希望对你有一定的参考价值。 ... [详细]
  • DFS基本概念步骤优缺点典型例题递推基本概念直接或者间接调用自身的算法称为递归算法一般数据n ... [详细]
  • 1.File类:文件和目录路径名的抽象表现形式2.创建对象:File(Stringpathname)通过给定的路径创建文件对象File(Stringpa ... [详细]
  • FroggerTimeLimit:1000MSMemoryLimit:65536KTotalSubmissions:32257Accepted:10396DescriptionFr ... [详细]
  • 九宫格计算. ... [详细]
  • 题意给出一个长度为n的序列,有一些位置可以放任意的数,问最长上升序列的长度。n ... [详细]
  • 在这一期的SendMessage函数应用中,我将向大家介绍如何利用消息函数来扩展树型列表(TreeView)控件的功能相信对于树型列表控件大家十分的熟悉, ... [详细]
  • IDEA实用插件Lombok
    LombokLombok是一个可以通过简单的注解形式来帮助我们简化消除一些必须有但显得很臃肿的Java代码的工具,通过使用对应的注解,可以在编译源码的时候生成对应的方法。通常,我们所定义的对象和b ... [详细]
  • 使用RSACryptoServiceProvider进行公钥加密我已经在CodeProject上发表了一篇文章,解释了如何使用RSA提供程序进行加密和解密:RSA私钥加密虽然200 ... [详细]
  • 此题有一个大坑id范围为1e9此题题意是按照同类按照价格大小从大到小输出,如果价格相等再按照id从小到大输出。​#includeusin ... [详细]
  • rbac 4表 常规设计
    rbac4表常规设计设计模型:1、管理员表(users)Schema::create('users',function(Blueprint$table){$tabl ... [详细]
author-avatar
手机用户2502938557
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有