hdu6053 TrickGCD 莫比乌斯反演
2017-07-28 18:25
330 查看
TrickGCD
Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Problem Description
You are given an array A ,
and Zhu wants to know there are how many different array B satisfy
the following conditions?
* 1≤Bi≤Ai
* For each pair( l , r ) (1≤l≤r≤n)
, gcd(bl,bl+1...br)≥2
Input
The first line is an integer T(1≤T≤10)
describe the number of test cases.
Each test case begins with an integer number n describe the size of array A.
Then a line contains n numbers
describe each element of A
You can assume that 1≤n,Ai≤105
Output
For the kth
test case , first output "Case #k: " , then output an integer as answer in a single line . because the answer may be large , so you are only need to output answer mod 109+7
Sample Input
1
4
4 4 4 4
Sample Output
Case #1: 17
Source
2017 Multi-University Training Contest - Team 2
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
new Task().solve();
}
}
class Task {
InputReader in = new InputReader(System.in) ;
PrintWriter out = new PrintWriter(System.out) ;
final long Mod = 1000000007L ;
final int N = 100000 ;
int[] mu = new int[N+1] ;
int[] prime = new int[N+1] ;
boolean[] vis = new boolean[N+1] ;
{
int cnt = 0 ;
Arrays.fill(vis, false);
mu[1] = 1 ;
for(int i = 2 ; i <= N ; i++){
if(! vis[i]){
prime[cnt++] = i ;
mu[i] = -1 ;
}
for(int j = 0 ; j < cnt && i * prime[j] <= N ; j++){
vis[i * prime[j]] = true ;
if(i % prime[j] == 0){
mu[i * prime[j]] = 0 ;
break ;
}
mu[i * prime[j]] = -mu[i] ;
}
}
}
long pow(long x , int y){
long s = 1 ;
for(; y > 0 ; y >>= 1){
if((y & 1) > 0){
s *= x ;
s %= Mod ;
}
x *= x ;
x %= Mod ;
}
return s ;
}
void solve(){
int t = in.nextInt() ;
for(int cas = 1 ; cas <= t ; cas++){
int[] sum = new int[2*N+1] ;
int _min = Integer.MAX_VALUE ;
int _max = Integer.MIN_VALUE ;
long all = 1 ;
Arrays.fill(sum, 0) ;
int n = in.nextInt() ;
int[] a = new int[n+1] ;
for(int i = 1 ; i <= n ; i++){
a[i] = in.nextInt() ;
_min = Math.min(_min , a[i]) ;
_max = Math.max(_max, a[i]) ;
sum[a[i]]++ ;
all *= a[i] ;
all %= Mod ;
}
for(int i = 1 ; i <= 2*N ; i++){
sum[i] += sum[i-1] ;
}
long _all = 0 ;
for(int d = 1 ; d <= _min ; d++){
long h = (mu[d] + Mod) % Mod ;
for(int g = 1 ; g <= _max/d ; g++){
int cnt = sum[d*(g+1)-1] - sum[d*g-1] ;
h *= pow(g, cnt) ;
h %= Mod ;
}
_all += h ;
_all %= Mod ;
}
all -= _all ;
all %= Mod ;
all = (all + Mod) % Mod ;
out.println("Case #"+ cas + ": " + all) ;
}
out.flush() ;
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = new StringTokenizer("");
}
private void eat(String s) {
tokenizer = new StringTokenizer(s);
}
public String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextInts(int n) {
int[] nums = new int
;
for (int i = 0; i < n; i++) {
nums[i] = nextInt();
}
return nums;
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
相关文章推荐
- hdu6053 TrickGCD
- hdu 6053 TrickGCD(莫比乌斯反演)
- hdu6053_TrickGCD_莫比乌斯反演_快速幂优化_前缀和
- 【HDU 6053 TrickGCD】 + 莫比乌斯反演
- HDU6053 TrickGCD(莫比乌斯函数)
- 解题报告:HDU_6053 TrickGCD 莫比乌斯反演
- 【2017多校】HDU6053 TrickGCD 【莫比乌斯】
- HDU 6053 TrickGCD(莫比乌斯反演)
- 2017多校训练赛第二场 HDU 6053 TrickGCD(容斥原理/莫比乌斯反演)
- HDU6053 TrickGCD(容斥原理)
- HDU 6053 TrickGCD(莫比乌斯反演+前缀和)
- HDU6053-TrickGCD 容斥原理+莫比乌斯反演
- HDU6053 TrickGCD(2017多校第2场)
- HDU 6053 TrickGCD (桶装+分段 / 莫比乌斯反演)
- HDU6053 TrickGCD【数学】
- hdu6053 TrickGCD 莫比乌斯函数 容斥原理
- hdu6053 TrickGCD 容斥原理
- HDU 6053 TrickGCD(莫比乌斯反演)
- HDU 6053 TrickGCD(莫比乌斯反演)
- [莫比乌斯函数][分段] hdu6053 TrickGCD (2017 Multi-University Training Contest - Team 2)