[xsy2282]cake
题意:一个$n\times n$的有标号点阵,现在用一条直线把它们分成两部分,问有多少种不同的分法
结论:方案数就是以点阵上的点为端点且不经过第三个点的线段数
对一个满足要求的线段,将其绕中点顺时针转一个小角度,所在直线是一种切割方案
对于一种分割方案,把直线逆时针旋转直到卡住,卡到的两个点(在原直线的两边且最近的点对)可以连出一条线段
水平竖直的答案为$2n(n-1)$,斜向的答案是$2\sum\limits_{i=1}^n\sum\limits_{j=1}^n(n-i)(n-j)[(i,j)=1]$
后面的sigma可以写成$(n-1)^2+2\sum\limits_{i=1}^n\sum\limits_{j=1}^{i-1}(n-i)(n-j)[(i,j)=1]$
一个结论是$\sum\limits_{i=1}^{n-1}i[(n,i)=1]=\frac{n\varphi(n)}2(n\geq2)$,这是因为在$n\geq3$时如果$(n,i)=1$那么$(n,n-i)=1$,刚好配成$\frac{\varphi(n)}2$对,而在$n=2$时又刚好满足
所以它又可以化成$\sum\limits_{i=2}^n(n-i)\left(n\varphi(i)-\frac{i\varphi(i)}2\right)$,展开后本质是求$\sum\limits_{i=1}^ni^k\varphi(i)$
因为$\sum\limits_{d|n}d^k\varphi(d)\left(\frac nd\right)^k=n^{k+1}$,所以直接杜教筛即可
#include<stdio.h> #include<map> using namespace std; typedef long long ll; const int T=1000000; int p; int mul(int a,int b){return a*(ll)b%p;} int pr[T+10],phi[T+10],phk[T+10][3]; bool np[T+10]; void sieve(){ int i,j,M=0; phi[1]=1; for(i=2;i<=T;i++){ if(!np[i]){ pr[++M]=i; phi[i]=i-1; } for(j=1;j<=M&&i*pr[j]<=T;j++){ np[i*pr[j]]=1; if(i%pr[j]==0){ phi[i*pr[j]]=phi[i]*pr[j]; break; } phi[i*pr[j]]=phi[i]*(pr[j]-1); } } for(i=1;i<=T;i++){ phk[i][0]=(phk[i-1][0]+phi[i])%p; phk[i][1]=(phk[i-1][1]+mul(i,phi[i]))%p; phk[i][2]=(phk[i-1][2]+mul(mul(i,i),phi[i]))%p; } } map<int,int>g; int pre(int k,int n){ if(k==0)return n; if(k==1)return(ll)n*(n+1)/2%p; if(k==2)return(__int128)n*(n+1)*(2*n+1)/6%p; return((ll)n*(n+1)/2%p)*((ll)n*(n+1)/2%p)%p; } int S(int k,int n){ if(n<=T)return phk[n][k]; if(g.count(n))return g[n]; int s=pre(k+1,n),i,nex; for(i=2;i<=n;i=nex+1){ nex=n/(n/i); (s-=mul(pre(k,nex)-pre(k,i-1),S(k,n/i)))%=p; } return g[n]=s; } int getT(int n){ int s=mul(mul(n,n),S(0,n)-1),t; g.clear(); t=mul(S(1,n)-1,3); if(~t&1) t>>=1; else t=mul(t,(p+1)/2); (s-=mul(n,t))%=p; g.clear(); t=S(2,n)-1; if(~t&1) t/=2; else t=mul(t,(p+1)/2); (s+=t)%=p; return s; } int main(){ int n; scanf("%d%d",&n,&p); sieve(); printf("%lld",((mul(2*n,n-1)+2ll*(mul(n-1,n-1)+2ll*getT(n)))%p+p)%p); }
[xsy2282]cake的更多相关文章
- Windows 7上执行Cake 报错原因是Powershell 版本问题
在Windows 7 SP1 电脑上执行Cake的的例子 http://cakebuild.net/docs/tutorials/getting-started ,运行./Build.ps1 报下面的 ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- Scalaz(15)- Monad:依赖注入-Reader besides Cake
我们可以用Monad Reader来实现依赖注入(dependency injection DI or IOC)功能.Scala界中比较常用的不附加任何Framework的依赖注入方式可以说是Cake ...
- uva10167 Birthday Cake
Lucy and Lily are twins. Today is their birthday. Mother buys a birthday cake for them. Now we put t ...
- HDU 4762 Cut the Cake(公式)
Cut the Cake Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Brute Force --- UVA 10167: Birthday Cake
Problem G. Birthday Cake Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudg ...
- 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest, B. Layer Cake
Description Dasha decided to bake a big and tasty layer cake. In order to do that she went shopping ...
- hdu acmsteps 2.1.3 Cake
Cake Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissi ...
- ZOJ 3905 Cake ZOJ Monthly, October 2015 - C
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
随机推荐
- poj 3751 时间日期格式转换
题目链接:http://poj.org/problem?id=3751 题目大意:按照要求的格式将输入的时间日期进行转化. #include <iostream> #include < ...
- delphi按钮文字换行
delphi按钮有TButton和TBitButton,而TButton不支持换行,TBitButton支持 拖拽TBitButton按钮以后,按alt+F12进入找到TBitButton的capti ...
- Exception 和 Error 包结构
- kernel defconfig
Some defconfig files are placed on below path. Only one *_defconfig can be selected. android/kernel/ ...
- Linux时间子系统之八:动态时钟框架(CONFIG_NO_HZ、tickless)【转】
转自:http://blog.csdn.net/droidphone/article/details/8112948 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 数据结 ...
- 菜鸟学习nodejs--Socket.IO即时通讯
https://blog.csdn.net/lovemenghaibin/article/details/51263774
- c++ 引用的分析
在一般教材里面,我们会说引用是变量的别名,另外在 c++ primer 5里面说到引用的时候,说引用不是对象,不能对它进行取地址.但是我们来看看下面代码的分析: #include <iostre ...
- jQuery为多个元素绑定同一个事件
$('.toals,input[type=datetime]').on('focus',function(){ $('.footer-focus-none').css('display','none' ...
- JS实现判断滚动条滚到页面底部并执行事件的方法
需要了解三个dom元素,分别是:clientHeight.offsetHeight.scrollTop. clientHeight:这个元素的高度,占用整个空间的高度,所以,如果一个div有滚动条,那 ...
- 基于UDP套接字编程实例
data.h #ifndef DATA_H #define DATA_H #include <stdio.h> #include <string.h> #include < ...