codeforces|Codeforces Round #665 (Div. 2) A. Distance and Axis(思维,数学)

题目传送
题意:
给你一个点A(n,0),现在要求你找到一个整数x,使得abs(x - (abs(n-x)) == k,给出n和k,现在你可以进行操作n+1,或者n-1多次,问最少多少次操作使得有非负整数x的存在
【codeforces|Codeforces Round #665 (Div. 2) A. Distance and Axis(思维,数学)】思路:
1.当n都小于k的时候,那么在n前面找一个点使得上述等式成立那是必不可能的,所以我们只有将n一直加到k,然后x选择0即可
2.当n大于x的时候,有等式x - (n-x) == k,(因为是对称的,所以这里我们直接假设x大于(n-k))那么化简得到x = (k+n)/2,要x为整数,当(k+n)%2== 0时不需要操作,当不为0时,只需要将n+1或者n-1一次即可
AC代码

#include inline int read(){char c = getchar(); int x = 0,s = 1; while(c < '0' || c > '9') {if(c == '-') s = -1; c = getchar(); } while(c >= '0' && c <= '9') {x = x*10 + c -'0'; c = getchar(); } return x*s; } using namespace std; #define NewNode (TreeNode *)malloc(sizeof(TreeNode)) #define Mem(a,b) memset(a,b,sizeof(a)) #define lowbit(x) (x)&(-x) const int N = 1e6 + 5; const long long INFINF = 0x7f7f7f7f7f7f7f; const int INF = 0x3f3f3f3f; const double EPS = 1e-7; const int mod = 998244353; const double II = acos(-1); const double PP = (II*1.0)/(180.00); typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef pair piil; signed main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int t; cin >> t; while(t--) { ll n,k; cin >> n >> k; if(n <= k) cout << k-n << endl; else if((n+k) % 2 == 0) cout << 0 << endl; else cout << 1 << endl; } }

    推荐阅读