5197. 【NOIP2017提高组模拟7.3】C
Time Limits:
1000 ms Memory Limits: 262144 KB Detailed Limits
Goto ProblemSet
做法:,因为???? = ????时肯定无解,我们不妨设???? > ????。 那么有gcd(????, ????) ≤ ???? − ????, ???? ???????????? ???? ≥ ???? − ????,很明显有???? = ???? − ????。我们依然 枚举????, ???? = ???? ∗ ????,因为gcd(????, ???? − ????) = ????,所以我们只需判断???? ???????????? ???? = ???? − ????即 可,时间O(???? log ????)。
代码如下:
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 int n; 6 long long ans; 7 8 int gcd(int x, int y) 9 { 10 while (y != 0) 11 { 12 int tmp = x; 13 x = y; 14 y = tmp % y; 15 } 16 return x; 17 } 18 19 int main() 20 { 21 scanf("%d", &n); 22 for (int i = 1; i <= n / 2 ; i++) 23 { 24 for (int j = 2; i * j <= n; j++) 25 if (i * (j - 1) == (i ^ (i * j))) ans++; 26 } 27 cout << ans; 28 }