牛客小白月赛14 G-many sum

题目链接

筛选因子不能用普通的筛法(这种方法会TLE),要利用筛因子法利用逆向思维,枚举因子的倍数反向加入到i中。

#include
#define ll long long
#define MOD 10e9
#define INF 999999999
const int MAX = 505;
const int INFTY = (1 << 21);
using namespace std;
ll n, ai, m;
ll a[2000006];
ll b[2000006];
int main() {
//  freopen("data.out", "w", stdout);
  scanf("%lld %lld %lld", &n, &ai, &m);
  a[1] = ai;
  for(int i = 2; i <= n; ++i) {
    a[i] = (a[i - 1] + 7 * i) % m;
  }
  for(int i = 1; i <= n; ++i) {
    for(int j = i; j <= n; j += i) 
      b[j] += a[i];
  }
  int tot = 0;
  for(int i = 1; i <= n; ++i) {
    tot ^= b[i];
  }
  printf("%lld\n", tot);
	return 0; 
}

你可能感兴趣的:(ACM)