【BJTU+A simple problem+异或+数位DP】

来源:互联网 发布:c语言编译器中文版 编辑:程序博客网 时间:2024/06/05 17:51

【题目描述】:

题面描述


求 0~n 中有多少个整数 k 满足 (2 * k) XOR (3 * k) == k, 其中 XOR 为异或操作。

输入数据

输入数据的第一行为一个正整数 T(T<=30) ,表示测试数据的组数。

接下来的 T 行中,每行为一组测试数据,包含一个正整数 n(n<=1000000000) 。

输出数据

对每一组输入数据,输出一行结果 ”Case #id: M” ,表示第 id 组数据的结果是 M , id 从 1 开始。

例如:

sample input:

5

0

1

2

3

4

sample output:

1

2

3

3

4

【思路】:

【方法一】推公式和规律无解情况下, 暴力+打表可过:

代码如下:

/***********************BJTU A sample problem【异或+数位DP】Author:herongweiTime:2017/5/12 19:00language:C++http://blog.csdn.net/u013050857***********************/#include <bits/stdc++.h>#include <iostream>#include <algorithm>#define rep(i,k,n) for(int i=k;i<=n;++i)#define rep2(i,k,n) for(int i=k;i>=n;--i)using namespace std;const int maxn= 1e6+233;const int N= 1e6;const int MOD = 1e9+7;typedef long long LL;int t,n,m,k,ret,ans,tot=0;inline int read(){    int  c=0,f=1;    char ch=getchar();    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){c=c*10+ch-'0';ch=getchar();}    return c*f;}//表和表的长度;int dp[N]= {1,17711,28657,46368,46368,64079,75025,75025,75025,90085,103682,114628,121393,121393,121393,121393,121393,128158,145760,154231,167761,169358,185472,196418,196418,196418,196418,196418,196418,196418,196418,196418,196418,196418,207364,225075,235844,242786,249551,267262,271443,271443,274027,289819,300100,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,317811,335522,346468,364179,364179,381603,392836,392836,392836,403782,421493,432439,439204,439204,439204,439204,439204,443385,459499,468937,485572,485572,503283,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,514229,531940,542886,559610,560597,574127,589254,589254,589254,600200,617446,626506,635622,635622,635622,635622,635622,636143,653333,664279,681990,681990,699701,710647,710647,710647,710647,710647,710647,710647,710647,710647,710647,710647,717412,735123,743485,757015,758756,774726,785672,785672,785672,803383,814329,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,832040,845193,860697,871643,878408,889354,905468,907065,907065,913830,928957,            939293,953433,953433,953433,953433,953433,953433,971144,982090,999048,999801,1013708,1028458,1028458,1028458,1028458,1028458,1028458,1028458,1028458,1028458,1028458,1028458,1029301,1046169,1057115,1074826,1074826,1092537,1103483,1103483,1103483,1118610,1132140,1143108,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1149851,1160797,1178508,1189454,1196219,1202984,1220695,1224876,1224876,1227693,1243829,1253533,1271244,1271244,1271244,1271244,1271244,1271244,1287968,1299901,1313431,1317612,1328558,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,            1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,1346269,            1346269,1346269,1350450,1367551,1376523,1392637,1392637,1410348,1421294,1421294,1421294,1439005,1449951,1465078,1467662,1467662,1467662,1467662,1467662,1478608,1494400,1503084,1514030,1519808,1535922,1542687,1542687,1542687,1542687,1542687,1542687,1542687,1542687,1542687,1542687,1542687,1556217,1571344,1582290,1589055,1600001,1616493,1617712,1617712,1624477,1640214,1650550,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1664080,1665444,1681791,1692737,1710448,1710448,1728159,1739105,1739105,1739105,1754232,1767762,1779551,1785473,1785473,1785473,1785473,1785473,1792238,1809949,1819298,1831841,1834425,1849587,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1860498,1878209,1889155,1906866,1906866,1924577,1935523,1935523,1935523,1946469,1964180,1975126,1981891,1981891,1981891,1981891,1981891,1986449,2003550,2012557,2028259,2028259,2045970,2056916,2056916,2056916,2056916,2056916,2056916,2056916,2056916,2056916,2056916,2056916,2067862,2083976,2092338,2103284,2109062,2125176,2131941,2131941,2131941,2149652,2160598,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,            2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309,            2178309,2178309,2178309,2178309,2178309,2178309,2178309,2178309           };//打表专用void init(){   memset(dp,0,sizeof(dp));   LL k=0,sum=0;   for(int i=0; i<=1e9; ++i){       LL xx=2*i,yy=3*i;       xx^=yy;       if(xx==i) sum++;       if(i%N==0) printf("%d,",sum);   }}int main(){    // freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    //init();    t=read();    while(t--){        n=read();        int ans=dp[n/N];       // cout<<"ans= "<<ans<<endl;       //从当前区间计数最大复杂度O(1e6)        for(LL i=n/N*N+1; i<=n; ++i){            LL xx=i*2,yy=3*i;            xx^=yy;            if(xx==i) ans++;        }        printf("Case #%d: %d\n",++tot,ans);    }    return 0;}

暂时看不懂的标程:

#include <iostream>  #include <cstdio>  #include <cstring>  #include <vector>  #include <algorithm>  #include <stack>  using namespace std;    int c[51][2];  int s[40];  int main() {      int T;      c[0][0] = 1;      c[1][1] = 1;      c[1][0] = 1;      for(int i = 1; i < 50; i++) {          c[i + 1][1] += c[i][0];          c[i + 1][0] += c[i][1] + c[i][0];      }      scanf("%d", &T);      for(int ii = 0; ii < T; ii++) {          int x;          scanf("%d", &x);          x++;          int l = 0;          while(x) {              s[l++] = x % 2;              x /= 2;          }          int ans = 0;          int pre = -1;          while(l) {              int x = s[--l];              if(x == 1) ans += c[l][0] + c[l][1];              if(x == 1 && pre == 1) break;              pre = x;          }          printf("Case #%d: %d\n", ii + 1, ans);      }         return 0;  }  



1 0