此题要优化优化再优化,关键在于要求出一个数列(其中每一个数分解后所包含的质因数的指数为1),反正我是这么做的,这样后面可以直接调用预处理的结果(所包含的质因数种类数为奇数则减去,否则加上) spoj 4168 也是用类似的方法做的,当然那题用dfs() 递归计算会更快,不过我还是坚持自己最初的想法,至少更好理解

#include <cstdio>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 1111111;
const int mod =  1000000007;
template<typename T>
T gcd(T a,T b)
{return b?gcd(b,a%b):a;
}
ll ans=1,tmp;
int limit = 1000007;
int a[maxn];
int p[1111],size;
ll c[maxn];
int pre[maxn],o;
bool vis[maxn];
int cnt;
void gao(int x,int cnt,ll m,ll n)
{if(x<0){if(cnt&1)tmp-=n/m;elsetmp+=n/m;}else{gao(x-1,cnt,m,n);if(m*p[x]<=n)gao(x-1,cnt+1,m*p[x],n);}
}
int n,m;
void dfs(int x)
{if(vis[x]) return;vis[x]=true;cnt++;for(int i=0;i<size;++i){if(1ll*x*p[i]<=m)dfs(x*p[i]);}
}
void spilt(int n)
{size=0;int pre=-1,k=n;while(k>1){//cout<<k<<endl;if(a[k]!=pre){pre=a[k];p[size++]=a[k];}k/=a[k];}
}
void init()
{int i,j;for(i=1;i<=1011111;++i) a[i]=i;for(i=2;i<=1011111;++i){if(a[i]==i){for(j=2*i;j<=1011111;j+=i){a[j]=i;//cout<<j<<endl;}}}
}
int d[1111111],tot;
int num[1111111];
int main() {init();int T,cas,i;m=1011111;for(i=1;i<=1011111;++i){if(vis[i]) continue;o=i;spilt(i);d[tot]=i;num[tot++]=size;dfs(i);}cnt=0;scanf("%d",&T);for(cas=1;cas<=T;++cas){ans=0;scanf("%d%d",&n,&m);if(n<m) swap(n,m);if(n==0||m==0){ans=1;if(n==0&&m==0) ans=0;printf("Case %d: %lld\n",cas,ans);continue;}ans=1ll*n*m;for(i=1;d[i]<=m&&i<tot;++i){cnt=m/d[i];// 这里表示能整除d[i]的整数个数if(num[i]&1) ans-=1ll*cnt*(n/d[i]);else ans+=1ll*cnt*(n/d[i]);}printf("Case %d: %lld\n",cas,ans+2);}return 0;
}

SPOJ 4168 对于每个n,求1到n中不能被完全平方数整除的数的个数(话说就冲这时限来说,spoj的服务器也该换了)

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<iostream>
#include<bitset>
using namespace std;
typedef long long ll;
const int maxn = 10011111;
const int N = 10000111;
bitset<maxn>vis;
int a[maxn];
int pre[maxn];
int num[7000000],o,tot;
int cnt[7000000];
int limit = 10000111;
void init()
{int i,j;for(i=1;i<=10001111;++i) a[i]=i;for(i=2;i<=10001111;++i){if(a[i]==i)for(j=2*i;j<=10001111;a[j]=i,j+=i);}
}
int p[1111],size;
void spilt(int n)
{size=0;int pre=-1,k=n;while(k>1){if(pre!=a[k])p[size++]=a[k];k/=a[k];}
}
void dfs(int n)
{if(vis[n])  return;vis[n]=true;pre[n]=o;for(int i=0;i<size;++i){if(1ll*n*p[i]<=limit)dfs(n*p[i]);}
}
int main()
{init();int i,j;for(i=2;i<=10000111;++i){if(vis[i]) continue;o=i;spilt(i);num[tot]=i;cnt[tot++]=size;dfs(i);}//cout<<tot<<endl;int T;scanf("%d",&T);ll n,t,ans,k;while(T--){scanf("%lld",&n);ans=n;for(i=0;i<tot&&1ll*num[i]*num[i]<=n;++i){t=1ll*num[i]*num[i];//表示能整除 num[i]^2 的整数个数(完全平方数)//cout<<num[i]<<" "<<cnt[i]<<endl;k=n/t;if(cnt[i]&1)ans-=k;elseans+=k;}printf("%lld\n",ans);}return 0;
}

light 1144 spoj 4168相关推荐

  1. 数学--数论--容斥定理完全解析(转)

    对容斥原理的描述 容斥原理是一种重要的组合数学方法,可以让你求解任意大小的集合,或者计算复合事件的概率. 描述 容斥原理可以描述如下: 要计算几个集合并集的大小,我们要先将所有单个集合的大小计算出来, ...

  2. π-Algorithmist分类题目(3)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(3) Probability ...

  3. 【转载】容斥原理的证明及应用

    翻译:vici@cust 对容斥原理的描述 容斥原理是一种重要的组合数学方法,可以让你求解任意大小的集合,或者计算复合事件的概率. 描述 容斥原理可以描述如下: 要计算几个集合并集的大小,我们要先将所 ...

  4. SPOJ 7259 Light Switching (水题,区间01取反)

    #include <iostream> #include <stdio.h> #include <algorithm> #define lson rt<< ...

  5. bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)

    Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MB Submit: 7669  Solved: 1894 [Sub ...

  6. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

    全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...

  7. 巧用CSS的Light滤镜

    作者: 冯永曜 Light滤镜能产生一个模拟光源的效果,但使用它要通过调用它的"方法(Method)"来实现,这就要用到一些Javascrpt知识,虽然有一点难度,但产生的效果也是 ...

  8. MVVM开发模式MVVM Light Toolkit中使用事件和参数传递

    Light中定义了类GalaSoft.MvvmLight.Command.RelayCommand 这个类继承了ICommand方法,实现了其中的方法,Action就是一个方法参数 // 摘要: // ...

  9. SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并

    Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...

最新文章

  1. Install zabbix 2.2 + PostgreSQL + nginx + php on CentOS 6.x x64
  2. 在Java中调用Python
  3. 百度地图离线_“高德地图”和“百度地图”有什么差别? 专家: 细节决定成败!...
  4. 单片机c语言强制数据类型,写单片机程序数据类型相关
  5. LeetCode 1059. 从始点到终点的所有路径(回溯)
  6. 利用curl验证ssl网站(webservice)
  7. Linux学习笔记-最基础的常用shell命令
  8. Weave Scope 容器地图 - 每天5分钟玩转 Docker 容器技术(80)
  9. HTML的基本知识-和常用标签-以及相对路径和绝对路径的区别
  10. python提供了两种基本的数值类型_python数据分析(一) python当中的数据类型--数字和常用函数...
  11. STM32串口通信(使用C8T6)
  12. 同态滤波 matlab代码,同态滤波处理光照不均匀图像Matlab代码
  13. VS 2017安装教程
  14. 小米市场魅力所在?你读懂小米了么?
  15. 在html标签中写alert,文本不会写入页面,但会显示在alert()消息中。 JavaScript和HTML...
  16. 将页面表格导出为excel并下载
  17. 网页收藏栏小图标_如何设置在网页地址栏中的小图标
  18. 工作态度-《精要主义》书中的精髓:我们如何践行精要主义,摆脱拥挤不堪的生活?
  19. jeremiah(jeremiah wright简介)
  20. Python实战:如何隐藏自己的爬虫身份

热门文章

  1. Python--方法或函数重试补偿机制
  2. 计算机显卡维修,维修常见显卡花屏故障(图解)
  3. [笔记]Windows核心编程《番外篇》几种常见的执行命令行方法
  4. 30 个免费的 UI 工具箱
  5. [小说连载]张小庆,在路上(1)- 找工作
  6. 【kubernetes-工具篇】K9S详解-宝藏k8s界面工具
  7. Windows电脑如何更改账户类型
  8. 【Animate】Actionscript3.0脚本制作下雨效果
  9. c++类内的static变量初始化和static函数
  10. Web自动化测试入门