hdu2841 Visible Trees(容斥)
来源:互联网 发布:win7引导linux 编辑:程序博客网 时间:2024/05/16 05:22
There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
21 12 3
15
该题目怎么分析呢?
我们最终可以得出是和行数互质的情况会被保留,为什么?
我们来举个例子:
(2,3)
(6,9)
(2,3)互质,在第二行第三列
(6,9)不互质,在第六行第九列
由于你人站在(0,0)在同一直线上的点只能看见一个,所以(6,9)被(2,3)挡住了
所以不互质的点都被互质的点挡住了
代码:
#include <iostream>using namespace std;typedef long long ll;const ll maxn=100000+10;ll prime[maxn];ll fac[maxn];//表示和n互质的范围为(1~x)的有多少个 ll solve(ll n,ll m){ ll num=0; ll temp_n=n; for(ll i=2;i*i<=n;i++){ if(temp_n%i==0){ prime[num++]=i; while(temp_n%i==0){ temp_n/=i; } } } if(temp_n!=1)prime[num++]=temp_n; ll cnt=0; fac[cnt++]=1; for(ll i=0;i<num;i++){ ll temp_cnt=cnt; for(ll j=0;j<temp_cnt;j++) fac[cnt++]=fac[j]*prime[i]*-1; } ll ans=0; for(ll i=0;i<cnt;i++){ ans+=m/fac[i]; } return ans;}int main(){ ll T; cin>>T; while(T--){ ll m,n; cin>>m>>n; ll ans=n; for(ll i=2;i<=m;i++){ ans+=solve(i,n);// cout<<i<<" "<<solve(i,n)<<endl; } cout<<ans<<endl; } return 0;}
阅读全文
0 0
- hdu2841 Visible Trees(容斥原理)
- hdu2841 Visible Trees(容斥原理)
- hdu2841 Visible Trees(容斥)
- HDU2841 Visible Trees (容斥原理)
- HDU2841 Visible Trees【容斥原理】
- 【容斥原理】HDU2841 Visible Trees
- hdu2841(容斥原理+分解质因数)Visible Trees
- Visible Trees HDU2841(容斥定理的简单扩展)
- Hdu2841 Visible Trees
- Visible Trees 【容斥】
- Visible Trees(容斥原理)
- HDU2841:Visible Trees(莫比乌斯函数)
- HDU 2841 Visible Trees (数论,容斥原理)
- hdu 2841 Visible Trees(容斥定理)
- hdoj 2841Visible Trees(容斥原理)
- HDU 2841-Visible Trees(容斥原理)
- HDU 2841 Visible Trees(容斥定理)
- hdoj Visible Trees 2841 (容斥原理)
- CLion 输出遇到乱码解决办法
- 为啥redis快?
- int、long、long long取值范围
- 注册表脚本
- ios 原生 social.framework 分享
- hdu2841 Visible Trees(容斥)
- 739. Daily Temperatures
- springcloud架构搭建(二) Zuul路由服务器搭建及配置
- Python3之创建类的两种方式
- 图片抖动轮播
- Mr.Smile填坑记——Android Studio运行Java代码,Run控制台中文乱码
- 转载】MySQL数据库连接超时(wait_timeout)问题的处理
- 安装Tomcat(win10)
- Intellij IDEA中java和scala开发流程