SRM 596 D2 L3:SparseFactorialDiv2,math
2014-02-11 12:40
246 查看
题目:http://community.topcoder.com/stat?c=problem_statement&pm=12838&rd=15708
参考:http://apps.topcoder.com/wiki/display/tc/SRM+596
F(n) = 0 mod m 等同于 n mod m - k*k mod m = 0. n mod m 只能取 [0, m-1],用 m 个loop,只需考虑k。
代码:
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
/*************** Program Begin **********************/
//long long bestK[1000];
class SparseFactorialDiv2 {
public:
// 计算 [0, uppderbound] 区间内,模divisor值为 d 的数的个数。
long long getC(long long upperbound, long long divisor, long long d)
{
if (upperbound % divisor >= d) {
return upperbound / divisor + 1;
} else {
return upperbound / divisor;
}
}
// 计算 区间 [0, upperbound] 内,F(n) % divisor == 0 的数的个数。
long long calc(long long uppperbound, long long divisor)
{
long long res = 0;
vector <long long> bestK(divisor, -1);
for (long long k = 0; k * k < uppperbound; k++) {
// 找的最小的k, 使 k * k % divisor == d
if (bestK[ (k * k) % divisor ] == -1) {
bestK[ (k * k) % divisor ] = k;
}
}
for (int d = 0; d < divisor; d++) {
if (bestK[d] == -1) {
continue;
}
res += ( getC(uppperbound, divisor, d) - getC(bestK[d] * bestK[d], divisor, d) );
}
return res;
}
long long getCount(long long lo, long long hi, long long divisor) {
return calc(hi, divisor) - calc(lo - 1, divisor);
}
};
/************** Program End ************************/
参考:http://apps.topcoder.com/wiki/display/tc/SRM+596
F(n) = 0 mod m 等同于 n mod m - k*k mod m = 0. n mod m 只能取 [0, m-1],用 m 个loop,只需考虑k。
代码:
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
#define CHECKTIME() printf("%.2lf\n", (double)clock() / CLOCKS_PER_SEC)
/*************** Program Begin **********************/
//long long bestK[1000];
class SparseFactorialDiv2 {
public:
// 计算 [0, uppderbound] 区间内,模divisor值为 d 的数的个数。
long long getC(long long upperbound, long long divisor, long long d)
{
if (upperbound % divisor >= d) {
return upperbound / divisor + 1;
} else {
return upperbound / divisor;
}
}
// 计算 区间 [0, upperbound] 内,F(n) % divisor == 0 的数的个数。
long long calc(long long uppperbound, long long divisor)
{
long long res = 0;
vector <long long> bestK(divisor, -1);
for (long long k = 0; k * k < uppperbound; k++) {
// 找的最小的k, 使 k * k % divisor == d
if (bestK[ (k * k) % divisor ] == -1) {
bestK[ (k * k) % divisor ] = k;
}
}
for (int d = 0; d < divisor; d++) {
if (bestK[d] == -1) {
continue;
}
res += ( getC(uppperbound, divisor, d) - getC(bestK[d] * bestK[d], divisor, d) );
}
return res;
}
long long getCount(long long lo, long long hi, long long divisor) {
return calc(hi, divisor) - calc(lo - 1, divisor);
}
};
/************** Program End ************************/
相关文章推荐
- SRM 576 D2 L3:CharacterBoard2
- SRM 607 D2 L3:CombinationLockDiv2,DP
- SRM 588 D2 L3:GameInDarknessDiv2,DFS
- SRM 596 D2 L2:ColorfulRoad,dp
- SRM 595 D2 L3:LittleElephantAndXor, dp
- SRM 581 D2 L3:TreeUnionDiv2,Floyd算法
- SRM 603 D2 L3:GraphWalkWithProbabilities
- SRM 581 D2 L3:TreeUnionDiv2,Floyd算法
- SRM532 D2 L3
- SRM 606 D2 L3:EllysCandyGame
- SRM 608 D2 L3:BigOEasy,DFS
- SRM 601 D2 L3:WinterAndReindeers,dp
- SRM 588 D2 L3:GameInDarknessDiv2,DFS
- SRM 605 D2 L3: AlienAndSetDiv2,DP
- SRM 596 DIV 2
- SRM 576 D2 L2:ArcadeManao,DFS,善于根据实际问题使用最简便的方法
- SRM 581 D2 L2:SurveillanceSystem,重叠度
- SRM 588 D2 L2:GUMIAndSongsDiv2,冷静思考,好的算法简洁明了
- 【TC SRM 596 DIV1】BitwiseAnd
- SRM 596 1000pt