5197. 【NOIP2017提高组模拟7.3】C
Time Limits:
1000 ms Memory Limits: 262144 KB Detailed Limits
Goto ProblemSet
Description
![](https://i-blog.csdnimg.cn/blog_migrate/7ece57899c8d235103f24e6b0bc5c5b9.png)
Input
![](https://i-blog.csdnimg.cn/blog_migrate/9ac4fceae078974f2fb6e74f75d6ca9c.png)
Output
![](https://i-blog.csdnimg.cn/blog_migrate/045b844f7285a4f2cdb2bd0fa1040912.png)
Sample Input
3
Sample Output
1
Data Constraint
![](https://i-blog.csdnimg.cn/blog_migrate/3fcf8590bbde669e325d03c33f82a807.png)
做法:,因为? = ?时肯定无解,我们不妨设? > ?。 那么有gcd(?, ?) ≤ ? − ?, ? ??? ? ≥ ? − ?,很明显有? = ? − ?。我们依然 枚举?, ? = ? ∗ ?,因为gcd(?, ? − ?) = ?,所以我们只需判断? ??? ? = ? − ?即 可,时间O(? log ?)。
代码如下:
View Code
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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 }