Mathematical Structure of uva12716 gcd xor Number Theory

Source: Internet
Author: User

The question gives you an N, asking you to calculate two numbers A and B, and a> = B <= N, yes, gcd (a, B) = a ^ B


The range of N is 3*10 ^ 7, which is scary. At first, I didn't dare to construct it, because even if the array to be constructed is too large, it's already 10 ^ 7, after thinking about the ^ operation for a long time, I didn't come up with anything here, so I had no way to construct it. I constructed two numbers according to the meaning of his question, in J, J is a multiple of I, so the maximum common divisor of J + I and I must be I, then (J + I) ^ I = I, the constructed result is satisfied, and then the GCD will be imitated to put them in an array for counting, so that preprocessing can be done.


After playing well, I ran another brute-force program to run the answer. The result was correct, but the transaction timed out, because the preprocessing at the beginning assigned the long type, there is a % operation in the Process of division, which will lead to a very slow operation, so you can change it to int.


#define  _CRT_SECURE_NO_WARNINGS/*#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<string>#include<math.h>using namespace std;#define IN freopen("c:\\Users\\nit\\desktop\\input.txt", "r", stdin)    #define OUT freopen("c:\\Users\\nit\\desktop\\output.txt", "w", stdout)   int gcd(int a,int b){return b==0?a:gcd(b,a%b);}int main(){OUT;int ans[510],k=0;memset(ans,0,sizeof(ans));for(int i=1;i<500;i++){for(int b=1;b<=i;b++){for(int a=b;a<=i;a++){if((a^b)==gcd(a,b))ans[i]++;}}printf("%d\t",ans[i]);if(k%10==0)puts("");k++;}return 0;}*/#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<string>#include<math.h>using namespace std;//#define IN freopen("c:\\Users\\nit\\desktop\\input.txt", "r", stdin)    //#define OUT freopen("c:\\Users\\nit\\desktop\\outpu1t.txt", "w", stdout)   #define ll long long#define MAXN 30000000 + 5ll ans[MAXN];void init() {for(ll i = 1;i<MAXN;i++) {for(ll j = i;i + j < MAXN;j += i) {if( ((i + j)^j) == i) {int x = j;int y = i + j;for(;x > 0 && y > 0;) {int tmp = x%y;x = y;y = tmp;if((x + y) == i)ans[i + j]++;}}}}for(int i = 2;i<MAXN;i++)ans[i] += ans[i-1];}int main() {init();int t;scanf("%d",&t);int Case = 0;while(t--) {int n;scanf("%d",&n);printf("Case %d: %lld\n",++Case,ans[n]);}return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.