Problem A. Number Theory Problem
Input file: Standard Input Output file: Standard Ouptut Time limit: 1 second
Mr. Panda is one of the top specialists on number theory all over the world. Now Mr. Panda is investigating the property of the powers of 2. Since 7 is the lucky number of Mr. Panda, he is always interested in the number that is a multiple of 7. However, we know that there is no power of 2 that is a multiple of 7, but a power of 2 subtracted by one can be a multiple of 7. Mr. Panda wants to know how many positive integers less than 2N in the form of 2k −1 (k is a positive integer) that is divisible by 7. N is a positive interger given by Mr Panda. Input The first line of the input gives the number of test cases, T. T test cases follow. Each test case contains only one positive interger N. Output For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the answer. Limits
• 1 ≤ T ≤ 100. • 1 ≤ N ≤ 105. Sample input and output Sample Input Sample Output 2 1 4 Case #1: 0 Case #2: 1
题意:问你有多少个整数形如2^k - 1且小于2^N的数能被7整除。
解题思路:直接快速幂搞一下就行,判断(2^k - 1)mod7 == 0就行。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int mod = 7;
const int maxn=100005;
ll ans[maxn];
void pow_mod()
{
int a,x,num=0;
for(int i=1;i<maxn;i++)
{
ans[i]=1;
x = i;
a = 2;
while(x)
{
if(x&1) ans[i] = (ans[i]*a)%mod;
a = (a*a)%mod;
x >>= 1;
}
if(ans[i]%mod==1) num++;
ans[i] = num;
}
}
int main()
{
pow_mod();
int t;
scanf("%d",&t);
for(int ca=1;ca<=t;ca++)
{
int n;
scanf("%d",&n);
printf("Case #%d: %d\n",ca,ans[n]);
}
return 0;
}