A. Multiples of 3 and 5
大水题,计算出below n内是3,5倍数的总和
数据较大,只能用求和公式
#include <iostream> #include <cstdio> typedef long long ll; using namespace std; int main() { int T; scanf("%d",&T); while(T--){ int n; scanf("%d",&n); ll sum=0; ll p; p = (n-1)/3; sum += (1LL)*3*(p+1)*p/2; p = (n-1)/5; sum += (1LL)*5*(p+1)*p/2; p = (n-1)/15; sum -= (1LL)*15*(p+1)*p/2; cout<<sum<<endl; } //cout << "Hello world!" << endl; return 0; }
时间: 2025-01-04 23:23:24