示例#1
0
int main()
{
	phi_table(maxn);
	scanf("%lld %d", &N, &K);
	if (K == 1 || K == 3)
	{
		inv = get_inv(2);
	}
	else if (K == 2)
	{
		inv = get_inv(6);
	}
	else if (K == 4)
	{
		inv = get_inv(30);
	}
	else if (K == 5)
	{
		inv = get_inv(12);
	}
	long long ans = 0;
	for (int i = 1, j = N; i <= j; ++i, j = N / i)
	{
		printf("%lld %lld\n", pow_mod(i, K), euler_phi(N / i));
		ans = (ans + pow_mod(i, K) * euler_phi(N / i) % mod) % mod;
		ans = (ans + (sum(j, K) - sum(N / (i + 1), K)) * euler_phi(i) % mod) % mod;
	}
	if (ans < 0)
	{
		ans += mod;
	}
	printf("%lld\n", ans);
	return 0;
}
示例#2
0
int main(){
    int n;
    while(scanf("%d", &n) == 1){
        printf("¦Õ(%d) = %d\n", n, euler_phi(n));
    } 
    return 0;
}    
示例#3
0
int main()
{
	int n;
	while(scanf("%d",&n),n)
		printf("%I64d\n",euler_phi(n));
	return 0;
}
int * makeTest(int m, int print = 0){
    if (print) printf("Exist %d values for a\n", euler_phi(m));

    int * ret = (int *) calloc(euler_phi(m), sizeof(int));
    int nTest,i =0;
    for (nTest = 0; nTest < m; ++nTest)
    {
        int result = mdc(m, nTest);

        if(result == 1){
            if(print) printf("a = %d\n", nTest);

            ret[i++] = nTest;
        }
    }
    return ret;
}
示例#5
0
文件: uva10692.cpp 项目: ksBSB/ACM
int solve (int d, int M) {
	if (d == k - 1)
		return A[d]%M;

	int phi = euler_phi(M);
		int c = solve (d+1, phi) + phi;
	return pow_mod(A[d], c, M);
}
void broke(){

    m = 26;
    printf("Possible keys:\n");
    printf("\t b = any element of Zm\n");
    int * as = makeTest(m,1);
    printf("%d posibity of keys\n", euler_phi(m) * m);
    int i, j;
    for(i = 0; i<euler_phi(m); i++){
        a=as[i];
        for (j = 0; j < m; ++j)
        {
            b=j;
            printf("a=%d, b=%d\n",a, b );
            if(decript("c")[0] == 'e' && decript("b")[0] == 't')
                printf("%s\n", decript(text));

        }
    }


 }
示例#7
0
int primitive_root(int m, prime_holder& prim) {
	int phi = euler_phi(prim.factor_integer(m));
	auto phi_factors = prime_factors(prim.factor_integer(phi));
	return primitive_root(m, phi, phi_factors);
}
示例#8
0
文件: cofact_plan.c 项目: pstach/gls
void stage2_plan_init(stage2_plan_t *plan, u_int32_t B2_min, u_int32_t B2)
{
	unsigned int p, n_primes, n_pairs;
	unsigned int i, i_min, i_max, j, d, m, n, not_in_d;
	u_int8_t *primes;
	int need_NEXT_D;
	const int composite_pairing = 1;
	mpz_t P;

	plan->B2 = B2;
	if(B2 <= B2_min)
	{
		plan->d = 0;
		plan->n_S1 = 0;
		plan->S1 = NULL;
		plan->pairs = NULL;
		return;
	}

	mpz_init(P);
	/* Choose a value for d. Should depend on B2-B2min, for a start we fix d=210 */
	d = 210;
	plan->d = d;
	not_in_d = 11; /* the smallest prime not in d */

	/* List of the j values for which we need to precompute V_j(x + 1/x) */
	plan->n_S1 = (u_int32_t) (euler_phi((u_int64_t) d) / 2);
	/* TODO: can these indexes be 16 bit given more appropriate values of d? saves on GPU shared memory */
	plan->S1 = (u_int32_t *) malloc(plan->n_S1 * sizeof(u_int32_t));

	for(i = 0, j = 1; j < d / 2; j += 2)
	{
		if(gcd(j, d) == 1)
			plan->S1[i++] = j;
	}
#ifdef VERBOSE_PLAN
	printf("S_1 = {");
	for(i = 0; i < plan->n_S1; i++)
		printf("%d%s", plan->S1[i], (i + 1 < plan->n_S1) ? ", " : "");
	printf("}\n");
#endif
	/* Preliminary choice for the smallest and largest i value we might need.
	 * The smallest may increase yet due to pairing with composite values.
	 * p > B2min, so i*d > B2min - max(S_1), or i >= ceil((B2min - max(S_1)) / d).
	 * For now we have max(S_1) < d/2, so we can write i >= floor((B2min + d/2) / d)
	 * p <= B2, so i*d +- j <= B2, so i*d - j <= B2, so i*d <= B2 + max(S_1),
	 * or i <= floor((B2 + max(S_1)) / d).
	 */
	i_min = (B2_min + d / 2) / d;
	i_max = (B2 + d / 2) / d;

#ifdef VERBOSE_PLAN
	printf("Initial choice for min_i = %u, max_i = %u\n", i_min, i_max);
#endif

	/* Generate the list of pairs for stage 2 */
	/* For each prime B2min < p <= B2, we write p = id-j, j in S1,
	 * and write into *pairs the index of j within the S_2 array.
	 * When it's time to increase i, NEXT_D is written to *pairs.
	 * NEXT_PASS is the signal to end stage 2.
	 */

	/* Make array where entry at index p, prime p with B2min < p <= B2, are
	 * set to 1. The largest value we can write as i*d+j with i < max_i and
	 * j < d/2 is max_i * d + d/2 - 1
	 */
	primes = (u_int8_t *) malloc(i_max * d + d/2);
	memset(primes, 0, i_max * d + d/2);
	n_primes = 0;

	mpz_set_ui(P, B2_min);
	for(mpz_nextprime(P, P); mpz_cmp_ui(P, B2) <= 0; mpz_nextprime(P, P))
	{
		n_primes++;
		primes[mpz_get_ui(P)] = 1;
	}

	/* We need at most one pair per prime, plus the number of NEXT_D and NEXT_PASS codes */
    plan->n_pairs = n_primes + (B2 - B2_min) / d + 1;
	plan->pairs = (u_int8_t *) malloc(plan->n_pairs);

	/* Lower max_i so that max_i * d +- j, 0 < j < d/2, actually includes any primes */
	for(i = i_max; i >= i_min; i--)
	{
		for (m = 0; m < plan->n_S1; m++)
		{
			j = plan->S1[m];
			if ((i * d >= j && primes[i * d - j]) || primes[i * d + j])
			{
#ifdef VERBOSE_PLAN
				printf("Final max_i = %u, found prime %u or %u\n",
						i, i*d - j, i*d + j);
#endif
				break;
			}
		}
		if (m < plan->n_S1)
			break;
	}
	i_max = i;

	n = 0;
	n_pairs = 0;
	need_NEXT_D = 0;

	if(composite_pairing)
	{
		/* Do a pass over the primes in reverse, flagging off those primes
		 * that are included as composite values id+-j, where id-+j is prime.
		 * The smallest prime not in d is not_in_d, so any proper divisor of
		 * i*d +- j is at most (i*d+d/2+1) / not_in_d
		 */
		for(p = B2; p >= (i_max * d + d/2 - 1) / not_in_d; p--)
		{
			if(primes[p])
			{
				unsigned int q, r;
				int sj;
				/* p = id + j or id - j with 0 < j < d/2 */
				j = p % d;
				sj = (int) j - ((j > d / 2) ? (int) d : 0);
				/* p = i*d + sj, q = i*d - sj = p - 2*sj */
				if((int) p < 2 * sj)
					continue;
				q = (int) p - 2 * sj;

				if(!primes[q]) /* If q is not a prime, see if a stage 2 prime divides q */
				{
					r = find_prime_div (q, not_in_d, primes);
					if(r)
					{
#ifdef VERBOSE_PLAN
						printf("Including %u as factor of %u = %u * %u "
								"+ %d which pairs with prime %u = %u"
								" * %u + %d\n",
								r, q, (p - sj) / d, d, -sj, p,
								(p-sj)/d, d, sj);
#endif
						primes[r] = 0;
					}
				}
			}
		}

		/* Some small primes may remain. Go through all possible (i,j)
		 * values in order of decreasing id+j, and choose those that
		 * cover two small primes
		 */
		for(i = i_max; i >= i_min && i > 0; i--)
		{
			for(m = 0; m < plan->n_S1; m++)
			{
				unsigned int q;

				j = plan->S1[m];
				p = i * d - j;
				q = i * d + j;

				if(!primes[q] && !primes[q])
				{
					unsigned int r1, r2;

					r1 = find_prime_div(p, not_in_d, primes);
					r2 = find_prime_div(q, not_in_d, primes);

					if(r1 && r2)
					{
						/* Flag on one of the two "primes" that include these two composite values */
						primes[p] = 1;
						primes[r1] = 0;
						primes[r2] = 0;
#ifdef VERBOSE_PLAN
						printf("Including %u * %u +- %u which includes "
								"%u and %u as factors\n",
								i, d, j, r1, r2);
#endif
					}
				}
			}
		}

		/* Some small primes may still remain. Go through all possible (i,j)
		 * values in order of decreasing id+j again, and choose those that
		 * cover at least one prime
		 */
		for(i = i_max; i >= i_min && i > 0; i--)
		{
			for(m = 0; m < plan->n_S1; m++)
			{
				unsigned int q;

				j = plan->S1[m];
				p = i * d - j;
				q = i * d + j;

				if(!primes[p] && !primes[q])
				{
					unsigned int r1, r2;

					r1 = find_prime_div(p, not_in_d, primes);
					r2 = find_prime_div(q, not_in_d, primes);

					if(r1 || r2)
					{
						/* Flag on one of the two "primes" that include this composite value */
						primes[p] = 1;
						primes[r1] = 0;
						primes[r2] = 0;
#ifdef VERBOSE_PLAN
						printf("Including %u * %u +- %u which includes "
								"%u as factors\n",
								i, d, j, (r1 == 0) ? r2 : r1);
#endif
					}
				}
			}
		}
	}

	/* Increase min_i so that min_i * d +- j, 0 < j < d/2, actually includes any primes */
	for(i = i_min; i <= i_max; i++)
	{
		for (m = 0; m < plan->n_S1; m++)
		{
			j = plan->S1[m];
			if((i * d >= j && primes[i * d - j]) || primes[i * d + j])
			{
#ifdef VERBOSE_PLAN
				printf("Final min_i = %u, found prime %u or %u\n",
					i, i*d - j, i*d + j);
#endif
				break;
			}
		}
		if(m < plan->n_S1)
			break;
	}
	i_min = i;


	/* For the remaining primes, write the required (i,j)-pairs to a list */
	for(i = i_min; i <= i_max; i++)
	{
		for(m = 0; m < plan->n_S1; m++)
		{
			j = plan->S1[m];
			/* See if this is a i*d +- j we need to include */
			if((i * d >= j && primes[i * d - j]) || primes[i * d + j])
			{
				while (need_NEXT_D)
				{
					plan->pairs[n++] = NEXT_D;
#ifdef VERBOSE_PLAN
					printf("Adding NEXT_D to list\n");
#endif
					need_NEXT_D--;
				}

#ifdef VERBOSE_PLAN
				printf("Adding %d*d +- %d (=S1[%d]) to list, includes primes ",
						i, j, m);
				if(i * d >= j && primes[i * d - j])
					printf("%d ", i*d - j);
				if(i * d + j <= B2 && primes[i * d + j])
					printf("%d", i*d + j);
				printf("\n");
#endif

				plan->pairs[n++] = (u_int8_t) m;
				n_pairs++;
				if(i * d >= j)
					primes[i * d - j] = 0;

				if(i * d + j <= B2)
					primes[i * d + j] = 0;
			}
		}

		need_NEXT_D++;
	}

	plan->pairs[n++] = NEXT_PASS;
#ifdef VERBOSE_PLAN
	printf("Adding NEXT_PASS to list\n");
#endif
	plan->i0 = i_min;
	plan->i1 = i_max + 1;

#ifdef VERBOSE_PLAN
	printf("pairs = ");
	for(i = 0; i < n; i++)
	{
		if(plan->pairs[i] == NEXT_D)
			printf("NEXT_D ");
		else if(plan->pairs[i] == NEXT_PASS)
			printf ("NEXT_PASS ");
		else
			printf ("%d ", plan->pairs[i]);
	}
	printf ("\n");

	printf("Used %u pairs to include %u primes, avg. %.2f primes/pair\n",
		n_pairs, n_primes, (double)n_primes / (double) n_pairs);

	for(i = B2_min + 1; i <= B2; i++)
	{
		if(primes[i])
		{
			fprintf(stderr, "Error, prime %d is still set\n", i);
			exit(-1);
		}
	}
#endif
	free(primes);
	return;
}