以下转自于:
http://blog.sina.com.cn/s/blog_59e67e2c0100a84o.html
题目意思不难已知给定k,x,y求 1<=a<=x 1<=b<=y
/*
* hdu1695.c
*
* Created on: 2011-10-3
* Author: bjfuwangzhu
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#define LL long long
#define nmax 100010
int prime[nmax], phi[nmax], pfactor[nmax], cpfactor[nmax];
int plen, pflen;
void init() {
int i, j;
memset(phi, 0, sizeof(phi));
phi[0] = 0, phi[1] = 1;
for (i = 2, plen = 0; i < nmax; i++) {
if (!phi[i]) {
phi[i] = i - 1;
prime[plen++] = i;
}
for (j = 0; (j < plen) && (i * prime[j] < nmax); j++) {
if (i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j];
break;
} else {
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
}
void getpFactor(int n) {
int i, te;
te = (int) sqrt(n * 1.0);
for (i = 0, pflen = 0; (i < plen) && (prime[i] <= te); i++) {
if (n % prime[i] == 0) {
cpfactor[pflen] = 0;
while (n % prime[i] == 0) {
n /= prime[i];
cpfactor[pflen]++;
}
pfactor[pflen++] = prime[i];
}
}
if (n > 1) {
cpfactor[pflen] = 1;
pfactor[pflen++] = n;
}
}
LL dfs(int n, int start) {
int i;
LL res;
for (i = start, res = 0; i < pflen; i++) {
res += n / pfactor[i] - dfs(n / pfactor[i], i + 1);
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int t, i, j, a, b, c, d, k;
LL res;
init();
scanf("%d", &t);
for (i = 1; i <= t; i++) {
scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
b /= a, d /= c;
if (k == 0 || k > b || k > d) {
res = 0;
} else {
b /= k, d /= k;
if (b > d) {
b ^= d, d ^= b, b ^= d;
}
for (j = 1, res = 0; j <= b; j++) {
res += phi[j];
}
for (j = b + 1; j <= d; j++) {
getpFactor(j);
res += b - dfs(b, 0);
}
}
printf("Case %d: %I64d\n", i, res);
}
return 0;
}
ural 1091. Tmutarakan Exams
http://acm.timus.ru/problem.aspx?space=1&num=1091
/*
* timus1091.c
*
* Created on: 2011-10-6
* Author: bjfuwangzhu
*/
#include<stdio.h>
#define pnum 15
#define nmax 10000
int prime[pnum] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
int mark;
int solve(int n, int k, int m) {
int te, i, res;
te = n / m;
if (k > te) {
return 0;
}
if (k > te / 2) {
k = te - k;
}
for (i = 0, res = 1; i < k; i++) {
res = res * (te - i) / (i + 1);
if (res >= nmax) {
return -1;
}
}
return res;
}
int dfs(int start, int s, int k) {
int i, res, temp;
for (i = start, res = 0; (i < pnum) && (prime[i] <= s); i++) {
temp = solve(s, k, prime[i]);
if (temp == -1) {
mark = 1;
return 0;
} else {
res += temp - dfs(i + 1, s / prime[i], k);
}
if (res >= nmax) {
mark = 1;
return 0;
}
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int k, s, res;
while (~scanf("%d %d", &k, &s)) {
mark = 0;
res = dfs(0, s, k);
if (mark) {
res = nmax;
}
printf("%d\n", res);
}
return 0;
}