题意:给一个序列A,要求构造序列B,使得 Bi <= Ai, gcd(Bi) > 1, 1 <= i <= n, 输出构造的方法数。

析:首先这个题直接暴力是不可能解决的,可以先找出最大值mmax和最小值mmin,然后枚举每个gcd,也就是最大公约数d,那么其他数就应该是d 2*d 3*d 4*d ...。如果把每个数的个数求出来。那么答案就是每一种 d 的数量的和,每一种的数量就是 a1 * a2 * a3 ... an,其中是 ai 是 第 i 个数所能取到 d 的数量。

这样做复杂度不但很高,而且还有重复的数,比如当d = 2时,假设所以选的B都是 6,6,6,6,....,6,那么当 d = 3 时或者是 d = 6时,同样还可能选择6,6,6,6,...,6,所以这样的算重了,需要把重复的删除,这里就用到莫比乌斯函数,通过它可以直接得到当d = x时是加上还是减去。

就算是去重了,时间复杂度还是很高,要再次进行优化,对于每个 d,我们可以直接求每个数所能选的最大数数量,并且可能通过区间直接进行求取,比如 t = Ai / d,那么它的范围就是 [t*d, t*d+d-1],这样就能对于每个区间进行处理,把复杂度降到 n*log(n)*log(n),因为还要用一次快速幂。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define FOR(x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r > 0 && r <= n && c > 0 && c <= m;
} int len;
int prime[maxn];
bool vis[maxn];
int mu[maxn];
int cnt[maxn*2]; void init(){
mu[1] = 1;
FOR(2, maxn){
if(!vis[i]){
prime[len++] = i;
mu[i] = -1;
}
for(int j = 0; j < len && i * prime[j] < maxn; ++j){
vis[i*prime[j]] = 1;
if(i % prime[j]) mu[i*prime[j]] = -mu[i];
else{
mu[i*prime[j]] = 0;
break;
}
}
}
} LL fast_pow(LL a, int n){
LL res = 1;
while(n){
if(n&1) res = res * a % mod;
n >>= 1;
a = a * a % mod;
}
return res;
} int main(){
init();
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
ms(cnt, 0);
scanf("%d", &n);
int mmin = maxn, mmax = -1;
FOR(0, n){
int x;
scanf("%d", &x);
++cnt[x];
mmin = min(mmin, x);
mmax = max(mmax, x);
}
for(int i = mmin; i <= mmax*2; ++i) cnt[i] += cnt[i-1];
LL ans = 0;
for(int i = 2; i <= mmin; ++i){
LL tmp = 1;
int t = mmax / i;
for(int j = 2; j <= t; ++j)
tmp = tmp * fast_pow(j, cnt[j*i+i-1] - cnt[j*i-1]) % mod;
ans = (ans - mu[i] * tmp + mod) % mod;
}
printf("Case #%d: %I64d\n", kase, ans);
}
return 0;
}

  

最新文章

  1. Oracle触发器原理、创建、修改、删除
  2. linux建立一个快捷方式,连接到另一个目录
  3. iOS 使用AFN 进行单图和多图上传
  4. 如何做一个脚本自动打开IE浏览器
  5. size_t为何这么重要?
  6. JVM系列五:JVM监测&amp;工具
  7. PHP学习之[第05讲]PHP5.4 循环结构、系统函数和自定义函数
  8. SQL STUFF函数 拼接字符串
  9. hibernate框架学习笔记12:查询优化
  10. Servlet不再是烦恼
  11. POJ 1848 Tree 树形DP
  12. torch分类问题
  13. DeprecationWarning
  14. POJ-1860.CurrencyExchange(Spfa判断负环模版题)
  15. python列表与元组的用法
  16. node.js定时任务 node-schedule
  17. python中的全局变量和局部变量(转)
  18. 一台Windows下配置多个Tomcat服务器
  19. 006-线程同步解决【ReentrantLock】
  20. 今天廷鹏师弟来的java建议

热门文章

  1. ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql (转)
  2. 对Node的优点和缺点提出了自己的看法?
  3. python开发_python中的list操作
  4. leetcode706
  5. setKeepAliveTimeout
  6. LNMP 参数调优 ( 无注释 )
  7. BAT脚本编写教程
  8. PHP中使用CURL模拟文件上传实例
  9. 获取崩溃时的调用栈和生成dump文件,然后自动重启
  10. 75. Sort Colors (Array)