已知 f(n)=∑0<=i 求 g(n)=∑m|nf(m) , n<=109
f(n)=n2?∑i∑j[ij(modn)=0]=n2?∑d|n?(n/d)d
g(n)=∑m|nf(m)=∑m|n[m2?∑d|m?(m/d)d]
后面部分交换求和顺序就是 ∑d|nd∑md|nd?(m/d)=∑d|nd?nd=n∑d|n=nτ(n)
最后 g(n)=∑m|n(m2?n)
然后这题 O(sqrt(n)) 过不去,所以要预处理质数表
【HDU 5528(Count a * b-反演)】 n=∏i=1npkii
则
g(n)=∏i=1n1?p2(ai+1)i1?p2i?n∏i=1n(ki+1)
#include
#include
using namespace std;
#define For(i,n) for(int i=1;
i<=n;
i++)
#define Fork(i,k,n) for(int i=k;
i<=n;
i++)
#define Rep(i,n) for(int i=0;
i=k;
i--)
#define RepD(i,n) for(int i=n;
i>=0;
i--)
#define Forp(x) for(int p=Pre[x];
p;
p=Next[p])
#define Forpiter(x) for(int &p=iter[x];
p;
p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector
#define pi pair
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case %d: %lld\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1) cout<' ';
\
coutn) break;
b[i*p[j]]=1;
if (i%p[j]==0) break;
}
}
}
int main()
{
//freopen("hdu5528.in","r",stdin);
//freopen(".out","w",stdout);
make_prime((int)sqrt(1e9+0.5) );
int T=read();
while(T--) {ll n;
ll ans;
ans=1;
n=read();
ll tou=n;
//nh(n)
For(i,tot) {
int t=p[i];
if (t*t>n) break;
if (n%t) continue;
int cnt=1;
ll mul = t;
while(n%t==0) ++cnt,mul*=t,n/=t;
tou*=cnt;
ll a=(mul-1)/(t-1),b=mul+1,c=t+1;
//ans=a*b/c;
写法1
//ans*=(a/c)*(b/c)*c+a%c*(b/c)+b%c*(a/c);
写法1的防溢出
if (a%c==0) ans*=a/c*b;
//这个更直观
else ans*=b/c*a;
} if (n>1) {
tou*=2;
ans*=(1+n*n);
} cout
推荐阅读